Skip to content

Commit

Permalink
Android. OkHttp crash fixed on upload re-try. (facebook#20802)
Browse files Browse the repository at this point in the history
Summary:
This change fixes this issue:
facebook#10423
Pull Request resolved: facebook#20802

Differential Revision: D9478422

Pulled By: hramos

fbshipit-source-id: ce3a098a71c8f50a62b011a2a277004ab7a7b655
  • Loading branch information
dryganets authored and aleclarson committed Sep 16, 2018
1 parent 08e07ee commit 195274d
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 195274d

Please sign in to comment.