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

Wrap GZIPInputStream with a ConsumingInputStream to drain on close #749

Closed
wants to merge 2 commits into from
Closed
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 @@ -331,7 +331,8 @@ public InputStream getContent() throws IOException {
if (!returnRawInputStream
&& contentEncoding != null
&& contentEncoding.contains("gzip")) {
lowLevelResponseContent = new GZIPInputStream(lowLevelResponseContent);
lowLevelResponseContent = new ConsumingInputStream(
new GZIPInputStream(lowLevelResponseContent));
}
// logging (wrap content with LoggingInputStream)
Logger logger = HttpTransport.LOGGER;
Expand All @@ -356,6 +357,44 @@ public InputStream getContent() throws IOException {
return content;
}

static class ConsumingInputStream extends InputStream {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An outer class would better separate unrelated code. This is complex enough to justify it.

private InputStream inputStream;
private boolean closed = false;

private ConsumingInputStream(InputStream inputStream) {
this.inputStream = Preconditions.checkNotNull(inputStream);
}

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

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

@Override
public void close() throws IOException {
if (!closed && inputStream != null) {
try {
drainInputStream(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it important to drain the input stream and throw away the excess data, instead of simply closing the stream?

inputStream.close();
} finally {
this.closed = true;
}
}
}

static void drainInputStream(final InputStream inputStream) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method can be private. Also you've reinvented ByteStreams.exhaust from Guava.

byte buffer[] = new byte[1024];
while (inputStream.read(buffer) >= 0) {
// do nothing
}
}
}

/**
* Writes the content of the HTTP response into the given destination output stream.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -457,4 +459,40 @@ public LowLevelHttpResponse execute() throws IOException {
"it should not decompress stream",
request.execute().getContent() instanceof GZIPInputStream);
}

public void testGetContent_gzipEncoding_finishReading() throws IOException {
byte[] dataToCompress = "abcd".getBytes(StandardCharsets.UTF_8);
byte[] mockBytes;
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) {
GZIPOutputStream zipStream = new GZIPOutputStream((byteStream));
zipStream.write(dataToCompress);
zipStream.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also have a case where there's extra data on the stream after the gzipped content, since handling that is the purpose of this PR.

mockBytes = byteStream.toByteArray();
}
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.setContent(mockBytes);
mockResponse.setContentEncoding("gzip");
mockResponse.setContentType("text/plain");

HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, final String url)
throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
return mockResponse;
}
};
}
};
HttpRequest request =
transport.createRequestFactory().buildHeadRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();
TestableByteArrayInputStream output = (TestableByteArrayInputStream) mockResponse.getContent();
assertFalse(output.isClosed());
assertEquals("abcd", response.parseAsString());
assertTrue(output.isClosed());
}
}