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

x33g5p2x  于2022-01-16 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(176)

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

BufferedSource.read介绍

[英]Removes up to sink.length bytes from this and copies them into sink. Returns the number of bytes read, or -1 if this source is exhausted.
[中]移到水槽。从中提取字节长度,并将其复制到接收器中。返回读取的字节数,如果此源已用尽,则返回-1。

代码示例

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 try {
  long read = source.read(sink, byteCount);
  if (read > 0) {
   bytesRead += read;
  }
  return read;
 } catch (IOException e) {
  endOfInput(false, e);
  throw e;
 }
}

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

@Test public void gunzipExhaustsSource() throws Exception {
 Buffer gzippedSource = new Buffer()
   .write(ByteString.decodeHex("1f8b08000000000000004b4c4a0600c241243503000000")); // 'abc'
 ExhaustableSource exhaustableSource = new ExhaustableSource(gzippedSource);
 BufferedSource gunzippedSource = Okio.buffer(new GzipSource(exhaustableSource));
 assertEquals('a', gunzippedSource.readByte());
 assertEquals('b', gunzippedSource.readByte());
 assertEquals('c', gunzippedSource.readByte());
 assertFalse(exhaustableSource.exhausted);
 assertEquals(-1, gunzippedSource.read(new Buffer(), 1));
 assertTrue(exhaustableSource.exhausted);
}

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 while (left == 0) {
  source.skip(padding);
  padding = 0;
  if ((flags & FLAG_END_HEADERS) != 0) return -1;
  readContinuationHeader();
  // TODO: test case for empty continuation header?
 }
 long read = source.read(sink, Math.min(byteCount, left));
 if (read == -1) return -1;
 left -= read;
 return read;
}

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 try {
  long read = source.read(sink, byteCount);
  if (read > 0) {
   bytesRead += read;
  }
  return read;
 } catch (IOException e) {
  endOfInput(false, e);
  throw e;
 }
}

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 while (left == 0) {
  source.skip(padding);
  padding = 0;
  if ((flags & FLAG_END_HEADERS) != 0) return -1;
  readContinuationHeader();
  // TODO: test case for empty continuation header?
 }
 long read = source.read(sink, Math.min(byteCount, left));
 if (read == -1) return -1;
 left -= read;
 return read;
}

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

/**
 * Eagerly reads {@code byteCount} bytes from the source before launching a background task to
 * process the data.  This avoids corrupting the stream.
 */
void pushDataLater(final int streamId, final BufferedSource source, final int byteCount,
  final boolean inFinished) throws IOException {
 final Buffer buffer = new Buffer();
 source.require(byteCount); // Eagerly read the frame before firing client thread.
 source.read(buffer, byteCount);
 if (buffer.size() != byteCount) throw new IOException(buffer.size() + " != " + byteCount);
 pushExecutorExecute(new NamedRunnable("OkHttp %s Push Data[%s]", connectionName, streamId) {
  @Override public void execute() {
   try {
    boolean cancel = pushObserver.onData(streamId, buffer, byteCount, inFinished);
    if (cancel) writer.rstStream(streamId, ErrorCode.CANCEL);
    if (cancel || inFinished) {
     synchronized (Http2Connection.this) {
      currentPushRequests.remove(streamId);
     }
    }
   } catch (IOException ignored) {
   }
  }
 });
}

代码示例来源:origin: apollographql/apollo-android

static void copyResponseBody(Response response, Sink sink) throws IOException {
 final int bufferSize = 8 * 1024;
 BufferedSource responseBodySource = response.body().source();
 BufferedSink cacheResponseBody = Okio.buffer(sink);
 while (responseBodySource.read(cacheResponseBody.buffer(), bufferSize) > 0) {
  cacheResponseBody.emit();
 }
 closeQuietly(responseBodySource);
}

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

long read = source.read(buffer, toRead);
if (read == -1) return;

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

long read = in.read(receiveBuffer, byteCount);
if (read == -1) throw new EOFException();
byteCount -= read;

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 long bytesRead;
 try {
  bytesRead = source.read(sink, byteCount);
 } catch (IOException e) {
  if (!cacheRequestClosed) {
   cacheRequestClosed = true;
   cacheRequest.abort(); // Failed to write a complete cache response.
  }
  throw e;
 }
 if (bytesRead == -1) {
  if (!cacheRequestClosed) {
   cacheRequestClosed = true;
   cacheBody.close(); // The cache response is complete!
  }
  return -1;
 }
 sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead);
 cacheBody.emitCompleteSegments();
 return bytesRead;
}

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

@Test public void readNioBuffer() throws Exception {
 String expected = factory.isOneByteAtATime() ? "a" : "abcdefg";
 sink.writeUtf8("abcdefg");
 sink.emit();
 ByteBuffer nioByteBuffer = ByteBuffer.allocate(1024);
 int byteCount = source.read(nioByteBuffer);
 assertEquals(expected.length(), byteCount);
 assertEquals(expected.length(), nioByteBuffer.position());
 assertEquals(nioByteBuffer.capacity(), nioByteBuffer.limit());
 nioByteBuffer.flip();
 byte[] data = new byte[expected.length()];
 nioByteBuffer.get(data);
 assertEquals(expected, new String(data));
}

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

@Test public void readLargeNioBufferOnlyReadsOneSegment() throws Exception {
 String expected = factory.isOneByteAtATime()
   ? "a"
   : TestUtil.repeat('a', SEGMENT_SIZE);
 sink.writeUtf8(TestUtil.repeat('a', SEGMENT_SIZE * 4));
 sink.emit();
 ByteBuffer nioByteBuffer = ByteBuffer.allocate(SEGMENT_SIZE * 3);
 int byteCount = source.read(nioByteBuffer);
 assertEquals(expected.length(), byteCount);
 assertEquals(expected.length(), nioByteBuffer.position());
 assertEquals(nioByteBuffer.capacity(), nioByteBuffer.limit());
 nioByteBuffer.flip();
 byte[] data = new byte[expected.length()];
 nioByteBuffer.get(data);
 assertEquals(expected, new String(data));
}

代码示例来源:origin: aa112901/remusic

public static void getOut(final String url) {
  try {
    mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
    mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
    Request request = new Request.Builder()
        .url(url)
        .build();
    Response response = mOkHttpClient.newCall(request).execute();
    if (response.isSuccessful()) {
      FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + "gedangein" + ".json");
      byte[] c = new byte[1024];
      while (response.body().source().read(c) != -1) {
        fo.write(c);
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: aa112901/remusic

@Override
  public void run() {
    try {
      mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
      mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
      Request request = new Request.Builder()
          .url(url)
          .build();
      Response response = mOkHttpClient.newCall(request).execute();
      if (response.isSuccessful()) {
        FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + name + ".mp3");
        byte[] c = new byte[1024];
        while (response.body().source().read(c) != -1) {
          fo.write(c);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}).start();

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 long bytesRead;
 try {
  bytesRead = source.read(sink, byteCount);
 } catch (IOException e) {
  if (!cacheRequestClosed) {
   cacheRequestClosed = true;
   cacheRequest.abort(); // Failed to write a complete cache response.
  }
  throw e;
 }
 if (bytesRead == -1) {
  if (!cacheRequestClosed) {
   cacheRequestClosed = true;
   cacheBody.close(); // The cache response is complete!
  }
  return -1;
 }
 sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead);
 cacheBody.emitCompleteSegments();
 return bytesRead;
}

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

@Test public void readExhaustedSource() throws Exception {
 Buffer sink = new Buffer();
 sink.writeUtf8(repeat('a', 10));
 assertEquals(-1, source.read(sink, 10));
 assertEquals(10, sink.size());
 assertTrue(source.exhausted());
}

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

@Test public void readZeroBytesFromSource() throws Exception {
 Buffer sink = new Buffer();
 sink.writeUtf8(repeat('a', 10));
 // Either 0 or -1 is reasonable here. For consistency with Android's
 // ByteArrayInputStream we return 0.
 assertEquals(-1, source.read(sink, 0));
 assertEquals(10, sink.size());
 assertTrue(source.exhausted());
}

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

@Test public void readIntoByteArrayNotEnough() throws IOException {
 sink.writeUtf8("abcd");
 sink.emit();
 byte[] sink = new byte[5];
 int read = source.read(sink);
 if (factory.isOneByteAtATime()) {
  assertEquals(1, read);
  byte[] expected = { 'a', 0, 0, 0, 0 };
  assertByteArraysEquals(expected, sink);
 } else {
  assertEquals(4, read);
  byte[] expected = { 'a', 'b', 'c', 'd', 0 };
  assertByteArraysEquals(expected, sink);
 }
}

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

@Test public void readIntoByteArray() throws IOException {
 sink.writeUtf8("abcd");
 sink.emit();
 byte[] sink = new byte[3];
 int read = source.read(sink);
 if (factory.isOneByteAtATime()) {
  assertEquals(1, read);
  byte[] expected = { 'a', 0, 0 };
  assertByteArraysEquals(expected, sink);
 } else {
  assertEquals(3, read);
  byte[] expected = { 'a', 'b', 'c' };
  assertByteArraysEquals(expected, sink);
 }
}

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

@Test public void readIntoByteArrayOffsetAndCount() throws IOException {
 sink.writeUtf8("abcd");
 sink.emit();
 byte[] sink = new byte[7];
 int read = source.read(sink, 2, 3);
 if (factory.isOneByteAtATime()) {
  assertEquals(1, read);
  byte[] expected = { 0, 0, 'a', 0, 0, 0, 0 };
  assertByteArraysEquals(expected, sink);
 } else {
  assertEquals(3, read);
  byte[] expected = { 0, 0, 'a', 'b', 'c', 0, 0 };
  assertByteArraysEquals(expected, sink);
 }
}

相关文章

微信公众号

最新文章

更多