Permalink
Browse files

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

Summary:
This change fixes this issue:
#10423
Pull Request resolved: #20802

Differential Revision: D9478422

Pulled By: hramos

fbshipit-source-id: ce3a098a71c8f50a62b011a2a277004ab7a7b655
  • Loading branch information...
dryganets authored and kelset committed Aug 23, 2018
1 parent 9a4c21f commit 7a246e4872ec29a521216877ae5f5d26b5384a38
@@ -19,7 +19,6 @@
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 long contentLength() throws IOException {
@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) {

0 comments on commit 7a246e4

Please sign in to comment.