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

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

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

ByteString.utf8介绍

[英]Constructs a new String by decoding the bytes as UTF-8.
[中]通过将字节解码为UTF-8来构造新字符串。

代码示例

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

public String boundary() {
 return boundary.utf8();
}

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

@Override public String toString() {
  return Util.format("%s: %s", name.utf8(), value.utf8());
 }
}

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

public static Headers toHeaders(List<Header> headerBlock) {
 Headers.Builder builder = new Headers.Builder();
 for (Header header : headerBlock) {
  Internal.instance.addLenient(builder, header.name.utf8(), header.value.utf8());
 }
 return builder.build();
}

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

@Override public String toString() {
  return Util.format("%s: %s", name.utf8(), value.utf8());
 }
}

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

/**
  * An HTTP/2 response cannot contain uppercase header characters and must be treated as
  * malformed.
  */
 static ByteString checkLowercase(ByteString name) throws IOException {
  for (int i = 0, length = name.size(); i < length; i++) {
   byte c = name.getByte(i);
   if (c >= 'A' && c <= 'Z') {
    throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8());
   }
  }
  return name;
 }
}

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

MultipartBody(ByteString boundary, MediaType type, List<Part> parts) {
 this.boundary = boundary;
 this.originalType = type;
 this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8());
 this.parts = Util.immutableList(parts);
}

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

public static Headers toHeaders(List<Header> headerBlock) {
 Headers.Builder builder = new Headers.Builder();
 for (Header header : headerBlock) {
  Internal.instance.addLenient(builder, header.name.utf8(), header.value.utf8());
 }
 return builder.build();
}

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

MultipartBody(ByteString boundary, MediaType type, List<Part> parts) {
 this.boundary = boundary;
 this.originalType = type;
 this.contentType = MediaType.get(type + "; boundary=" + boundary.utf8());
 this.parts = Util.immutableList(parts);
}

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

public void readConnectionPreface(Handler handler) throws IOException {
 if (client) {
  // The client reads the initial SETTINGS frame.
  if (!nextFrame(true, handler)) {
   throw ioException("Required SETTINGS preface not received");
  }
 } else {
  // The server reads the CONNECTION_PREFACE byte string.
  ByteString connectionPreface = source.readByteString(CONNECTION_PREFACE.size());
  if (logger.isLoggable(FINE)) logger.fine(format("<< CONNECTION %s", connectionPreface.hex()));
  if (!CONNECTION_PREFACE.equals(connectionPreface)) {
   throw ioException("Expected a connection header but was %s", connectionPreface.utf8());
  }
 }
}

代码示例来源: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 decodeBase64WithWhitespace() {
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AA AA").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("A    AAA").utf8());
 assertEquals("", ByteString.decodeBase64("    ").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 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 ignoreUnnecessaryPadding() {
 assertEquals("", ByteString.decodeBase64("====").utf8());
 assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AAAA====").utf8());
}

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

@Test public void utf8() throws Exception {
 ByteString byteString = factory.encodeUtf8(bronzeHorseman);
 assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(Charsets.UTF_8));
 assertTrue(byteString.equals(ByteString.of(bronzeHorseman.getBytes(Charsets.UTF_8))));
 assertEquals(byteString.utf8(), bronzeHorseman);
}

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

@Test public void readByteString() throws IOException {
 sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE));
 sink.emit();
 assertEquals("abcd" + repeat('e', SEGMENT_SIZE), source.readByteString().utf8());
}

代码示例来源:origin: k9mail/k-9

@Test
public void read_withByteArraySmallerThanLimit_shouldConsumeSizeOfByteArray() throws Exception {
  FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6);
  byte[] data = new byte[5];
  int numberOfBytesRead = fixedLengthInputStream.read(data);
  assertEquals(5, numberOfBytesRead);
  assertEquals("Hello", ByteString.of(data).utf8());
}

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

@Test public void readByteStringPartial() throws IOException {
 sink.writeUtf8("abcd").writeUtf8(repeat('e', SEGMENT_SIZE));
 sink.emit();
 assertEquals("abc", source.readByteString(3).utf8());
 assertEquals("d", source.readUtf8(1));
}

代码示例来源:origin: k9mail/k-9

@Test
public void read_withOverSizedByteArrayInMiddleOfStream_shouldReturnDataUpToLimit() throws Exception {
  FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6);
  consumeBytes(fixedLengthInputStream, 5);
  byte[] data = new byte[10];
  int numberOfBytesRead = fixedLengthInputStream.read(data);
  assertEquals(1, numberOfBytesRead);
  assertEquals(" ", ByteString.of(data, 0, numberOfBytesRead).utf8());
}

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

@Test public void decodeBase64() {
 assertEquals("", ByteString.decodeBase64("").utf8());
 assertEquals(null, ByteString.decodeBase64("/===")); // Can't do anything with 6 bits!
 assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("//=="));
 assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("__=="));
 assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("///="));
 assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("___="));
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("////"));
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("____"));
 assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("////////"));
 assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("________"));
 assertEquals("What's to be scared about? It's just a little hiccup in the power...",
   ByteString.decodeBase64("V2hhdCdzIHRvIGJlIHNjYXJlZCBhYm91dD8gSXQncyBqdXN0IGEgbGl0dGxlIGhpY2"
     + "N1cCBpbiB0aGUgcG93ZXIuLi4=").utf8());
 // Uses two encoding styles. Malformed, but supported as a side-effect.
 assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("__//"));
}

相关文章