Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input streams returned by DecompressingEntity.getContent() should support mark(int) and reset() #567

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
}

}