Skip to content

Commit f59c0eb

Browse files
authored
Decompress gzipped files before saving to local storage (#416)
1 parent 514030f commit f59c0eb

File tree

1 file changed

+17
-3
lines changed
  • AndroidSDKCore/src/main/java/com/leanplum/internal

1 file changed

+17
-3
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,22 @@ public static HttpURLConnection uploadFilesOperation(
670670
return urlConnection;
671671
}
672672

673+
private static boolean isGzipCompressed(URLConnection op) {
674+
String contentHeader = op.getHeaderField("content-encoding");
675+
if (contentHeader != null) {
676+
return contentHeader.trim().equalsIgnoreCase(Constants.LEANPLUM_SUPPORTED_ENCODING);
677+
}
678+
return false;
679+
}
680+
673681
public static void saveResponse(URLConnection op, OutputStream outputStream) throws IOException {
674682
InputStream is = op.getInputStream();
683+
684+
// If we have a gzipped response, de-compress it first
685+
if (isGzipCompressed(op)) {
686+
is = new GZIPInputStream(is);
687+
}
688+
675689
byte[] buffer = new byte[4096];
676690
int bytesRead;
677691
while ((bytesRead = is.read(buffer)) != -1) {
@@ -682,16 +696,16 @@ public static void saveResponse(URLConnection op, OutputStream outputStream) thr
682696

683697
private static String getResponse(HttpURLConnection op) throws IOException {
684698
InputStream inputStream;
685-
String contentHeader = op.getHeaderField("content-encoding");
686-
boolean isCompressed = contentHeader != null && contentHeader.trim().equalsIgnoreCase(Constants.LEANPLUM_SUPPORTED_ENCODING);
687699
if (op.getResponseCode() < 400) {
688700
inputStream = op.getInputStream();
689701
} else {
690702
inputStream = op.getErrorStream();
691703
}
692704

693705
// If we have a gzipped response, de-compress it first
694-
if (isCompressed) inputStream = new GZIPInputStream(inputStream);
706+
if (isGzipCompressed(op)) {
707+
inputStream = new GZIPInputStream(inputStream);
708+
}
695709

696710
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
697711
StringBuilder builder = new StringBuilder();

0 commit comments

Comments
 (0)