@Override public long read(Buffer sink, long byteCount) throws IOException { long result = source.read(sink, byteCount); if (result == -1) exhausted = true; return result; }
@Override public long read(Buffer sink, long byteCount) throws IOException { try { long read = delegate().read(sink, byteCount); if (read > 0) { bytesRead += read; } return read; } catch (IOException e) { endOfInput(e); throw e; } }
@Override public void writeTo(BufferedSink sink) throws IOException { Buffer buffer = new Buffer(); while (pipe.source().read(buffer, 8192) != -1L) { sink.write(buffer, buffer.size()); } } }
@Override public long read(Buffer sink, long byteCount) throws IOException { try { long read = delegate().read(sink, byteCount); if (read > 0) { bytesRead += read; } return read; } catch (IOException e) { endOfInput(e); throw e; } }
/** * 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); } }
/** * Reads until {@code in} is exhausted or the deadline has been reached. This is careful to not * extend the deadline if one exists already. */ public static boolean skipAll(Source source, int duration, TimeUnit timeUnit) throws IOException { long now = System.nanoTime(); long originalDuration = source.timeout().hasDeadline() ? source.timeout().deadlineNanoTime() - now : Long.MAX_VALUE; source.timeout().deadlineNanoTime(now + Math.min(originalDuration, timeUnit.toNanos(duration))); try { Buffer skipBuffer = new Buffer(); while (source.read(skipBuffer, 8192) != -1) { skipBuffer.clear(); } return true; // Success! The source has been exhausted. } catch (InterruptedIOException e) { return false; // We ran out of time before exhausting the source. } finally { if (originalDuration == Long.MAX_VALUE) { source.timeout().clearDeadline(); } else { source.timeout().deadlineNanoTime(now + originalDuration); } } }
@Test public void testReadChannel() throws Exception { ReadableByteChannel channel = new Buffer().writeUtf8(quote); Buffer buffer = new Buffer(); Source source = new ByteChannelSource(channel, Timeout.NONE); source.read(buffer, 75); assertThat(buffer.readUtf8()) .isEqualTo("John, the kind of control you're attempting simply is... it's not possible."); }
@Test public void sourceFromInputStreamBounds() throws Exception { Source source = Okio.source(new ByteArrayInputStream(new byte[100])); try { source.read(new Buffer(), -1); fail(); } catch (IllegalArgumentException expected) { } }
/** 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; }
long upstreamBytesRead = upstream.read(upstreamBuffer, bufferMaxSize);
@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 wrappedSourceTimesOut() throws Exception { Source source = new ForwardingSource(new Buffer()) { @Override public long read(Buffer sink, long byteCount) throws IOException { try { Thread.sleep(500); return -1; } catch (InterruptedException e) { throw new AssertionError(); } } }; AsyncTimeout timeout = new AsyncTimeout(); timeout.timeout(250, TimeUnit.MILLISECONDS); Source timeoutSource = timeout.source(source); try { timeoutSource.read(new Buffer(), 0); fail(); } catch (InterruptedIOException expected) { } }
@Override public void run() { try { Buffer buffer = new Buffer(); Thread.sleep(1000L); assertEquals(3, pipe.source().read(buffer, Long.MAX_VALUE)); assertEquals("abc", buffer.readUtf8()); Thread.sleep(1000L); assertEquals(3, pipe.source().read(buffer, Long.MAX_VALUE)); assertEquals("def", buffer.readUtf8()); Thread.sleep(1000L); assertEquals(3, pipe.source().read(buffer, Long.MAX_VALUE)); assertEquals("ghi", buffer.readUtf8()); Thread.sleep(1000L); assertEquals(3, pipe.source().read(buffer, Long.MAX_VALUE)); assertEquals("jkl", buffer.readUtf8()); } catch (IOException | InterruptedException e) { throw new AssertionError(); } } });
@Test public void sourceFromInputStream() throws Exception { InputStream in = new ByteArrayInputStream( ("a" + repeat('b', SEGMENT_SIZE * 2) + "c").getBytes(UTF_8)); // Source: ab...bc Source source = Okio.source(in); Buffer sink = new Buffer(); // Source: b...bc. Sink: abb. assertEquals(3, source.read(sink, 3)); assertEquals("abb", sink.readUtf8(3)); // Source: b...bc. Sink: b...b. assertEquals(SEGMENT_SIZE, source.read(sink, 20000)); assertEquals(repeat('b', SEGMENT_SIZE), sink.readUtf8()); // Source: b...bc. Sink: b...bc. assertEquals(SEGMENT_SIZE - 1, source.read(sink, 20000)); assertEquals(repeat('b', SEGMENT_SIZE - 2) + "c", sink.readUtf8()); // Source and sink are empty. assertEquals(-1, source.read(sink, 1)); }
@Test public void sourceClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.source().close(); try { pipe.source().read(new Buffer(), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } }
@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(); } });
@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 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); }
@Test public void sourceTimeout() throws Exception { Pipe pipe = new Pipe(3L); pipe.source().timeout().timeout(1000, TimeUnit.MILLISECONDS); double start = now(); Buffer readBuffer = new Buffer(); try { pipe.source().read(readBuffer, 6L); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); assertEquals(0, readBuffer.size()); }
@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()); }