Skip to content

Commit

Permalink
Remove okhttp internal util usage (#37843)
Browse files Browse the repository at this point in the history
Summary:
Remove okhttp internal `Util` usage so it can be compatible with [okhttp 5.0.0 alpha](https://square.github.io/okhttp/changelogs/changelog/#version-500-alpha7).

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[ANDROID] [CHANGED] - Remove okhttp3 internal util usage

Pull Request resolved: #37843

Test Plan: None, the implementation is based on okhttp internal util

Reviewed By: NickGerleman

Differential Revision: D46764363

Pulled By: cortinico

fbshipit-source-id: e46770625f19057fa7374be15e92903d7966f880
  • Loading branch information
adrianha authored and facebook-github-bot committed Jun 15, 2023
1 parent b40bc79 commit 3e30326
Showing 1 changed file with 16 additions and 2 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -152,7 +166,7 @@ public void writeTo(BufferedSink sink) throws IOException {
source = Okio.source(inputStream);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
closeQuietly(source);
}
}
};
Expand Down

0 comments on commit 3e30326

Please sign in to comment.