-
Notifications
You must be signed in to change notification settings - Fork 397
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
Showing
7 changed files
with
382 additions
and
0 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
83 changes: 83 additions & 0 deletions
83
mvp/src/main/java/cn/droidlover/xdroidmvp/net/progress/ProRequestBody.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,83 @@ | ||
package cn.droidlover.xdroidmvp.net.progress; | ||
|
||
import java.io.IOException; | ||
import java.lang.ref.WeakReference; | ||
import java.util.Set; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.Buffer; | ||
import okio.BufferedSink; | ||
import okio.ForwardingSink; | ||
import okio.Okio; | ||
|
||
/** | ||
* Created by wanglei on 2017/9/10. | ||
*/ | ||
|
||
public class ProRequestBody extends RequestBody { | ||
private RequestBody delegate; | ||
private BufferedSink bufferedSink; | ||
private Set<WeakReference<ProgressListener>> listeners; | ||
|
||
public ProRequestBody(RequestBody delegate, Set<WeakReference<ProgressListener>> listeners) { | ||
this.delegate = delegate; | ||
this.listeners = listeners; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return delegate.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
try { | ||
return delegate.contentLength(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return -1; | ||
} | ||
|
||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
if (bufferedSink == null) { | ||
bufferedSink = Okio.buffer(new ProgressSink(sink)); | ||
} | ||
try { | ||
delegate.writeTo(bufferedSink); | ||
bufferedSink.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
ProgressHelper.dispatchErrorEvent(listeners, e); | ||
throw e; | ||
} | ||
} | ||
|
||
final class ProgressSink extends ForwardingSink { | ||
private long soFarBytes = 0; | ||
private long totalBytes = -1; | ||
|
||
public ProgressSink(okio.Sink delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) { | ||
try { | ||
super.write(source, byteCount); | ||
} catch (Exception e) { | ||
ProgressHelper.dispatchErrorEvent(listeners, e); | ||
} | ||
|
||
if (totalBytes < 0) { | ||
totalBytes = contentLength(); | ||
} | ||
soFarBytes += byteCount; | ||
|
||
ProgressHelper.dispatchProgressEvent(listeners, soFarBytes, totalBytes); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
mvp/src/main/java/cn/droidlover/xdroidmvp/net/progress/ProResponseBody.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,76 @@ | ||
package cn.droidlover.xdroidmvp.net.progress; | ||
|
||
import java.io.IOException; | ||
import java.lang.ref.WeakReference; | ||
import java.util.Set; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.ResponseBody; | ||
import okio.Buffer; | ||
import okio.BufferedSource; | ||
import okio.ForwardingSource; | ||
import okio.Okio; | ||
import okio.Source; | ||
|
||
/** | ||
* Created by wanglei on 2017/9/10. | ||
*/ | ||
|
||
public class ProResponseBody extends ResponseBody { | ||
private ResponseBody delegate; | ||
private BufferedSource bufferedSource; | ||
private Set<WeakReference<ProgressListener>> listeners; | ||
|
||
public ProResponseBody(ResponseBody delegate, Set<WeakReference<ProgressListener>> listeners) { | ||
this.delegate = delegate; | ||
this.listeners = listeners; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return delegate.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() { | ||
return delegate.contentLength(); | ||
} | ||
|
||
@Override | ||
public BufferedSource source() { | ||
if (bufferedSource == null) { | ||
bufferedSource = Okio.buffer(new ProgressSource(delegate.source())); | ||
} | ||
return bufferedSource; | ||
} | ||
|
||
|
||
final class ProgressSource extends ForwardingSource { | ||
private long soFarBytes = 0; | ||
private long totalBytes = -1; | ||
|
||
public ProgressSource(Source delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public long read(Buffer sink, long byteCount) throws IOException { | ||
long bytesRead = 0L; | ||
try { | ||
bytesRead = super.read(sink, byteCount); | ||
|
||
if (totalBytes < 0) { | ||
totalBytes = contentLength(); | ||
} | ||
soFarBytes += (bytesRead != -1 ? bytesRead : 0); | ||
|
||
ProgressHelper.dispatchProgressEvent(listeners, soFarBytes, totalBytes); | ||
} catch (IOException e) { | ||
ProgressHelper.dispatchErrorEvent(listeners, e); | ||
throw e; | ||
} | ||
|
||
return bytesRead; | ||
} | ||
} | ||
} |
Oops, something went wrong.