Skip to content

Instantly share code, notes, and snippets.

@evanhalley
Created January 12, 2016 17:09
Show Gist options
  • Save evanhalley/c3a948fb9b227e59716b to your computer and use it in GitHub Desktop.
Save evanhalley/c3a948fb9b227e59716b to your computer and use it in GitHub Desktop.
public class ConnectionStreamDetectionExample {
/**
* Returns the correct input stream based on the content encoding
* @param connection
* @return
* @throws IOException
*/
private static InputStream getInputStream(HttpURLConnection connection) throws IOException {
InputStream inputStream = null;
if (connection.getContentEncoding() != null) {
if (connection.getContentEncoding().equalsIgnoreCase("gzip") ||
connection.getContentEncoding().equalsIgnoreCase("x-gzip")) {
inputStream = new GZIPInputStream(connection.getInputStream());
} else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) {
inputStream = new DeflaterInputStream(connection.getInputStream());
}
}
if (inputStream == null) {
inputStream = connection.getInputStream();
}
return inputStream;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment