/** * 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); } }
while (!source.exhausted()) { out.write(source.getBuffer(), source.getBuffer().size()); out.flush();
@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 wrappedSinkFlushTimesOut() throws Exception { Sink sink = new ForwardingSink(new Buffer()) { @Override public void flush() throws IOException { try { Thread.sleep(500); } catch (InterruptedException e) { throw new AssertionError(); } } }; AsyncTimeout timeout = new AsyncTimeout(); timeout.timeout(250, TimeUnit.MILLISECONDS); Sink timeoutSink = timeout.sink(sink); try { timeoutSink.flush(); fail(); } catch (InterruptedIOException expected) { } }
@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 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 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()); } }
@Override public void flush() throws IOException { delegate.flush(); }
@Override public void flush() throws IOException { if (closed) throw new IllegalStateException("closed"); if (buffer.size > 0) { sink.write(buffer, buffer.size); } sink.flush(); }
@Override public void flush() throws IOException { boolean throwOnTimeout = false; enter(); try { sink.flush(); throwOnTimeout = true; } catch (IOException e) { throw exit(e); } finally { exit(throwOnTimeout); } }
while (!source.exhausted()) { out.write(source.getBuffer(), source.getBuffer().size()); out.flush();