static ResponseBody buffer(final ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
static ResponseBody buffer(final ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
/** like {@link #get(Request)} except doesn't throw unsupported on not found */ Response call(Request request) throws IOException { try (Response response = client.newCall(request).execute()) { if (!HttpHeaders.hasBody(response)) return response; // buffer response so tests can read it. Otherwise the finally block will drop it ResponseBody toReturn; try (ResponseBody body = response.body()) { Buffer buffer = new Buffer(); body.source().readAll(buffer); toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer); } return response.newBuilder().body(toReturn).build(); } }
if (outputStream != null) { sink = Okio.sink(outputStream); source.readAll(sink); } else { throw new NullPointerException("OutputStream for given output Uri is null");
try (HashingSource hashingSource = HashingSource.sha256(Okio.source(file)); BufferedSource source = Okio.buffer(hashingSource)) { source.readAll(Okio.blackhole()); System.out.println(" sha256: " + hashingSource.hash().hex());
private void exhaustStream(InputStream inputStream) throws IOException { Okio.buffer(Okio.source(inputStream)).readAll(Okio.blackhole()); }
@Test public void readAllExhausted() throws IOException { MockSink mockSink = new MockSink(); assertEquals(0, source.readAll(mockSink)); assertTrue(source.exhausted()); mockSink.assertLog(); }
@Test public void readAll() throws IOException { source.getBuffer().writeUtf8("abc"); sink.writeUtf8("def"); sink.emit(); Buffer sink = new Buffer(); assertEquals(6, source.readAll(sink)); assertEquals("abcdef", sink.readUtf8()); assertTrue(source.exhausted()); }
/** * We don't want readAll to buffer an unbounded amount of data. Instead it * should buffer a segment, write it, and repeat. */ @Test public void readAllReadsOneSegmentAtATime() 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(); BufferedSource bufferedSource = Okio.buffer((Source) source); assertEquals(SEGMENT_SIZE * 3, bufferedSource.readAll(mockSink)); mockSink.assertLog( "write(" + write1 + ", " + write1.size() + ")", "write(" + write2 + ", " + write2.size() + ")", "write(" + write3 + ", " + write3.size() + ")"); } }
@Test public void peekSegmentThenInvalid() throws IOException { sink.writeUtf8("abc"); sink.writeUtf8(repeat('d', 2 * SEGMENT_SIZE)); sink.emit(); assertEquals("abc", source.readUtf8(3)); // Peek a little data and skip the rest of the upstream source BufferedSource peek = source.peek(); assertEquals("ddd", peek.readUtf8(3)); source.readAll(Okio.blackhole()); // Skip the rest of the buffered data peek.skip(SEGMENT_SIZE - 3); try { peek.readByte(); fail(); } catch (IllegalStateException e) { assertEquals("Peek source is invalid because upstream source was used", e.getMessage()); } }
public static ResponseBody buffer(final ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
/** Buffers the response body, allowing to close the original source. */ static ResponseBody buffer(ResponseBody body) throws IOException { Buffer buffer = new Buffer(); body.source().readAll(buffer); return ResponseBody.create(body.contentType(), body.contentLength(), buffer); }
public static void copy(InputStream is, OutputStream out) { try { buffer(source(is)).readAll(sink(out)); } catch (IOException e) { throw new IllegalStateException(e); } }
private void downloadJavadoc(Artifact artifact, File destination) throws IOException { try (BufferedSource source = mavenCentral.downloadJavadocJar(artifact); ZipInputStream zipIn = new ZipInputStream(source.inputStream())) { for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null; ) { if (entry.isDirectory()) continue; File file = new File(destination + "/" + entry.getName()); if (!file.getParentFile().mkdirs() && !file.getParentFile().isDirectory()) { throw new IOException("failed to mkdirs for " + file); } try (Sink out = Okio.sink(file)) { Okio.buffer(Okio.source(zipIn)).readAll(out); } } } }
/** like {@link #get(Request)} except doesn't throw unsupported on not found */ Response call(Request request) throws IOException { try (Response response = client.newCall(request).execute()) { if (!HttpHeaders.hasBody(response)) return response; // buffer response so tests can read it. Otherwise the finally block will drop it ResponseBody toReturn; try (ResponseBody body = response.body()) { Buffer buffer = new Buffer(); body.source().readAll(buffer); toReturn = ResponseBody.create(body.contentType(), body.contentLength(), buffer); } return response.newBuilder().body(toReturn).build(); } }
/** * Download the resource at the URL with the headers and write it to the file. * * @return Response whose body has already been consumed and closed */ public Response download(String url, File destination, String... headers) throws IOException { try (Response resp = response(url, headers)) { if (resp.isSuccessful()) { try (BufferedSink sink = Okio.buffer(Okio.sink(destination))) { resp.body().source().readAll(sink); } } return resp; } } }
/** * Download the resource at the URL with the headers and write it to the file. * * @return Response whose body has already been consumed and closed */ public Response download(String url, File destination, String... headers) throws IOException { try (Response resp = response(url, headers)) { if (resp.isSuccessful()) { try (BufferedSink sink = Okio.buffer(Okio.sink(destination))) { resp.body().source().readAll(sink); } } return resp; } } }
source.timeout().timeout(30, TimeUnit.SECONDS); source.timeout().deadline(5, TimeUnit.MINUTES); source.readAll(buffer); } catch (IOException e) { throw new IOException("Failed to execute " + Arrays.toString(command) + ":\n"
ImageInputStream iis = ImageIO.createImageInputStream(source.inputStream());) { so.write(sink.outputStream()); source.readAll(sink); final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) {
ImageInputStream iis = ImageIO.createImageInputStream(source.inputStream());) { so.write(sink.outputStream()); source.readAll(sink); final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) {