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

Conversation

chingor13
Copy link
Collaborator

@chingor13 chingor13 commented Jul 15, 2019

Fixes #367

@chingor13 chingor13 requested a review from as a code owner Jul 15, 2019
@googlebot googlebot added the cla: yes label Jul 15, 2019
@@ -356,6 +357,44 @@ public InputStream getContent() throws IOException {
return content;
}

static class ConsumingInputStream extends InputStream {
Copy link
Collaborator

@elharo elharo Jul 29, 2019

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.

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

@elharo elharo Jul 29, 2019

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?

try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(dataToCompress.length)) {
GZIPOutputStream zipStream = new GZIPOutputStream((byteStream));
zipStream.write(dataToCompress);
zipStream.close();
Copy link
Collaborator

@elharo elharo Jul 29, 2019

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.

}
}

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

@elharo elharo Jul 29, 2019

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.

codyoss added a commit to codyoss/google-http-java-client that referenced this issue Oct 14, 2019
If a connection is closed and there are some bytes that have not
been read that connection can't be reused. Now GZIPInputStream
will have all of its bytes read on close automatically to promote
connection reuse.

Checkpicked from: googleapis#749
Fixes: googleapis#367
codyoss added a commit to codyoss/google-http-java-client that referenced this issue Oct 14, 2019
If a connection is closed and there are some bytes that have not
been read that connection can't be reused. Now GZIPInputStream
will have all of its bytes read on close automatically to promote
connection reuse.

Cherry-picked: googleapis#749
Fixes: googleapis#367
codyoss added a commit that referenced this issue Oct 18, 2019
If a connection is closed and there are some bytes that have not
been read that connection can't be reused. Now GZIPInputStream
will have all of its bytes read on close automatically to promote
connection reuse.

Cherry-picked: #749
Fixes: #367
@chingor13 chingor13 closed this Oct 22, 2019
@chingor13 chingor13 deleted the chunked-keepalive branch Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants