Skip to content

Commit

Permalink
Android. OkHttp crash fixed on upload re-try. (#20802)
Browse files Browse the repository at this point in the history
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 facebook-github-bot committed Aug 23, 2018
1 parent 43cd6f7 commit 79fe925
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 79fe925

Please sign in to comment.