@Override public void writeTo(BufferedSink sink) throws IOException { try (Source source = Okio.source(file)) { sink.writeAll(source); } } };
@Override public void writeTo(BufferedSink sink) throws IOException { sink.writeAll(pipe.source()); } }
@Override public void onResponse(Call call, Response response) throws IOException { File downloadedFile = new File(context.getCacheDir(), filename); BufferedSink sink = Okio.buffer(Okio.sink(downloadedFile)); sink.writeAll(response.body().source()); sink.close(); }
@Override public void writeTo(BufferedSink sink) throws IOException { try (Source source = Okio.source(file)) { sink.writeAll(source); } } };
private File download(String url) throws IOException { File output=new File(getFilesDir(), UPDATE_FILENAME); if (output.exists()) { output.delete(); } OkHttpClient client=new OkHttpClient(); Request request=new Request.Builder().url(url).build(); Response response=client.newCall(request).execute(); BufferedSink sink=Okio.buffer(Okio.sink(output)); sink.writeAll(response.body().source()); sink.close(); return(output); } }
protected RequestBody toRequestBody(InputStream body, @Nullable String mediaType) { return new RequestBody() { @Nullable @Override public MediaType contentType() { if (mediaType == null) { return null; } return MediaType.parse(mediaType); } @Override public void writeTo(BufferedSink sink) throws IOException { try (Source source = Okio.source(body)) { sink.writeAll(source); } } }; }
@Override public JsonWriter value(BufferedSource source) throws IOException { if (promoteValueToName) { throw new IllegalStateException( "BufferedSource cannot be used as a map key in JSON at path " + getPath()); } writeDeferredName(); beforeValue(); sink.writeAll(source); pathIndices[stackSize - 1]++; return this; }
@Override @SuppressWarnings("NullableProblems") public void writeTo(BufferedSink sink) throws IOException { Source source = null; try { source = Okio.source(stream); sink.writeAll(source); } finally { Util.closeQuietly(source); } } }
@Override public void onResponse(Call call, Response response) { try { BufferedSink sink = Okio.buffer(Okio.sink(outputFile)); sink.writeAll(response.body().source()); sink.close(); mMainHandler.post(() -> callback.onSuccess(outputFile.getPath())); } catch (IOException e) { this.onFailure(call, e); } } });
BufferedSink sink=Okio.buffer(Okio.sink(new File(output.getPath()))); sink.writeAll(response.body().source()); sink.close();
BufferedSink sink=Okio.buffer(Okio.sink(new File(output.getPath()))); sink.writeAll(response.body().source()); sink.close();
static File copyTestFile(String file) throws IOException { File newFile = File.createTempFile(file, "test"); InputStream in = QueueTestUtils.class.getResourceAsStream(file); try (BufferedSink sink = Okio.buffer(Okio.sink(newFile))) { sink.writeAll(Okio.source(in)); } assertTrue(newFile.exists()); return newFile; }
BufferedSink sink = Okio.buffer(hashingSink); Source source = Okio.source(file)) { sink.writeAll(source);
@Override public void onHandleWork(@NonNull 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()); } }
@Test public void writeAllExhausted() throws Exception { Buffer source = new Buffer(); assertEquals(0, sink.writeAll(source)); assertEquals(0, source.size()); }
@Test public void writeAllExhausted() throws IOException { MockSink mockSink = new MockSink(); BufferedSink bufferedSink = Okio.buffer(mockSink); assertEquals(0, bufferedSink.writeAll(new Buffer())); assertEquals(0, bufferedSink.getBuffer().size()); mockSink.assertLog(); // No writes. }
@Test public void writeAll() throws Exception { Buffer source = new Buffer().writeUtf8("abcdef"); assertEquals(6, sink.writeAll(source)); assertEquals(0, source.size()); sink.flush(); assertEquals("abcdef", data.readUtf8()); }
@Test public void writeAllWritesOneSegmentAtATime() throws IOException { Buffer write1 = new Buffer().writeUtf8(TestUtil.repeat('a', SEGMENT_SIZE)); Buffer write2 = new Buffer().writeUtf8(TestUtil.repeat('b', SEGMENT_SIZE)); Buffer write3 = new Buffer().writeUtf8(TestUtil.repeat('c', SEGMENT_SIZE)); Buffer source = new Buffer().writeUtf8("" + TestUtil.repeat('a', SEGMENT_SIZE) + TestUtil.repeat('b', SEGMENT_SIZE) + TestUtil.repeat('c', SEGMENT_SIZE)); MockSink mockSink = new MockSink(); BufferedSink bufferedSink = Okio.buffer(mockSink); assertEquals(SEGMENT_SIZE * 3, bufferedSink.writeAll(source)); mockSink.assertLog( "write(" + write1 + ", " + write1.size() + ")", "write(" + write2 + ", " + write2.size() + ")", "write(" + write3 + ", " + write3.size() + ")"); } }
@Test public void writeAll() throws IOException { MockSink mockSink = new MockSink(); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.getBuffer().writeUtf8("abc"); assertEquals(3, bufferedSink.writeAll(new Buffer().writeUtf8("def"))); assertEquals(6, bufferedSink.getBuffer().size()); assertEquals("abcdef", bufferedSink.getBuffer().readUtf8(6)); mockSink.assertLog(); // No writes. }
@Override public void writeTo(BufferedSink sink) throws IOException { try (Source source = Okio.source(stream)) { sink.writeAll(source); } } };