From 37aecfd230bde38d31dd4497e0fdba844738e098 Mon Sep 17 00:00:00 2001 From: Angel Leon Date: Thu, 5 Dec 2024 13:01:46 -0700 Subject: [PATCH] fix: avoid android.os.strictmode.UnbufferedIoViolation by using BufferedInputStream - Wrap InputStream with BufferedInputStream to ensure buffered I/O and prevent UnbufferedIoViolation in Android 14 (SDK 34). - Use try-with-resources to automatically close streams, improving resource management. - Retain existing functionality for converting InputStream to a UTF-8 string. This change ensures compliance with Android 14's stricter I/O policies. --- .../common/SessionReportingCoordinator.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinator.java b/firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinator.java index 29e2301591a..61bce5b555b 100644 --- a/firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinator.java +++ b/firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinator.java @@ -419,13 +419,15 @@ private static CrashlyticsReport.ApplicationExitInfo convertApplicationExitInfo( @VisibleForTesting @RequiresApi(api = Build.VERSION_CODES.KITKAT) public static String convertInputStreamToString(InputStream inputStream) throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; - int length; - while ((length = inputStream.read(bytes)) != -1) { - byteArrayOutputStream.write(bytes, 0, length); + try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { + byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; + int length; + while ((length = bufferedInputStream.read(bytes)) != -1) { + byteArrayOutputStream.write(bytes, 0, length); + } + return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name()); } - return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name()); } /** Finds the first ANR ApplicationExitInfo within the session. */