Last active
August 29, 2015 14:19
-
-
Save richard1122/d7643ed74b4b207e33ca to your computer and use it in GitHub Desktop.
Android HTTPURLConnection auto read gzip inputStream
This file contains hidden or 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
public static String networkInputstreamtoString(InputStream is) throws IOException { | |
byte []sig = new byte[2]; | |
final PushbackInputStream pb = new PushbackInputStream(is, sig.length); | |
pb.read(sig); | |
pb.unread(sig); | |
if (sig[0] == (byte) 0x1f && sig[1] == (byte) 0x8b) { | |
is = new GZIPInputStream(pb); | |
} else { | |
is = new BufferedInputStream(pb); | |
} | |
int len; | |
byte buffer[] = new byte[1024]; | |
final StringBuilder sb = new StringBuilder(); | |
try { | |
while ((len = is.read(buffer)) != -1) { | |
sb.append(new String(buffer, 0, len, "UTF-8")); | |
} | |
return sb.toString(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment