public BufferedSink createSink() { return Okio.buffer(pipe.sink()); }
public void foldSink(Sink requestBodyOut) throws IOException { pipe.fold(requestBodyOut); }
@Override public void writeTo(BufferedSink sink) throws IOException { sink.writeAll(pipe.source()); } }
public DuplexRequestBody(@Nullable MediaType contentType, long pipeMaxBufferSize) { this.pipe = new Pipe(pipeMaxBufferSize); this.contentType = contentType; }
@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 void writeTo(BufferedSink sink) throws IOException { Buffer buffer = new Buffer(); while (pipe.source().read(buffer, 8192) != -1L) { sink.write(buffer, buffer.size()); } } }
@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()); } }
final Pipe pipe = new Pipe(1000L); // An awkward size to force producer/consumer exchange. final long totalBytes = 16L * 1024L * 1024L; ByteString expectedHash = ByteString.decodeHex("7c3b224bea749086babe079360cf29f98d88262d");
@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()); } }
@Override public void run() { try { pipe.source().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS);
StreamedRequestBody(long expectedContentLength) { initOutputStream(Okio.buffer(pipe.sink()), expectedContentLength); }
@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 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 sourceReadUnblockedByClosedSink() throws Exception { final Pipe pipe = new Pipe(3L); executorService.schedule(new Runnable() { @Override public void run() { try { pipe.sink().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS); double start = now(); Buffer readBuffer = new Buffer(); assertEquals(-1, pipe.source().read(readBuffer, Long.MAX_VALUE)); assertEquals(0, readBuffer.size()); assertElapsed(1000.0, start); }
@Override public ByteString call() throws Exception { Buffer blackhole = new Buffer(); HashingSink hashingSink = HashingSink.sha1(blackhole); Buffer buffer = new Buffer(); while (pipe.source().read(buffer, Long.MAX_VALUE) != -1) { hashingSink.write(buffer, buffer.size()); blackhole.clear(); } pipe.source().close(); return hashingSink.hash(); } });
@Override public void run() { try { pipe.sink().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS);