Skip to content

Commit a2b9104

Browse files
authored
Removing try-with-resources (#1458)
* Dropping guava and appcompat dependency from FIS SDK. * Removing try-with-resources * Fix for Rayo's comments
1 parent ea1caf5 commit a2b9104

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ private void preConditionChecks() {
148148
Preconditions.checkNotEmpty(getApiKey());
149149
Preconditions.checkArgument(
150150
Utils.isValidAppIdFormat(getApplicationId()),
151-
"Please set your Application ID. A valid Firebase App ID is required to communicate "
152-
+ "with Firebase server APIs: It identifies your application with Firebase."
153-
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
151+
"Please set your Application ID. A valid Firebase App ID is required to communicate "
152+
+ "with Firebase server APIs: It identifies your application with Firebase."
153+
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
154154
Preconditions.checkArgument(
155155
Utils.isValidApiKeyFormat(getApiKey()),
156-
"Please set a valid API key. A Firebase API key is required to communicate with "
157-
+ "Firebase server APIs: It authenticates your project with Google."
158-
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
156+
"Please set a valid API key. A Firebase API key is required to communicate with "
157+
+ "Firebase server APIs: It authenticates your project with Google."
158+
+ "Please refer to https://firebase.google.com/support/privacy/init-options.");
159159
}
160160

161161
/** Returns the Project Id or Project Number for the Firebase Project. */

firebase-installations/src/main/java/com/google/firebase/installations/Utils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
package com.google.firebase.installations;
1616

1717
import android.text.TextUtils;
18-
1918
import androidx.annotation.Nullable;
20-
2119
import com.google.firebase.installations.local.PersistedInstallationEntry;
2220
import java.util.concurrent.TimeUnit;
2321
import java.util.regex.Pattern;

firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,19 @@ private static byte[] getJsonBytes(JSONObject jsonObject) throws IOException {
205205

206206
private static void writeRequestBodyToOutputStream(URLConnection urlConnection, byte[] jsonBytes)
207207
throws IOException {
208-
try (OutputStream outputStream = urlConnection.getOutputStream()) {
209-
if (outputStream == null) {
210-
throw new IOException("Cannot send request to FIS servers. No OutputStream available.");
211-
}
212-
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) {
213-
gzipOutputStream.write(jsonBytes);
208+
OutputStream outputStream = urlConnection.getOutputStream();
209+
if (outputStream == null) {
210+
throw new IOException("Cannot send request to FIS servers. No OutputStream available.");
211+
}
212+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
213+
try {
214+
gzipOutputStream.write(jsonBytes);
215+
} finally {
216+
try {
217+
gzipOutputStream.close();
218+
outputStream.close();
219+
} catch (IOException ignored) {
220+
214221
}
215222
}
216223
}
@@ -525,8 +532,12 @@ private static void logFisCommunicationError(HttpURLConnection conn) {
525532
// Read the error message from the response.
526533
@Nullable
527534
private static String readErrorResponse(HttpURLConnection conn) {
528-
try (BufferedReader reader =
529-
new BufferedReader(new InputStreamReader(conn.getErrorStream(), UTF_8))) {
535+
InputStream errorStream = conn.getErrorStream();
536+
if (errorStream == null) {
537+
return null;
538+
}
539+
BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream, UTF_8));
540+
try {
530541
StringBuilder response = new StringBuilder();
531542
for (String input = reader.readLine(); input != null; input = reader.readLine()) {
532543
response.append(input).append('\n');
@@ -536,6 +547,12 @@ private static String readErrorResponse(HttpURLConnection conn) {
536547
conn.getResponseCode(), conn.getResponseMessage(), response);
537548
} catch (IOException ignored) {
538549
return null;
550+
} finally {
551+
try {
552+
reader.close();
553+
} catch (IOException ignored) {
554+
555+
}
539556
}
540557
}
541558
}

firebase-installations/src/test/java/com/google/firebase/installations/FirebaseInstallationsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,8 @@ public void testApiKeyCheck() {
10011001
assertFalse(Utils.isValidApiKeyFormat("AIzaSyabcdefghijklmno qrstuvwxyzabcdefg")); // wrong char
10021002
assertFalse(
10031003
Utils.isValidApiKeyFormat(
1004-
"AAAAdpB7anM:APA91bFFK03DIT8y3l5uymwbKcUDJdYqTRSP9Qcxg8SU5kKPalEpObdx0C0xv8gQttdWlL"
1005-
+ "W4hLvvHA0JoDKA6Lrvbi-edUjFCPY_WJkuvHxFwGWXjnj4yI4sPQ27mXuSVIyAbgX4aTK0QY"
1006-
+ "pIKq2j1NBi7ZU75gunQg")); // using FCM server key as API key.
1004+
"AAAAdpB7anM:APA91bFFK03DIT8y3l5uymwbKcUDJdYqTRSP9Qcxg8SU5kKPalEpObdx0C0xv8gQttdWlL"
1005+
+ "W4hLvvHA0JoDKA6Lrvbi-edUjFCPY_WJkuvHxFwGWXjnj4yI4sPQ27mXuSVIyAbgX4aTK0QY"
1006+
+ "pIKq2j1NBi7ZU75gunQg")); // using FCM server key as API key.
10071007
}
10081008
}

0 commit comments

Comments
 (0)