Android. OkHttp crash fixed on upload re-try. (#20802)

Summary:
This change fixes this issue:
https://github.com/facebook/react-native/issues/10423
Pull Request resolved: https://github.com/facebook/react-native/pull/20802

Differential Revision: D9478422

Pulled By: hramos

fbshipit-source-id: ce3a098a71c8f50a62b011a2a277004ab7a7b655
This commit is contained in:
Sergei Dryganets 2018-08-22 23:00:07 -07:00 committed by Facebook Github Bot
parent 43cd6f7579
commit 79fe925f1d
1 changed files with 6 additions and 6 deletions

View File

@ -19,7 +19,6 @@ public class ProgressRequestBody extends RequestBody {
private final RequestBody mRequestBody;
private final ProgressListener mProgressListener;
private BufferedSink mBufferedSink;
private long mContentLength = 0L;
public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) {
@ -42,16 +41,17 @@ public class ProgressRequestBody extends RequestBody {
@Override
public void writeTo(BufferedSink sink) throws IOException {
if (mBufferedSink == null) {
mBufferedSink = Okio.buffer(outputStreamSink(sink));
}
// In 99% of cases, this method is called strictly once.
// The only case when it is called more than once is internal okhttp upload re-try.
// We need to re-create CountingOutputStream in this case as progress should be re-evaluated.
BufferedSink sinkWrapper = Okio.buffer(outputStreamSink(sink));
// contentLength changes for input streams, since we're using inputStream.available(),
// so get the length before writing to the sink
contentLength();
mRequestBody.writeTo(mBufferedSink);
mBufferedSink.flush();
mRequestBody.writeTo(sinkWrapper);
sinkWrapper.flush();
}
private Sink outputStreamSink(BufferedSink sink) {