diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java index 4a56e92a88796a..7c0d07dde5b06e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java @@ -30,7 +30,6 @@ import java.util.zip.GZIPOutputStream; import okhttp3.MediaType; import okhttp3.RequestBody; -import okhttp3.internal.Util; import okio.BufferedSink; import okio.ByteString; import okio.Okio; @@ -128,6 +127,21 @@ private static InputStream getDownloadFileInputStream(Context context, Uri uri) return RequestBody.create(mediaType, gzipByteArrayOutputStream.toByteArray()); } + /** + * Reference: + * https://github.com/square/okhttp/blob/8c8c3dbcfa91e28de2e13975ec414e07f153fde4/okhttp/src/commonMain/kotlin/okhttp3/internal/-UtilCommon.kt#L281-L288 + * Checked exceptions will be ignored + */ + private static void closeQuietly(Source source) { + try { + source.close(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + // noop. + } + } + /** Creates a RequestBody from a mediaType and inputStream given. */ public static RequestBody create(final MediaType mediaType, final InputStream inputStream) { return new RequestBody() { @@ -152,7 +166,7 @@ public void writeTo(BufferedSink sink) throws IOException { source = Okio.source(inputStream); sink.writeAll(source); } finally { - Util.closeQuietly(source); + closeQuietly(source); } } };