aboutsummaryrefslogtreecommitdiff
path: root/java/com/android
diff options
context:
space:
mode:
authorMeng Wang <mewan@google.com>2021-03-09 13:11:54 -0800
committerMeng Wang <mewan@google.com>2021-03-09 14:18:01 -0800
commit0930a0c7630dea225e846219bf746a49c079068a (patch)
tree46b74efb1d61212adc623e4a9da3bc073b172c27 /java/com/android
parent7683e077e4d612988bff5c928455cbe2289ca190 (diff)
downloadservice_entitlement-0930a0c7630dea225e846219bf746a49c079068a.tar.gz
Add null check to StreamUtils.inputStreamToStringSafe
HttpUrlConnecion.getInputStream/getErrorStream could return null on connection issues. Bug: 182298836 Test: atest Change-Id: Id3bda6e79b62847710be223356cb51bd776a1069
Diffstat (limited to 'java/com/android')
-rw-r--r--java/com/android/libraries/entitlement/utils/StreamUtils.java29
1 files changed, 14 insertions, 15 deletions
diff --git a/java/com/android/libraries/entitlement/utils/StreamUtils.java b/java/com/android/libraries/entitlement/utils/StreamUtils.java
index e2ba890..1e3ce7a 100644
--- a/java/com/android/libraries/entitlement/utils/StreamUtils.java
+++ b/java/com/android/libraries/entitlement/utils/StreamUtils.java
@@ -16,25 +16,26 @@
package com.android.libraries.entitlement.utils;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import androidx.annotation.Nullable;
+
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-/**
- * Utility methods about InputStream.
- */
+/** Utility methods about {@link InputStream}. */
public final class StreamUtils {
private static final int BUFFER_SIZE = 1024;
- private StreamUtils() {
- }
+ private StreamUtils() {}
- /**
- * Reads an {@link InputStream} into a string.
- */
- public static String inputStreamToString(InputStream inputStream) throws IOException {
+ /** Reads an {@link InputStream} into a UTF-8 string. */
+ public static String inputStreamToString(@Nullable InputStream inputStream) throws IOException {
+ if (inputStream == null) {
+ throw new IOException("inputStream is null");
+ }
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream result = new ByteArrayOutputStream()) {
byte[] buffer = new byte[BUFFER_SIZE];
@@ -42,14 +43,12 @@ public final class StreamUtils {
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
- return result.toString(StandardCharsets.UTF_8.name());
+ return result.toString(UTF_8.name());
}
}
- /**
- * Reads an {@link InputStream} into a string. Returns an empty string if any error.
- */
- public static String inputStreamToStringSafe(InputStream inputStream) {
+ /** Reads an {@link InputStream} into a UTF-8 string. Returns an empty string if any error. */
+ public static String inputStreamToStringSafe(@Nullable InputStream inputStream) {
try {
return inputStreamToString(inputStream);
} catch (IOException e) {