Skip to content

Commit

Permalink
Wrap GZIPInputStream with a ConsumingInputStream to drain on close
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 authored and codyoss committed Oct 14, 2019
1 parent ea09fd5 commit abf01a5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
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 {
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);
inputStream.close();
} finally {
this.closed = true;
}
}
}

static void drainInputStream(final InputStream inputStream) throws IOException {
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,11 +26,14 @@
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;
import sun.net.www.http.ChunkedInputStream;

/**
* Tests {@link HttpResponse}.
Expand Down Expand Up @@ -457,4 +460,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();
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());
}
}

0 comments on commit abf01a5

Please sign in to comment.