public BufferedSink createSink() { return Okio.buffer(pipe.sink()); }
StreamedRequestBody(long expectedContentLength) { initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength); }
@Override public void run() { try { pipe.sink().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS);
@Override public void run() { try { Thread.sleep(1000L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); Thread.sleep(1000L); pipe.sink().write(new Buffer().writeUtf8("def"), 3); Thread.sleep(1000L); pipe.sink().write(new Buffer().writeUtf8("ghi"), 3); Thread.sleep(1000L); pipe.sink().write(new Buffer().writeUtf8("jkl"), 3); } catch (IOException | InterruptedException e) { throw new AssertionError(); } } });
@Test public void sinkClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().close(); try { pipe.sink().write(new Buffer().writeUtf8("abc"), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } try { pipe.sink().flush(); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } }
@Test public void sinkFlushFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().flush(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Override public ByteString call() throws Exception { HashingSink hashingSink = HashingSink.sha1(pipe.sink()); Random random = new Random(0); byte[] data = new byte[8192]; Buffer buffer = new Buffer(); for (long i = 0L; i < totalBytes; i += data.length) { random.nextBytes(data); buffer.write(data); hashingSink.write(buffer, buffer.size()); } hashingSink.close(); return hashingSink.hash(); } });
@Test public void sinkCloseFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().close(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Test public void sinkCloseDoesntWaitForSourceRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().close(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8()); assertTrue(bufferedSource.exhausted()); }
@Test public void test() throws Exception { Pipe pipe = new Pipe(6); pipe.sink().write(new Buffer().writeUtf8("abc"), 3L); Source source = pipe.source(); Buffer readBuffer = new Buffer(); assertEquals(3L, source.read(readBuffer, 6L)); assertEquals("abc", readBuffer.readUtf8()); pipe.sink().close(); assertEquals(-1L, source.read(readBuffer, 6L)); source.close(); }
@Test public void sinkWriteFailsByClosedReader() throws Exception { final Pipe pipe = new Pipe(3L); executorService.schedule(new Runnable() { @Override public void run() { try { pipe.source().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS); double start = now(); try { pipe.sink().write(new Buffer().writeUtf8("abcdef"), 6); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); assertElapsed(1000.0, start); } }
@Test public void sinkTimeout() throws Exception { Pipe pipe = new Pipe(3); pipe.sink().timeout().timeout(1000, TimeUnit.MILLISECONDS); pipe.sink().write(new Buffer().writeUtf8("abc"), 3L); double start = now(); try { pipe.sink().write(new Buffer().writeUtf8("def"), 3L); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); Buffer readBuffer = new Buffer(); assertEquals(3L, pipe.source().read(readBuffer, 6L)); assertEquals("abc", readBuffer.readUtf8()); }
pipe.sink().write(new Buffer().writeUtf8("abcdefghijkl"), 12); assertElapsed(3000.0, start);
@Test public void gzipSink() throws Exception { Pipe pipe = new Pipe(1024 * 1024); GzipSink gzipSink = new GzipSink(pipe.sink()); // Disable compression to speed up a slow test. Improved from 141s to 35s on one machine. gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream()); readAllAndClose(Okio.source(gzipIn), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
@Test public void test() throws Exception { Pipe pipe = new Pipe(1024 * 1024); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), pipe.sink()); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); readAllAndClose(pipe.source(), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
@Test public void gzipSource() throws Exception { Pipe pipe = new Pipe(1024 * 1024); OutputStream gzipOut = new GZIPOutputStream(Okio.buffer(pipe.sink()).outputStream()) { { // Disable compression to speed up a slow test. Improved from 141s to 33s on one machine. def.setLevel(Deflater.NO_COMPRESSION); } }; Future<Long> future = readAllAndCloseAsync( randomSource(FOUR_GIB_PLUS_ONE), Okio.sink(gzipOut)); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GzipSource gzipSource = new GzipSource(pipe.source()); readAllAndClose(gzipSource, hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
StreamedRequestBody(long expectedContentLength) { initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength); }
StreamedRequestBody(long expectedContentLength) { initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength); }