okio.Buffer.copyTo()方法的使用及代码示例

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

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

Buffer.copyTo介绍

[英]Copy the contents of this to out.
[中]把这本书的内容复制出来。

代码示例

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

@Override public void writeTo(BufferedSink sink) throws IOException {
  buffer.copyTo(sink.buffer(), 0, buffer.size());
 }
}

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

/**
 * Returns true if the body in question probably contains human readable text. Uses a small sample
 * of code points to detect unicode control characters commonly used in binary file signatures.
 */
static boolean isPlaintext(Buffer buffer) {
 try {
  Buffer prefix = new Buffer();
  long byteCount = buffer.size() < 64 ? buffer.size() : 64;
  buffer.copyTo(prefix, 0, byteCount);
  for (int i = 0; i < 16; i++) {
   if (prefix.exhausted()) {
    break;
   }
   int codePoint = prefix.readUtf8CodePoint();
   if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
    return false;
   }
  }
  return true;
 } catch (EOFException e) {
  return false; // Truncated UTF-8 sequence.
 }
}

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

buffer.copyTo(sink, sourcePos - bufferPos, bytesToRead);
sourcePos += bytesToRead;
return bytesToRead;
upstreamBuffer.copyTo(sink, 0, bytesRead);
sourcePos += bytesRead;

代码示例来源:origin: jgilfelt/chuck

/**
 * Returns true if the body in question probably contains human readable text. Uses a small sample
 * of code points to detect unicode control characters commonly used in binary file signatures.
 */
private boolean isPlaintext(Buffer buffer) {
  try {
    Buffer prefix = new Buffer();
    long byteCount = buffer.size() < 64 ? buffer.size() : 64;
    buffer.copyTo(prefix, 0, byteCount);
    for (int i = 0; i < 16; i++) {
      if (prefix.exhausted()) {
        break;
      }
      int codePoint = prefix.readUtf8CodePoint();
      if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
        return false;
      }
    }
    return true;
  } catch (EOFException e) {
    return false; // Truncated UTF-8 sequence.
  }
}

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

/**
 * Returns true if the body in question probably contains human readable text. Uses a small sample
 * of code points to detect unicode control characters commonly used in binary file signatures.
 */
static boolean isPlaintext(Buffer buffer) {
  try {
    Buffer prefix = new Buffer();
    long byteCount = buffer.size() < 64 ? buffer.size() : 64;
    buffer.copyTo(prefix, 0, byteCount);
    for (int i = 0; i < 16; i++) {
      if (prefix.exhausted()) {
        break;
      }
      int codePoint = prefix.readUtf8CodePoint();
      if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
        return false;
      }
    }
    return true;
  } catch (EOFException e) {
    return false; // Truncated UTF-8 sequence.
  }
}

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

public static ByteString encodeQuery(String host, int type) {
 Buffer buf = new Buffer();
 buf.writeShort(0); // query id
 buf.writeShort(256); // flags with recursion
 buf.writeShort(1); // question count
 buf.writeShort(0); // answerCount
 buf.writeShort(0); // authorityResourceCount
 buf.writeShort(0); // additional
 Buffer nameBuf = new Buffer();
 final String[] labels = host.split("\\.");
 for (String label : labels) {
  long utf8ByteCount = Utf8.size(label);
  if (utf8ByteCount != label.length()) {
   throw new IllegalArgumentException("non-ascii hostname: " + host);
  }
  nameBuf.writeByte((byte) utf8ByteCount);
  nameBuf.writeUtf8(label);
 }
 nameBuf.writeByte(0); // end
 nameBuf.copyTo(buf, 0, nameBuf.size());
 buf.writeShort(type);
 buf.writeShort(1); // CLASS_IN
 return buf.readByteString();
}

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

long posInBuffer = offset - mark;
long result = Math.min(byteCount, markBuffer.size() - posInBuffer);
markBuffer.copyTo(sink, posInBuffer, result);
offset += result;
return result;
long result = super.read(markBuffer, Math.min(byteCount, byteCountBeforeLimit));
if (result == -1L) return -1L;
markBuffer.copyTo(sink, markBuffer.size() - result, result);
offset += result;
return result;

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

void copyFrom(Buffer buffer, long offset, long bytesCount) {
 if (failed) return;
 try {
  BufferedSink outSink = (BufferedSink) delegate();
  buffer.copyTo(outSink.buffer(), offset, bytesCount);
  outSink.emitCompleteSegments();
 } catch (Exception e) {
  failed = true;
  onException(e);
 }
}

代码示例来源: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 copyToStream() throws Exception {
 Buffer buffer = new Buffer().writeUtf8("hello, world!");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 buffer.copyTo(out);
 String outString = new String(out.toByteArray(), UTF_8);
 assertEquals("hello, world!", outString);
 assertEquals("hello, world!", buffer.readUtf8());
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-16246
public void shouldSendLargeTextFile() throws IOException {
  prepareResponse(response -> {});
  Resource resource = new ClassPathResource("largeTextFile.txt", getClass());
  byte[] expected = Files.readAllBytes(resource.getFile().toPath());
  Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096);
  this.webClient.post()
      .uri("/")
      .body(body, DataBuffer.class)
      .retrieve()
      .bodyToMono(Void.class)
      .block(Duration.ofSeconds(5));
  expectRequest(request -> {
    ByteArrayOutputStream actual = new ByteArrayOutputStream();
    try {
      request.getBody().copyTo(actual);
    }
    catch (IOException ex) {
      throw new IllegalStateException(ex);
    }
    assertEquals(expected.length, actual.size());
    assertEquals(hash(expected), hash(actual.toByteArray()));
  });
}

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

@Test public void copyTo() {
 Buffer source = new Buffer();
 source.writeUtf8("party");
 Buffer target = new Buffer();
 source.copyTo(target, 1, 3);
 assertEquals("art", target.readUtf8());
 assertEquals("party", source.readUtf8());
}

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

@Test public void copyToEmptySource() {
 Buffer source = new Buffer();
 Buffer target = new Buffer().writeUtf8("aaa");
 source.copyTo(target, 0L, 0L);
 assertEquals("", source.readUtf8());
 assertEquals("aaa", target.readUtf8());
}

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

@Test public void copyToEmptyTarget() {
 Buffer source = new Buffer().writeUtf8("aaa");
 Buffer target = new Buffer();
 source.copyTo(target, 0L, 3L);
 assertEquals("aaa", source.readUtf8());
 assertEquals("aaa", target.readUtf8());
}

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

@Test public void copyToOffSegmentBoundary() {
 String as = repeat('a', SEGMENT_SIZE - 1);
 String bs = repeat('b', SEGMENT_SIZE + 2);
 String cs = repeat('c', SEGMENT_SIZE - 4);
 String ds = repeat('d', SEGMENT_SIZE + 8);
 Buffer source = new Buffer();
 source.writeUtf8(as);
 source.writeUtf8(bs);
 source.writeUtf8(cs);
 Buffer target = new Buffer();
 target.writeUtf8(ds);
 source.copyTo(target, as.length(), bs.length() + cs.length());
 assertEquals(ds + bs + cs, target.readUtf8());
}

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

@Test public void copyToOnSegmentBoundary() {
 String as = repeat('a', SEGMENT_SIZE);
 String bs = repeat('b', SEGMENT_SIZE);
 String cs = repeat('c', SEGMENT_SIZE);
 String ds = repeat('d', SEGMENT_SIZE);
 Buffer source = new Buffer();
 source.writeUtf8(as);
 source.writeUtf8(bs);
 source.writeUtf8(cs);
 Buffer target = new Buffer();
 target.writeUtf8(ds);
 source.copyTo(target, as.length(), bs.length() + cs.length());
 assertEquals(ds + bs + cs, target.readUtf8());
}

代码示例来源: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 copyToSpanningSegments() throws Exception {
 Buffer source = new Buffer();
 source.writeUtf8(repeat('a', SEGMENT_SIZE * 2));
 source.writeUtf8(repeat('b', SEGMENT_SIZE * 2));
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 source.copyTo(out, 10, SEGMENT_SIZE * 3);
 assertEquals(repeat('a', SEGMENT_SIZE * 2 - 10) + repeat('b', SEGMENT_SIZE + 10),
   out.toString());
 assertEquals(repeat('a', SEGMENT_SIZE * 2) + repeat('b', SEGMENT_SIZE * 2),
   source.readUtf8(SEGMENT_SIZE * 4));
}

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

@Test public void copyToSourceAndTargetCanBeTheSame() {
 String as = repeat('a', SEGMENT_SIZE);
 String bs = repeat('b', SEGMENT_SIZE);
 Buffer source = new Buffer();
 source.writeUtf8(as);
 source.writeUtf8(bs);
 source.copyTo(source, 0, source.size());
 assertEquals(as + bs + as + bs, source.readUtf8());
}

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

@Test public void shrink() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 assumeTrue(buffer.size() > 3);
 long originalSize = buffer.size();
 Buffer expected = new Buffer();
 deepCopy(buffer).copyTo(expected, 0, originalSize - 3);
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  assertEquals(originalSize, cursor.resizeBuffer(originalSize - 3));
 }
 assertEquals(expected, buffer);
}

相关文章