use of okio.ForwardingSink in project HttpApiDemo by xiaoyunchengzhu.
the class ProgressRequestBody method writeTo.
@Override
public void writeTo(BufferedSink sink) throws IOException {
ForwardingSink forwardingSink = new ForwardingSink(sink) {
long currentlength = 0;
@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
if (longth == 0) {
longth = contentLength();
}
currentlength += byteCount;
LogManger.i("currentlenth--------" + currentlength);
progressBodyListenner.onProgress(currentlength, longth, (double) currentlength / longth);
}
};
BufferedSink bufferedSink = Okio.buffer(forwardingSink);
requestBody.writeTo(bufferedSink);
forwardingSink.flush();
bufferedSink.flush();
}
use of okio.ForwardingSink in project OkHttp3 by MrZhousf.
the class ProgressRequestBody method sink.
private Sink sink(Sink originalSink) {
return new ForwardingSink(originalSink) {
long bytesWritten = 0L;
long contentLength = 0L;
int lastPercent = 0;
@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
if (contentLength == 0) {
contentLength = contentLength();
}
bytesWritten += byteCount;
if (null != progressCallback) {
int percent = (int) ((100 * bytesWritten) / contentLength);
// 每处理1%则立即回调
if (percent != lastPercent) {
lastPercent = percent;
progressCallback.onProgressAsync(percent, bytesWritten, contentLength, bytesWritten == contentLength);
// 主线程回调
Message msg = new ProgressMessage(OkMainHandler.PROGRESS_CALLBACK, progressCallback, percent, bytesWritten, contentLength, bytesWritten == contentLength, requestTag).build();
OkMainHandler.getInstance().sendMessage(msg);
}
}
}
};
}
use of okio.ForwardingSink in project okhttp by square.
the class InMemoryFileSystem method sink.
private Sink sink(File file, boolean appending) {
Buffer result = null;
if (appending) {
result = files.get(file);
}
if (result == null) {
result = new Buffer();
}
files.put(file, result);
final Sink sink = result;
openSinks.put(sink, file);
return new ForwardingSink(sink) {
@Override
public void close() throws IOException {
openSinks.remove(sink);
super.close();
}
};
}
Aggregations