-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap GZIPInputStream for connection reuse (#840)
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
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
google-http-client/src/main/java/com/google/api/client/http/ConsumingInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.api.client.http; | ||
|
||
import com.google.common.io.ByteStreams; | ||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* This class in meant to wrap an {@link InputStream} so that all bytes in the steam are read and | ||
* discarded on {@link InputStream#close()}. This ensures that the underlying connection has the | ||
* option to be reused. | ||
*/ | ||
final class ConsumingInputStream extends FilterInputStream { | ||
private boolean closed = false; | ||
|
||
ConsumingInputStream(InputStream inputStream) { | ||
super(inputStream); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (!closed && in != null) { | ||
try { | ||
ByteStreams.exhaust(this); | ||
super.in.close(); | ||
} finally { | ||
this.closed = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
google-http-client/src/test/java/com/google/api/client/http/ConsumingInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.api.client.http; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
|
||
public class ConsumingInputStreamTest { | ||
|
||
@Test | ||
public void testClose_drainsBytesOnClose() throws IOException { | ||
MockInputStream mockInputStream = new MockInputStream("abc123".getBytes(StandardCharsets.UTF_8)); | ||
InputStream consumingInputStream = new ConsumingInputStream(mockInputStream); | ||
|
||
assertEquals(6, mockInputStream.getBytesToRead()); | ||
|
||
// read one byte | ||
consumingInputStream.read(); | ||
assertEquals(5, mockInputStream.getBytesToRead()); | ||
|
||
// closing the stream should read the remaining bytes | ||
consumingInputStream.close(); | ||
assertEquals(0, mockInputStream.getBytesToRead()); | ||
} | ||
|
||
private class MockInputStream extends InputStream { | ||
private int bytesToRead; | ||
|
||
MockInputStream(byte[] data) { | ||
this.bytesToRead = data.length; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (bytesToRead == 0) { | ||
return -1; | ||
} | ||
bytesToRead--; | ||
return 1; | ||
} | ||
|
||
int getBytesToRead() { | ||
return bytesToRead; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters