Search in sources :

Example 1 with ForwardingSink

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();
}
Also used : Buffer(okio.Buffer) ForwardingSink(okio.ForwardingSink) BufferedSink(okio.BufferedSink)

Example 2 with ForwardingSink

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);
                }
            }
        }
    };
}
Also used : Buffer(okio.Buffer) ProgressMessage(com.okhttplib.bean.ProgressMessage) Message(android.os.Message) ProgressMessage(com.okhttplib.bean.ProgressMessage) ForwardingSink(okio.ForwardingSink)

Example 3 with ForwardingSink

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();
        }
    };
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) ForwardingSink(okio.ForwardingSink) ForwardingSink(okio.ForwardingSink)

Aggregations

Buffer (okio.Buffer)3 ForwardingSink (okio.ForwardingSink)3 Message (android.os.Message)1 ProgressMessage (com.okhttplib.bean.ProgressMessage)1 BufferedSink (okio.BufferedSink)1 Sink (okio.Sink)1