okio.ByteString.of()方法的使用及代码示例

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

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

ByteString.of介绍

[英]Returns a new byte string containing a copy of byteCount bytes of data starting at offset.
[中]返回一个新的字节字符串,该字符串包含从偏移量开始的字节计数字节数据的副本。

代码示例

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

private ByteString randomToken() {
 byte[] bytes = new byte[16];
 secureRandom.nextBytes(bytes);
 return ByteString.of(bytes);
}

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

/**
 * Returns the RSA private key encoded in <a href="https://tools.ietf.org/html/rfc5208">PKCS
 * #8</a> <a href="https://tools.ietf.org/html/rfc7468">PEM format</a>.
 */
public String privateKeyPkcs8Pem() {
 StringBuilder result = new StringBuilder();
 result.append("-----BEGIN PRIVATE KEY-----\n");
 encodeBase64Lines(result, ByteString.of(keyPair.getPrivate().getEncoded()));
 result.append("-----END PRIVATE KEY-----\n");
 return result.toString();
}

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

static ByteString sha256(X509Certificate x509Certificate) {
 return ByteString.of(x509Certificate.getPublicKey().getEncoded()).sha256();
}

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

static ByteString sha1(X509Certificate x509Certificate) {
 return ByteString.of(x509Certificate.getPublicKey().getEncoded()).sha1();
}

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

/**
 * Returns the certificate encoded in <a href="https://tools.ietf.org/html/rfc7468">PEM
 * format</a>.
 */
public String certificatePem() {
 try {
  StringBuilder result = new StringBuilder();
  result.append("-----BEGIN CERTIFICATE-----\n");
  encodeBase64Lines(result, ByteString.of(certificate.getEncoded()));
  result.append("-----END CERTIFICATE-----\n");
  return result.toString();
 } catch (CertificateEncodingException e) {
  throw new AssertionError(e);
 }
}

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

private ByteString pkcs1Bytes() {
 try {
  PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded());
  return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded());
 } catch (IOException e) {
  throw new AssertionError(e);
 }
}

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

public RealWebSocket(Request request, WebSocketListener listener, Random random,
  long pingIntervalMillis) {
 if (!"GET".equals(request.method())) {
  throw new IllegalArgumentException("Request must be GET: " + request.method());
 }
 this.originalRequest = request;
 this.listener = listener;
 this.random = random;
 this.pingIntervalMillis = pingIntervalMillis;
 byte[] nonce = new byte[16];
 random.nextBytes(nonce);
 this.key = ByteString.of(nonce).base64();
 this.writerRunnable = () -> {
  try {
   while (writeOneFrame()) {
   }
  } catch (IOException e) {
   failWebSocket(e, null);
  }
 };
}

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

private void writeCertList(BufferedSink sink, List<Certificate> certificates)
  throws IOException {
 try {
  sink.writeDecimalLong(certificates.size())
    .writeByte('\n');
  for (int i = 0, size = certificates.size(); i < size; i++) {
   byte[] bytes = certificates.get(i).getEncoded();
   String line = ByteString.of(bytes).base64();
   sink.writeUtf8(line)
     .writeByte('\n');
  }
 } catch (CertificateEncodingException e) {
  throw new IOException(e.getMessage());
 }
}

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

private ByteString gzipHeaderWithFlags(byte flags) {
 byte[] result = gzipHeader.toByteArray();
 result[3] = flags;
 return ByteString.of(result);
}

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

/** Reads a potentially Huffman encoded byte string. */
 ByteString readByteString() throws IOException {
  int firstByte = readByte();
  boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN
  int length = readInt(firstByte, PREFIX_7_BITS);
  if (huffmanDecode) {
   return ByteString.of(Huffman.get().decode(source.readByteArray(length)));
  } else {
   return source.readByteString(length);
  }
 }
}

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

@Test public void ofByteBuffer() {
 byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8);
 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
 byteBuffer.position(2).limit(11);
 ByteString byteString = ByteString.of(byteBuffer);
 // Verify that the bytes were copied out.
 byteBuffer.put(4, (byte) 'a');
 assertEquals("llo, Worl", byteString.utf8());
}

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

@Test public void ofCopyRange() {
 byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8);
 ByteString byteString = ByteString.of(bytes, 2, 9);
 // Verify that the bytes were copied out.
 bytes[4] = (byte) 'a';
 assertEquals("llo, Worl", byteString.utf8());
}

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

@Test public void ofCopy() {
 byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8);
 ByteString byteString = ByteString.of(bytes);
 // Verify that the bytes were copied out.
 bytes[4] = (byte) 'a';
 assertEquals("Hello, World!", byteString.utf8());
}

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

@Test public void asByteBuffer() {
  assertEquals(0x42, ByteString.of((byte) 0x41, (byte) 0x42, (byte) 0x43).asByteBuffer().get(1));
 }
}

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

@Test public void decodeNullCharset() throws Exception {
 try {
  ByteString.of().string(null);
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

@Test public void read() throws Exception {
 InputStream in = new ByteArrayInputStream("abc".getBytes(Charsets.UTF_8));
 assertEquals(ByteString.decodeHex("6162"), ByteString.read(in, 2));
 assertEquals(ByteString.decodeHex("63"), ByteString.read(in, 1));
 assertEquals(ByteString.of(), ByteString.read(in, 0));
}

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

@Test public void accessByteByByte() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 try (UnsafeCursor cursor = buffer.readUnsafe()) {
  byte[] actual = new byte[(int) buffer.size()];
  for (int i = 0; i < buffer.size(); i++) {
   cursor.seek(i);
   actual[i] = cursor.data[cursor.start];
  }
  assertEquals(ByteString.of(actual), buffer.snapshot());
 }
}

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

@Test public void accessByteByByteReverse() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 try (UnsafeCursor cursor = buffer.readUnsafe()) {
  byte[] actual = new byte[(int) buffer.size()];
  for (int i = (int) (buffer.size() - 1); i >= 0; i--) {
   cursor.seek(i);
   actual[i] = cursor.data[cursor.start];
  }
  assertEquals(ByteString.of(actual), buffer.snapshot());
 }
}

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

@Test public void accessByteByByteAlwaysResettingToZero() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 try (UnsafeCursor cursor = buffer.readUnsafe()) {
  byte[] actual = new byte[(int) buffer.size()];
  for (int i = 0; i < buffer.size(); i++) {
   cursor.seek(i);
   actual[i] = cursor.data[cursor.start];
   cursor.seek(0L);
  }
  assertEquals(ByteString.of(actual), buffer.snapshot());
 }
}

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

@SuppressWarnings("SelfEquals")
@Test public void equals() throws Exception {
 ByteString byteString = factory.decodeHex("000102");
 assertTrue(byteString.equals(byteString));
 assertTrue(byteString.equals(ByteString.decodeHex("000102")));
 assertTrue(factory.decodeHex("").equals(ByteString.EMPTY));
 assertTrue(factory.decodeHex("").equals(ByteString.of()));
 assertTrue(ByteString.EMPTY.equals(factory.decodeHex("")));
 assertTrue(ByteString.of().equals(factory.decodeHex("")));
 assertFalse(byteString.equals(new Object()));
 assertFalse(byteString.equals(ByteString.decodeHex("000201")));
}

相关文章