Skip to content

Commit a12868d

Browse files
authored
fix: call end inflater/deflater to avoid leaks (#422)
1 parent f11ed5b commit a12868d

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventHandlerUtils.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
package com.optimizely.ab.android.event_handler;
2-
3-
import static java.util.zip.Deflater.BEST_COMPRESSION;
1+
/****************************************************************************
2+
* Copyright 2022, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
416

5-
import android.os.Build;
17+
package com.optimizely.ab.android.event_handler;
618

719
import androidx.annotation.NonNull;
8-
import androidx.annotation.RequiresApi;
9-
import androidx.annotation.VisibleForTesting;
1020

1121
import org.apache.commons.codec.binary.Base64;
1222

13-
import java.io.ByteArrayInputStream;
1423
import java.io.ByteArrayOutputStream;
1524
import java.io.IOException;
16-
import java.nio.charset.Charset;
17-
import java.nio.charset.StandardCharsets;
1825
import java.util.zip.Deflater;
19-
import java.util.zip.GZIPInputStream;
20-
import java.util.zip.GZIPOutputStream;
2126
import java.util.zip.Inflater;
2227

2328
public class EventHandlerUtils {
@@ -42,6 +47,9 @@ public static String compress(@NonNull String decompressed) throws IOException {
4247
// encoded to Base64 (instead of byte[] since WorkManager.Data size is unexpectedly expanded with byte[]).
4348
return encodeToBase64(bytes);
4449
}
50+
finally {
51+
deflater.end();
52+
}
4553
}
4654

4755
public static String decompress(@NonNull String base64) throws Exception {
@@ -59,6 +67,9 @@ public static String decompress(@NonNull String base64) throws Exception {
5967

6068
return outputStream.toString();
6169
}
70+
finally {
71+
inflater.end();
72+
}
6273
}
6374

6475
static String encodeToBase64(byte[] bytes) {

0 commit comments

Comments
 (0)