-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alladin El Akhrass
committed
Dec 18, 2015
1 parent
b8fb6df
commit c1bf57b
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/alelak/backblazeb2/listeners/ProgressRequestBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.alelak.backblazeb2.listeners; | ||
|
||
import com.squareup.okhttp.MediaType; | ||
import com.squareup.okhttp.RequestBody; | ||
import okio.*; | ||
|
||
import java.io.IOException; | ||
|
||
public class ProgressRequestBody extends RequestBody { | ||
|
||
private final RequestBody requestBody; | ||
private final ProgressListener progressListener; | ||
private BufferedSink bufferedSink; | ||
|
||
public ProgressRequestBody(RequestBody requestBody, ProgressListener progressListener) { | ||
this.requestBody = requestBody; | ||
this.progressListener = progressListener; | ||
} | ||
|
||
@Override | ||
public long contentLength() throws IOException { | ||
return requestBody.contentLength(); | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return requestBody.contentType(); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
if (bufferedSink == null) { | ||
bufferedSink = Okio.buffer(sink(sink)); | ||
} | ||
requestBody.writeTo(bufferedSink); | ||
bufferedSink.flush(); | ||
|
||
} | ||
|
||
private Sink sink(Sink sink) { | ||
return new ForwardingSink(sink) { | ||
long bytesWritten = 0L; | ||
long contentLength = 0L; | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) throws IOException { | ||
super.write(source, byteCount); | ||
if (contentLength == 0) { | ||
contentLength = contentLength(); | ||
} | ||
bytesWritten += byteCount; | ||
if (progressListener != null) { | ||
progressListener.onProgress(bytesWritten, contentLength, bytesWritten == contentLength); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public interface ProgressListener { | ||
void onProgress(long bytesWritten, long contentLength, boolean done); | ||
} | ||
|
||
} |