Skip to content

Commit 8b39c31

Browse files
committed
Use gzip for compressing content and fix ourput stream memory leak risk.
1 parent 047c0af commit 8b39c31

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

firebase-segmentation/src/main/java/com/google/firebase/segmentation/remote/SegmentationServiceClient.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import androidx.annotation.NonNull;
1818
import java.io.IOException;
19-
import java.io.OutputStream;
2019
import java.net.URL;
20+
import java.util.zip.GZIPOutputStream;
2121
import javax.net.ssl.HttpsURLConnection;
2222
import org.json.JSONException;
2323
import org.json.JSONObject;
@@ -33,6 +33,11 @@ public class SegmentationServiceClient {
3333
"projects/%s/installations/%s/customSegmentationData:clear";
3434
private static final String FIREBASE_SEGMENTATION_API_VERSION = "v1alpha";
3535

36+
private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type";
37+
private static final String JSON_CONTENT_TYPE = "application/json";
38+
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding";
39+
private static final String GZIP_CONTENT_ENCODING = "gzip";
40+
3641
public enum Code {
3742
OK,
3843

@@ -71,18 +76,21 @@ public Code updateCustomInstallationId(
7176
httpsURLConnection.setRequestMethod("PATCH");
7277
httpsURLConnection.addRequestProperty(
7378
"Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken);
74-
httpsURLConnection.addRequestProperty("Content-Type", "application/json");
75-
OutputStream os = httpsURLConnection.getOutputStream();
79+
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE);
80+
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
81+
GZIPOutputStream gzipOutputStream =
82+
new GZIPOutputStream(httpsURLConnection.getOutputStream());
7683
try {
77-
os.write(
84+
gzipOutputStream.write(
7885
buildUpdateCustomSegmentationDataRequestBody(resourceName, customInstallationId)
7986
.toString()
8087
.getBytes("UTF-8"));
8188
} catch (JSONException e) {
8289
throw new IllegalStateException(e);
90+
} finally {
91+
gzipOutputStream.close();
8392
}
8493
httpsURLConnection.connect();
85-
8694
int httpResponseCode = httpsURLConnection.getResponseCode();
8795
switch (httpResponseCode) {
8896
case 200:
@@ -130,13 +138,17 @@ public Code clearCustomInstallationId(
130138
httpsURLConnection.setRequestMethod("POST");
131139
httpsURLConnection.addRequestProperty(
132140
"Authorization", "FIREBASE_INSTALLATIONS_AUTH " + firebaseInstanceIdToken);
133-
httpsURLConnection.addRequestProperty("Content-Type", "application/json");
134-
OutputStream os = httpsURLConnection.getOutputStream();
141+
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE);
142+
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING);
143+
GZIPOutputStream gzipOutputStream =
144+
new GZIPOutputStream(httpsURLConnection.getOutputStream());
135145
try {
136-
os.write(
146+
gzipOutputStream.write(
137147
buildClearCustomSegmentationDataRequestBody(resourceName).toString().getBytes("UTF-8"));
138148
} catch (JSONException e) {
139149
throw new IllegalStateException(e);
150+
} finally {
151+
gzipOutputStream.close();
140152
}
141153
httpsURLConnection.connect();
142154

0 commit comments

Comments
 (0)