|
72 | 72 | import java.io.IOException;
|
73 | 73 | import java.io.InputStream;
|
74 | 74 | import java.io.OutputStream;
|
| 75 | +import java.io.PushbackInputStream; |
75 | 76 | import java.net.URI;
|
76 | 77 | import java.util.Collections;
|
77 | 78 | import java.util.HashMap;
|
@@ -1164,6 +1165,23 @@ public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url,
|
1164 | 1165 | return url;
|
1165 | 1166 | }
|
1166 | 1167 |
|
| 1168 | + /** |
| 1169 | + * Checks the InputStream if it contains GZIP compressed data |
| 1170 | + * |
| 1171 | + * @param inputStream InputStream to be checked |
| 1172 | + * @return true or false if the stream contains GZIP compressed data |
| 1173 | + */ |
| 1174 | + public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException { |
| 1175 | + if (inputStream == null) |
| 1176 | + return false; |
| 1177 | + |
| 1178 | + byte[] signature = new byte[2]; |
| 1179 | + int readStatus = inputStream.read(signature); |
| 1180 | + inputStream.unread(signature); |
| 1181 | + int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00); |
| 1182 | + return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader; |
| 1183 | + } |
| 1184 | + |
1167 | 1185 | /**
|
1168 | 1186 | * A utility function to close an input stream without raising an exception.
|
1169 | 1187 | *
|
@@ -1247,7 +1265,12 @@ public InflatingEntity(HttpEntity wrapped) {
|
1247 | 1265 |
|
1248 | 1266 | @Override
|
1249 | 1267 | public InputStream getContent() throws IOException {
|
1250 |
| - return new GZIPInputStream(wrappedEntity.getContent()); |
| 1268 | + PushbackInputStream content = new PushbackInputStream(wrappedEntity.getContent(), 2); |
| 1269 | + if (isInputStreamGZIPCompressed(content)) { |
| 1270 | + return new GZIPInputStream(content); |
| 1271 | + } else { |
| 1272 | + return content; |
| 1273 | + } |
1251 | 1274 | }
|
1252 | 1275 |
|
1253 | 1276 | @Override
|
|
0 commit comments