Skip to content

Commit 25023d8

Browse files
committedJun 7, 2014
Decompressing returned InputStream only if it ontains GZIP/DEFLATE compressed data (signature check)
1 parent 3751ae6 commit 25023d8

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed
 

‎library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import java.io.IOException;
7373
import java.io.InputStream;
7474
import java.io.OutputStream;
75+
import java.io.PushbackInputStream;
7576
import java.net.URI;
7677
import java.util.Collections;
7778
import java.util.HashMap;
@@ -1164,6 +1165,23 @@ public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url,
11641165
return url;
11651166
}
11661167

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+
11671185
/**
11681186
* A utility function to close an input stream without raising an exception.
11691187
*
@@ -1247,7 +1265,12 @@ public InflatingEntity(HttpEntity wrapped) {
12471265

12481266
@Override
12491267
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+
}
12511274
}
12521275

12531276
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.