File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed
main/java/com/google/api/client/http
test/java/com/google/api/client/http Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -353,8 +353,13 @@ public InputStream getContent() throws IOException {
353
353
if (!returnRawInputStream && this .contentEncoding != null ) {
354
354
String oontentencoding = this .contentEncoding .trim ().toLowerCase (Locale .ENGLISH );
355
355
if (CONTENT_ENCODING_GZIP .equals (oontentencoding ) || CONTENT_ENCODING_XGZIP .equals (oontentencoding )) {
356
+ // Wrap the original stream in a ConsumingInputStream before passing it to
357
+ // GZIPInputStream. The GZIPInputStream leaves content unconsumed in the original
358
+ // stream (it almost always leaves the last chunk unconsumed in chunked responses).
359
+ // ConsumingInputStream ensures that any unconsumed bytes are read at close.
360
+ // GZIPInputStream.close() --> ConsumingInputStream.close() --> exhaust(ConsumingInputStream)
356
361
lowLevelResponseContent =
357
- new ConsumingInputStream (new GZIPInputStream (lowLevelResponseContent ));
362
+ new GZIPInputStream (new ConsumingInputStream (lowLevelResponseContent ));
358
363
}
359
364
}
360
365
// logging (wrap content with LoggingInputStream)
Original file line number Diff line number Diff line change @@ -567,6 +567,12 @@ private void do_testGetContent_gzipEncoding_finishReading(String contentEncoding
567
567
) {
568
568
zipStream .write (dataToCompress );
569
569
zipStream .close ();
570
+
571
+ // GZIPInputStream uses a default buffer of 512B. Add enough content to exceed this
572
+ // limit, so that some content will be left in the connection.
573
+ for (int i = 0 ; i < 1024 ; i ++) {
574
+ byteStream .write ('7' );
575
+ }
570
576
mockBytes = byteStream .toByteArray ();
571
577
}
572
578
final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse ();
@@ -594,6 +600,8 @@ public LowLevelHttpResponse execute() throws IOException {
594
600
assertFalse (output .isClosed ());
595
601
assertEquals ("abcd" , response .parseAsString ());
596
602
assertTrue (output .isClosed ());
603
+ // The underlying stream should be fully consumed, even if gzip only returns some of it.
604
+ assertEquals (-1 , output .read ());
597
605
}
598
606
}
599
607
You can’t perform that action at this time.
0 commit comments