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 commented Jul 15, 2019

Fixes #367

@chingor13 chingor13 requested a review from googleapis/yoshi-java 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 {

This comment has been minimized.

Copy link
@elharo

elharo Jul 29, 2019

Collaborator

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);

This comment has been minimized.

Copy link
@elharo

elharo Jul 29, 2019

Collaborator

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();

This comment has been minimized.

Copy link
@elharo

elharo Jul 29, 2019

Collaborator

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 {

This comment has been minimized.

Copy link
@elharo

elharo Jul 29, 2019

Collaborator

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 pull request 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 pull request 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 pull request 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 chingor13:chunked-keepalive branch Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.