Search in sources :

Example 36 with BufferedSink

use of okio.BufferedSink in project FlareBot by FlareBot.

the class GzipRequestInterceptor method gzip.

private RequestBody gzip(final RequestBody body) {
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return body.contentType();
        }

        @Override
        public long contentLength() {
            // We don't know the compressed length in advance!
            return -1;
        }

        @Override
        @ParametersAreNonnullByDefault
        public void writeTo(BufferedSink sink) throws IOException {
            BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
            body.writeTo(gzipSink);
            gzipSink.close();
        }
    };
}
Also used : GzipSink(okio.GzipSink) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody)

Example 37 with BufferedSink

use of okio.BufferedSink in project java by kubernetes-client.

the class GzipRequestInterceptor method forceContentLength.

private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
    final Buffer buffer = new Buffer();
    requestBody.writeTo(buffer);
    return new RequestBody() {

        @Override
        public MediaType contentType() {
            return requestBody.contentType();
        }

        @Override
        public long contentLength() {
            return buffer.size();
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(buffer.snapshot());
        }
    };
}
Also used : Buffer(okio.Buffer) BufferedSink(okio.BufferedSink)

Example 38 with BufferedSink

use of okio.BufferedSink in project okhttp-OkGo by jeasonlzy.

the class ProgressRequestBody method writeTo.

/** 重写进行写入 */
@Override
public void writeTo(BufferedSink sink) throws IOException {
    countingSink = new CountingSink(sink);
    BufferedSink bufferedSink = Okio.buffer(countingSink);
    delegate.writeTo(bufferedSink);
    //必须调用flush,否则最后一部分数据可能不会被写入
    bufferedSink.flush();
}
Also used : BufferedSink(okio.BufferedSink)

Example 39 with BufferedSink

use of okio.BufferedSink in project materialistic by hidroh.

the class FavoriteManager method toFile.

@WorkerThread
private Uri toFile(Context context, Cursor cursor) throws IOException {
    if (cursor.getCount() == 0) {
        return null;
    }
    File dir = new File(context.getFilesDir(), PATH_SAVED);
    if (!dir.exists() && !dir.mkdir()) {
        return null;
    }
    File file = new File(dir, FILENAME_EXPORT);
    if (!file.exists() && !file.createNewFile()) {
        return null;
    }
    BufferedSink bufferedSink = Okio.buffer(Okio.sink(file));
    do {
        Favorite item = cursor.getFavorite();
        bufferedSink.writeUtf8(item.getDisplayedTitle()).writeByte('\n').writeUtf8(item.getUrl()).writeByte('\n').writeUtf8(String.format(HackerNewsClient.WEB_ITEM_PATH, item.getId()));
        if (!cursor.isLast()) {
            bufferedSink.writeByte('\n').writeByte('\n');
        }
    } while (cursor.moveToNext());
    Util.closeQuietly(bufferedSink);
    return getUriForFile(context, file);
}
Also used : BufferedSink(okio.BufferedSink) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 40 with BufferedSink

use of okio.BufferedSink in project cw-omnibus by commonsguy.

the class RestoreService method onHandleIntent.

@Override
protected void onHandleIntent(Intent i) {
    Request request = new Request.Builder().url(i.getData().toString()).build();
    try {
        Response response = BackupService.OKHTTP_CLIENT.newCall(request).execute();
        File toRestore = new File(getCacheDir(), "backup.zip");
        if (toRestore.exists()) {
            toRestore.delete();
        }
        BufferedSink sink = Okio.buffer(Okio.sink(toRestore));
        sink.writeAll(response.body().source());
        sink.close();
        ZipUtils.unzip(toRestore, getFilesDir(), BackupService.ZIP_PREFIX_FILES);
        ZipUtils.unzip(toRestore, BackupService.getSharedPrefsDir(this), BackupService.ZIP_PREFIX_PREFS);
        ZipUtils.unzip(toRestore, getExternalFilesDir(null), BackupService.ZIP_PREFIX_EXTERNAL);
        EventBus.getDefault().post(new RestoreCompletedEvent());
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Exception restoring backup", e);
        EventBus.getDefault().post(new RestoreFailedEvent());
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) BufferedSink(okio.BufferedSink) File(java.io.File)

Aggregations

BufferedSink (okio.BufferedSink)91 Test (org.junit.Test)31 IOException (java.io.IOException)30 Buffer (okio.Buffer)19 File (java.io.File)15 RequestBody (okhttp3.RequestBody)14 GzipSink (okio.GzipSink)12 Source (okio.Source)11 Response (okhttp3.Response)10 BufferedSource (okio.BufferedSource)10 Request (okhttp3.Request)9 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)9 MediaType (okhttp3.MediaType)7 InterruptedIOException (java.io.InterruptedIOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 Sink (okio.Sink)5 InputStream (java.io.InputStream)4 Socket (java.net.Socket)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (com.squareup.okhttp.Request)3