/** 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; }
@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 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(); }
/** * 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); } }
@Override public void finishRequest() throws IOException { stream.getSink().close(); }
System.out.println("Transmit : " + transmit); sink.write(transmit, transmit.size()); System.out.println("Wire : " + wire);
@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()); }
@Override public Timeout timeout() { return sink.timeout(); }
@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) { } }
private void closeQuietly(Sink sink) { try { sink.close(); } catch (Exception ignore) { logger.w(ignore, "Failed to close sink"); } } }
@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(); } } });
@Override public Timeout timeout() { return delegate.timeout(); }
@Override public void flush() throws IOException { delegate.flush(); }
/** 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; }
@Override public void finishRequest() throws IOException { stream.getSink().close(); }
@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."); }
@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); }
@Override public Transport newInstance() throws TransportFactoryInitException { try { Socket socket; if (config.getUseSSL()) { if (config.getVerifySSL()) { socket = SSLSocketFactory.getDefault().createSocket(config.getHostname(), config.getPort()); } else { socket = getSSLContext().getSocketFactory().createSocket(config.getHostname(), config.getPort()); } } else { socket = new Socket(config.getHostname(), config.getPort()); } socket.setReuseAddress(true); Sink sink = Okio.sink(socket); sink.timeout().timeout(config.getTimeout(), TimeUnit.MILLISECONDS); return new TcpTransport(sink, config.getRetryCount(), config.getRetryDelay()); } catch (IOException ex) { throw new TransportFactoryInitException("Error while creating tcp transport", ex); } }