Refine search
@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."); }
@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(); } });
/** * 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); } } }
/** 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; }
@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(); } } });
/** * 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); } }
System.out.println("Cipher : " + cipher); Buffer wire = new Buffer(); Buffer transmit = new Buffer(); transmit.writeUtf8("This is not really a secure message"); System.out.println("Transmit : " + transmit); Buffer receive = new Buffer(); source.read(receive, Long.MAX_VALUE); System.out.println("Receive : " + receive);
@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 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()); }
public static void transfer( InputStream is, FileProgressCallback callback, Object tag, BufferedSink sink) throws IOException { Source source = null; try { source = Okio.source(is); Buffer os = new Buffer(); while ((source.read(os, 2048) != -1) && !isCancelled(callback)) { byte[] bytes = os.readByteArray(); if (sink != null) { sink.write(bytes); } if (callback != null) { callback.onBytes(bytes); callback.increment(bytes.length); } } if (isCancelled(callback)) { HttpUtil.cancel(tag); } } finally { Util.closeQuietly(source); } }
@Test public void wrappedSourceCloseTimesOut() throws Exception { Source source = new ForwardingSource(new Buffer()) { @Override public void close() throws IOException { try { Thread.sleep(500); } catch (InterruptedException e) { throw new AssertionError(); } } }; AsyncTimeout timeout = new AsyncTimeout(); timeout.timeout(250, TimeUnit.MILLISECONDS); Source timeoutSource = timeout.source(source); try { timeoutSource.close(); fail(); } catch (InterruptedIOException expected) { } }
private static void okio1() { try (Source source = Okio.buffer(Okio.source(new File("./19_io/text.txt")))) { Buffer buffer = new Buffer(); source.read(buffer, 1024); System.out.println(buffer.readUtf8Line()); System.out.println(buffer.readUtf8Line()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = responseBodySource.read(sink, byteCount); } catch (IOException e) { if (!closed) { // Failed to write a complete cache response. closed = true; abortCacheQuietly(); } throw e; } if (bytesRead == -1) { if (!closed) { // The cache response is complete! closed = true; commitCache(); } return -1; } responseBodyCacheSink.copyFrom(sink, sink.size() - bytesRead, bytesRead); return bytesRead; }
@Override public void skip(long byteCount) throws IOException { if (closed) throw new IllegalStateException("closed"); while (byteCount > 0) { if (buffer.size == 0 && source.read(buffer, Segment.SIZE) == -1) { throw new EOFException(); } long toSkip = Math.min(byteCount, buffer.size()); buffer.skip(toSkip); byteCount -= toSkip; } }
@Override public long readAll(Sink sink) throws IOException { if (sink == null) throw new IllegalArgumentException("sink == null"); long totalBytesWritten = 0; while (source.read(buffer, Segment.SIZE) != -1) { long emitByteCount = buffer.completeSegmentByteCount(); if (emitByteCount > 0) { totalBytesWritten += emitByteCount; sink.write(buffer, emitByteCount); } } if (buffer.size() > 0) { totalBytesWritten += buffer.size(); sink.write(buffer, buffer.size()); } return totalBytesWritten; }
static void closeQuietly(Source source) { try { source.close(); } catch (Exception ignore) { // ignore } }
@Override public long read(Buffer sink, long byteCount) throws IOException { long result = source.read(sink, byteCount); if (result == -1) exhausted = true; return result; }