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

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

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

ByteString.encodeUtf8介绍

[英]Returns a new byte string containing the UTF-8 bytes of s.
[中]返回包含s的UTF-8字节的新字节字符串。

代码示例

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

public Builder(String boundary) {
 this.boundary = ByteString.encodeUtf8(boundary);
}

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

public Header(ByteString name, String value) {
 this(name, ByteString.encodeUtf8(value));
}

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

public Header(String name, String value) {
 this(ByteString.encodeUtf8(name), ByteString.encodeUtf8(value));
}

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

@Override public boolean send(String text) {
 if (text == null) throw new NullPointerException("text == null");
 return send(ByteString.encodeUtf8(text), OPCODE_TEXT);
}

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

public static String key(HttpUrl url) {
 return ByteString.encodeUtf8(url.toString()).md5().hex();
}

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

public static String acceptHeader(String key) {
 return ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC).sha1().base64();
}

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

public void dumpStringData(String s) throws IOException {
 System.out.println("                       " + s);
 System.out.println("        String.length: " + s.length());
 System.out.println("String.codePointCount: " + s.codePointCount(0, s.length()));
 System.out.println("            Utf8.size: " + Utf8.size(s));
 System.out.println("          UTF-8 bytes: " + ByteString.encodeUtf8(s).hex());
 System.out.println();
}

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

synchronized boolean close(int code, String reason, long cancelAfterCloseMillis) {
 validateCloseCode(code);
 ByteString reasonBytes = null;
 if (reason != null) {
  reasonBytes = ByteString.encodeUtf8(reason);
  if (reasonBytes.size() > CLOSE_MESSAGE_MAX) {
   throw new IllegalArgumentException("reason.size() > " + CLOSE_MESSAGE_MAX + ": " + reason);
  }
 }
 if (failed || enqueuedClose) return false;
 // Immediately prevent further frames from being enqueued.
 enqueuedClose = true;
 // Enqueue the close frame.
 messageAndCloseQueue.add(new Close(code, reasonBytes, cancelAfterCloseMillis));
 runWriter();
 return true;
}

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

void checkResponse(Response response) throws ProtocolException {
 if (response.code() != 101) {
  throw new ProtocolException("Expected HTTP 101 response but was '"
    + response.code() + " " + response.message() + "'");
 }
 String headerConnection = response.header("Connection");
 if (!"Upgrade".equalsIgnoreCase(headerConnection)) {
  throw new ProtocolException("Expected 'Connection' header value 'Upgrade' but was '"
    + headerConnection + "'");
 }
 String headerUpgrade = response.header("Upgrade");
 if (!"websocket".equalsIgnoreCase(headerUpgrade)) {
  throw new ProtocolException(
    "Expected 'Upgrade' header value 'websocket' but was '" + headerUpgrade + "'");
 }
 String headerAccept = response.header("Sec-WebSocket-Accept");
 String acceptExpected = ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC)
   .sha1().base64();
 if (!acceptExpected.equals(headerAccept)) {
  throw new ProtocolException("Expected 'Sec-WebSocket-Accept' header value '"
    + acceptExpected + "' but was '" + headerAccept + "'");
 }
}

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

@Test public void selectNotFound() throws IOException {
 Options options = Options.Companion.of(
   ByteString.encodeUtf8("ROCK"),
   ByteString.encodeUtf8("SCISSORS"),
   ByteString.encodeUtf8("PAPER"));
 sink.writeUtf8("SPOCK");
 sink.emit();
 assertEquals(-1, source.select(options));
 assertEquals("SPOCK", source.readUtf8());
}

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

@Test public void selectLongerThanSource() throws IOException {
 Options options = Options.Companion.of(
   ByteString.encodeUtf8("abcd"),
   ByteString.encodeUtf8("abce"),
   ByteString.encodeUtf8("abcc"));
 sink.writeUtf8("abc");
 sink.emit();
 assertEquals(-1, source.select(options));
 assertEquals("abc", source.readUtf8());
}

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

@Test public void indexOfByteStringWithFromIndex() throws Exception {
 sink.writeUtf8("aaa");
 sink.emit();
 assertEquals(0, source.indexOf(ByteString.encodeUtf8("a")));
 assertEquals(0, source.indexOf(ByteString.encodeUtf8("a"), 0));
 assertEquals(1, source.indexOf(ByteString.encodeUtf8("a"), 1));
 assertEquals(2, source.indexOf(ByteString.encodeUtf8("a"), 2));
}

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

@Test public void indexOfElementWithFromIndex() throws Exception {
 sink.writeUtf8("aaa");
 sink.emit();
 assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a")));
 assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("a"), 0));
 assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("a"), 1));
 assertEquals(2, source.indexOfElement(ByteString.encodeUtf8("a"), 2));
}

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

@Test public void indexOfElement() throws IOException {
 sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c");
 sink.emit();
 assertEquals(0, source.indexOfElement(ByteString.encodeUtf8("DEFGaHIJK")));
 assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJKb")));
 assertEquals(SEGMENT_SIZE + 1, source.indexOfElement(ByteString.encodeUtf8("cDEFGHIJK")));
 assertEquals(1, source.indexOfElement(ByteString.encodeUtf8("DEFbGHIc")));
 assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJK")));
 assertEquals(-1L, source.indexOfElement(ByteString.encodeUtf8("")));
}

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

@Test public void writeUtf8SubstringWithCharset() throws IOException {
 sink.writeString("təˈranəˌsôr", 3, 7, Charset.forName("utf-8"));
 sink.flush();
 assertEquals(ByteString.encodeUtf8("ranə"), data.readByteString());
}

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

@Test public void readAndToUppercase() throws Exception {
 InputStream in = new ByteArrayInputStream("abc".getBytes(Charsets.UTF_8));
 assertEquals(ByteString.encodeUtf8("AB"), ByteString.read(in, 2).toAsciiUppercase());
 assertEquals(ByteString.encodeUtf8("C"), ByteString.read(in, 1).toAsciiUppercase());
 assertEquals(ByteString.EMPTY, ByteString.read(in, 0).toAsciiUppercase());
}

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

@Test public void indexOfByteStringWithOffset() throws IOException {
 assertEquals(-1, source.indexOf(ByteString.encodeUtf8("flop"), 1));
 sink.writeUtf8("flop flip flop");
 sink.emit();
 assertEquals(10, source.indexOf(ByteString.encodeUtf8("flop"), 1));
 source.readUtf8(); // Clear stream
 // Make sure we backtrack and resume searching after partial match.
 sink.writeUtf8("hi hi hi hi hey");
 sink.emit();
 assertEquals(6, source.indexOf(ByteString.encodeUtf8("hi hi hey"), 1));
}

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

@Test public void selectValuesHaveCommonPrefix() throws IOException {
 Options options = Options.Companion.of(
   ByteString.encodeUtf8("abcd"),
   ByteString.encodeUtf8("abce"),
   ByteString.encodeUtf8("abcc"));
 sink.writeUtf8("abcc").writeUtf8("abcd").writeUtf8("abce");
 sink.emit();
 assertEquals(2, source.select(options));
 assertEquals(0, source.select(options));
 assertEquals(1, source.select(options));
}

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

@Test public void indexOfElementWithOffset() throws IOException {
 sink.writeUtf8("a").writeUtf8(repeat('b', SEGMENT_SIZE)).writeUtf8("c");
 sink.emit();
 assertEquals(-1, source.indexOfElement(ByteString.encodeUtf8("DEFGaHIJK"), 1));
 assertEquals(15, source.indexOfElement(ByteString.encodeUtf8("DEFGHIJKb"), 15));
}

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

@Test public void rangeEqualsOnlyReadsUntilMismatch() throws IOException {
 assumeTrue(factory == Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE); // Other sources read in chunks anyway.
 sink.writeUtf8("A man, a plan, a canal. Panama.");
 sink.emit();
 assertFalse(source.rangeEquals(0, ByteString.encodeUtf8("A man.")));
 assertEquals("A man,", source.getBuffer().readUtf8());
}

相关文章