okio.Source.read()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(215)

本文整理了Java中okio.Source.read()方法的一些代码示例,展示了Source.read()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Source.read()方法的具体详情如下:
包路径:okio.Source
类名称:Source
方法名:read

Source.read介绍

[英]Removes at least 1, and up to byteCount bytes from this and appends them to sink. Returns the number of bytes read, or -1 if this source is exhausted.
[中]从中删除至少1个字节,最多字节数字节,并将其附加到接收器。返回读取的字节数,如果此源已耗尽,则返回-1。

代码示例

代码示例来源:origin: square/okio

@Override public long read(Buffer sink, long byteCount) throws IOException {
 long result = source.read(sink, byteCount);
 if (result == -1) exhausted = true;
 return result;
}

代码示例来源:origin: square/okhttp

@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;
 }
}

代码示例来源:origin: square/okhttp

@Override public void writeTo(BufferedSink sink) throws IOException {
  Buffer buffer = new Buffer();
  while (pipe.source().read(buffer, 8192) != -1L) {
   sink.write(buffer, buffer.size());
  }
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

@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;
 }
}

代码示例来源:origin: square/okio

/**
 * 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);
 }
}

代码示例来源:origin: square/okhttp

/**
 * 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);
  }
 }
}

代码示例来源:origin: square/okio

@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.");
}

代码示例来源:origin: square/okio

@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) {
 }
}

代码示例来源:origin: square/okio

/** 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;
}

代码示例来源:origin: square/okhttp

long upstreamBytesRead = upstream.read(upstreamBuffer, bufferMaxSize);

代码示例来源:origin: square/okio

@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.");
}

代码示例来源:origin: square/okio

@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) {
 }
}

代码示例来源:origin: square/okio

@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();
  }
 }
});

代码示例来源:origin: square/okio

@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));
}

代码示例来源:origin: square/okio

@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());
 }
}

代码示例来源:origin: square/okio

@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();
 }
});

代码示例来源:origin: square/okio

@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();
}

代码示例来源:origin: square/okio

@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);
}

代码示例来源:origin: square/okio

@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());
}

代码示例来源:origin: square/okio

@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());
}

相关文章