Skip to content

Commit

Permalink
Input streams returned by DecompressingEntity.getContent() should
Browse files Browse the repository at this point in the history
support mark(int) and reset() if the underlying stream does

- Pass calls to the underlying InputStream for mark(int)
- Pass calls to the underlying InputStream for reset()
- LazyDecompressingInputStream extends FilterInputStream
- Reimplement internals fluently
- Reuse Closer.close(Closeable)
- Tested locally with git master 5.3-beta2-SNAPSHOT
  • Loading branch information
garydgregory committed Aug 17, 2024
1 parent d7ed568 commit 2dafdca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,54 @@
*/
package org.apache.hc.client5.http.entity;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;

import org.apache.hc.core5.io.Closer;

/**
* Lazy init InputStream wrapper.
* Lazy initializes from an {@link InputStream} wrapper.
*/
class LazyDecompressingInputStream extends InputStream {
class LazyDecompressingInputStream extends FilterInputStream {

private final InputStream wrappedStream;
private final InputStreamFactory inputStreamFactory;

private InputStream wrapperStream;

public LazyDecompressingInputStream(
final InputStream wrappedStream,
final InputStreamFactory inputStreamFactory) {
this.wrappedStream = wrappedStream;
super(wrappedStream);
this.inputStreamFactory = inputStreamFactory;
}

private void initWrapper() throws IOException {
private InputStream initWrapper() throws IOException {
if (wrapperStream == null) {
wrapperStream = inputStreamFactory.create(wrappedStream);
wrapperStream = inputStreamFactory.create(in);
}
return wrapperStream;
}

@Override
public int read() throws IOException {
initWrapper();
return wrapperStream.read();
return initWrapper().read();
}

@Override
public int read(final byte[] b) throws IOException {
initWrapper();
return wrapperStream.read(b);
return initWrapper().read(b);
}

@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
initWrapper();
return wrapperStream.read(b, off, len);
return initWrapper().read(b, off, len);
}

@Override
public long skip(final long n) throws IOException {
initWrapper();
return wrapperStream.skip(n);
return initWrapper().skip(n);
}

@Override
Expand All @@ -83,19 +83,29 @@ public boolean markSupported() {

@Override
public int available() throws IOException {
initWrapper();
return wrapperStream.available();
return initWrapper().available();
}

@Override
public void close() throws IOException {
try {
if (wrapperStream != null) {
wrapperStream.close();
}
Closer.close(wrapperStream);
} finally {
wrappedStream.close();
super.close();
}
}

@Override
public void mark(final int readlimit) {
try {
initWrapper().mark(readlimit);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void reset() throws IOException {
initWrapper().reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ void testStreaming() throws Exception {
EntityUtils.consume(entity);
}

@Test
void testStreamingMarking() throws Exception {
final CRC32 crc32 = new CRC32();
final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(StandardCharsets.US_ASCII));
final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.DEFAULT_TEXT);
final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
final InputStream in1 = entity.getContent();
Assertions.assertEquals('1', in1.read());
Assertions.assertEquals('2', in1.read());
in1.mark(1);
Assertions.assertEquals('3', in1.read());
in1.reset();
Assertions.assertEquals('3', in1.read());
EntityUtils.consume(entity);
}

@Test
void testWriteToStream() throws Exception {
final CRC32 crc32 = new CRC32();
Expand All @@ -101,3 +117,4 @@ public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
}

}

0 comments on commit 2dafdca

Please sign in to comment.