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();
}
};
}
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());
}
};
}
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();
}
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);
}
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());
}
}
Aggregations