/** * Read data from {@code source} and write it to {@code sink}. This doesn't use {@link * BufferedSink#writeAll} because that method doesn't flush aggressively and we need that. */ private void transfer(Socket sourceSocket, Source source, Sink sink) { try { Buffer buffer = new Buffer(); for (long byteCount; (byteCount = source.read(buffer, 8192L)) != -1; ) { sink.write(buffer, byteCount); sink.flush(); } } catch (IOException e) { System.out.println("transfer failed from " + sourceSocket + ": " + e); } finally { closeQuietly(sink); closeQuietly(source); closeQuietly(sourceSocket); openSockets.remove(sourceSocket); } }
BufferedSource source = response.body().source(); while (!source.exhausted()) { out.write(source.getBuffer(), source.getBuffer().size()); out.flush();
@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 testWriteChannel() throws Exception { Buffer channel = new Buffer(); Sink sink = new ByteChannelSink(channel, Timeout.NONE); sink.write(new Buffer().writeUtf8(quote), 75); assertThat(channel.readUtf8()) .isEqualTo("John, the kind of control you're attempting simply is... it's not possible."); }
/** Reads all bytes from {@code source} and writes them to {@code sink}. */ private Long readAllAndClose(Source source, Sink sink) throws IOException { long result = 0L; Buffer buffer = new Buffer(); for (long count; (count = source.read(buffer, SEGMENT_SIZE)) != -1L; result += count) { sink.write(buffer, count); } source.close(); sink.close(); return result; }
/** Use DeflaterOutputStream to deflate source. */ private Buffer deflate(ByteString source) throws IOException { Buffer result = new Buffer(); Sink sink = Okio.sink(new DeflaterOutputStream(result.outputStream())); sink.write(new Buffer().write(source), source.size()); sink.close(); return result; }
@Test public void testReadWriteFile() throws Exception { Path path = temporaryFolder.newFile().toPath(); Sink sink = new FileChannelSink(FileChannel.open(path, w), Timeout.NONE); sink.write(new Buffer().writeUtf8(quote), 317); sink.close(); assertTrue(Files.exists(path)); assertEquals(quote.length(), Files.size(path)); Buffer buffer = new Buffer(); Source source = new FileChannelSource(FileChannel.open(path, r), Timeout.NONE); source.read(buffer, 44); assertThat(buffer.readUtf8()) .isEqualTo("John, the kind of control you're attempting "); source.read(buffer, 31); assertThat(buffer.readUtf8()) .isEqualTo("simply is... it's not possible."); }
@Test public void sinkFromOutputStream() throws Exception { Buffer data = new Buffer(); data.writeUtf8("a"); data.writeUtf8(repeat('b', 9998)); data.writeUtf8("c"); ByteArrayOutputStream out = new ByteArrayOutputStream(); Sink sink = Okio.sink(out); sink.write(data, 3); assertEquals("abb", out.toString("UTF-8")); sink.write(data, data.size()); assertEquals("a" + repeat('b', 9998) + "c", out.toString("UTF-8")); }
@Test public void writeWithoutTimeout() throws Exception { Socket socket = socket(0, ONE_MB); Sink sink = Okio.buffer(Okio.sink(socket)); sink.timeout().timeout(500, TimeUnit.MILLISECONDS); byte[] data = new byte[ONE_MB]; sink.write(new Buffer().write(data), data.length); sink.flush(); socket.close(); }
@Test public void blackhole() throws Exception { Buffer data = new Buffer(); data.writeUtf8("blackhole"); Sink blackhole = Okio.blackhole(); blackhole.write(data, 5); assertEquals("hole", data.readUtf8()); } }
@Test public void wrappedThrowsWithoutTimeout() throws Exception { Sink sink = new ForwardingSink(new Buffer()) { @Override public void write(Buffer source, long byteCount) throws IOException { throw new IOException("no timeout occurred"); } }; AsyncTimeout timeout = new AsyncTimeout(); timeout.timeout(250, TimeUnit.MILLISECONDS); Sink timeoutSink = timeout.sink(sink); Buffer data = new Buffer().writeUtf8("a"); try { timeoutSink.write(data, 1); fail(); } catch (IOException expected) { assertEquals("no timeout occurred", expected.getMessage()); } }
@Test public void writeWithTimeout() throws Exception { Socket socket = socket(0, 0); Sink sink = Okio.sink(socket); sink.timeout().timeout(500, TimeUnit.MILLISECONDS); byte[] data = new byte[ONE_MB]; long start = System.nanoTime(); try { sink.write(new Buffer().write(data), data.length); sink.flush(); fail(); } catch (SocketTimeoutException expected) { } long elapsed = System.nanoTime() - start; socket.close(); assertTrue("elapsed: " + elapsed, TimeUnit.NANOSECONDS.toMillis(elapsed) >= 500); assertTrue("elapsed: " + elapsed, TimeUnit.NANOSECONDS.toMillis(elapsed) <= 750); }
@Test public void sinkFlushDoesntWaitForReader() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().flush(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8(3)); }
@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 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()); } }
@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 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 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()); }
@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(); }