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

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

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

ByteString.decodeHex介绍

[英]Decodes the hex-encoded bytes and returns their value a byte string.
[中]对十六进制编码的字节进行解码,并将其值作为字节字符串返回。

代码示例

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

@Override public void onOpen(WebSocket webSocket, Response response) {
 webSocket.send("Hello...");
 webSocket.send("...World!");
 webSocket.send(ByteString.decodeHex("deadbeef"));
 webSocket.close(1000, "Goodbye, World!");
}

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

@Override public ByteString decodeHex(String hex) {
 Buffer buffer = new Buffer();
 buffer.write(ByteString.decodeHex(hex));
 return buffer.snapshot();
}

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

@Test public void decodeHexOddNumberOfChars() throws Exception {
 try {
  ByteString.decodeHex("aaa");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

@Test public void decodeHexInvalidChar() throws Exception {
 try {
  ByteString.decodeHex("a\u0000");
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

@Test public void endsWithByteString() throws Exception {
 ByteString byteString = factory.decodeHex("112233");
 assertTrue(byteString.endsWith(ByteString.decodeHex("")));
 assertTrue(byteString.endsWith(ByteString.decodeHex("33")));
 assertTrue(byteString.endsWith(ByteString.decodeHex("2233")));
 assertTrue(byteString.endsWith(ByteString.decodeHex("112233")));
 assertFalse(byteString.endsWith(ByteString.decodeHex("1122")));
 assertFalse(byteString.endsWith(ByteString.decodeHex("00112233")));
 assertFalse(byteString.endsWith(ByteString.decodeHex("002233")));
}

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

@Test public void decodeMalformedStringReturnsReplacementCharacter() throws Exception {
 Charset utf16be = Charset.forName("UTF-16BE");
 String string = ByteString.decodeHex("04").string(utf16be);
 assertEquals("\ufffd", string);
}

代码示例来源: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 startsWithByteArray() throws Exception {
 ByteString byteString = factory.decodeHex("112233");
 assertTrue(byteString.startsWith(ByteString.decodeHex("").toByteArray()));
 assertTrue(byteString.startsWith(ByteString.decodeHex("11").toByteArray()));
 assertTrue(byteString.startsWith(ByteString.decodeHex("1122").toByteArray()));
 assertTrue(byteString.startsWith(ByteString.decodeHex("112233").toByteArray()));
 assertFalse(byteString.startsWith(ByteString.decodeHex("2233").toByteArray()));
 assertFalse(byteString.startsWith(ByteString.decodeHex("11223344").toByteArray()));
 assertFalse(byteString.startsWith(ByteString.decodeHex("112244").toByteArray()));
}

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

@Test public void indexOfByteArray() throws Exception {
 ByteString byteString = factory.decodeHex("112233");
 assertEquals(0, byteString.indexOf(ByteString.decodeHex("112233").toByteArray()));
 assertEquals(1, byteString.indexOf(ByteString.decodeHex("2233").toByteArray()));
 assertEquals(2, byteString.indexOf(ByteString.decodeHex("33").toByteArray()));
 assertEquals(-1, byteString.indexOf(ByteString.decodeHex("112244").toByteArray()));
}

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

@Test public void lastIndexOfByteArray() throws Exception {
 ByteString byteString = factory.decodeHex("112233");
 assertEquals(0, byteString.lastIndexOf(ByteString.decodeHex("112233").toByteArray()));
 assertEquals(1, byteString.lastIndexOf(ByteString.decodeHex("2233").toByteArray()));
 assertEquals(2, byteString.lastIndexOf(ByteString.decodeHex("33").toByteArray()));
 assertEquals(3, byteString.lastIndexOf(ByteString.decodeHex("").toByteArray()));
}

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

@Test public void writeStringWithCharset() throws IOException {
 sink.writeString("təˈranəˌsôr", Charset.forName("utf-32be"));
 sink.flush();
 assertEquals(ByteString.decodeHex("0000007400000259000002c800000072000000610000006e00000259"
   + "000002cc00000073000000f400000072"), data.readByteString());
}

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

@Test public void writeSubstringWithCharset() throws IOException {
 sink.writeString("təˈranəˌsôr", 3, 7, Charset.forName("utf-32be"));
 sink.flush();
 assertEquals(ByteString.decodeHex("00000072000000610000006e00000259"), data.readByteString());
}

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

@Test public void readSpecificCharsetPartial() throws Exception {
 sink.write(ByteString.decodeHex("0000007600000259000002c80000006c000000e40000007300000259"
   + "000002cc000000720000006100000070000000740000025900000072"));
 sink.emit();
 assertEquals("vəˈläsə", source.readString(7 * 4, Charset.forName("utf-32")));
}

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

@Test public void encodeDecodeStringUtf16be() throws Exception {
 Charset utf16be = Charset.forName("UTF-16BE");
 ByteString byteString = ByteString.encodeString(bronzeHorseman, utf16be);
 assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf16be));
 assertEquals(byteString, ByteString.decodeHex("041d043000200431043504400435043304430020043f0443"
   + "04410442044b043d043d044b044500200432043e043b043d"));
 assertEquals(bronzeHorseman, byteString.string(utf16be));
}

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

@Test public void encodeDecodeStringUtf8() throws Exception {
 Charset utf8 = Charset.forName("UTF-8");
 ByteString byteString = ByteString.encodeString(bronzeHorseman, utf8);
 assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf8));
 assertEquals(byteString, ByteString.decodeHex("d09dd0b020d0b1d0b5d180d0b5d0b3d18320d0bfd183d181"
   + "d182d18bd0bdd0bdd18bd18520d0b2d0bed0bbd0bd"));
 assertEquals(bronzeHorseman, byteString.string(utf8));
}

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

@Test public void readCodePointBeyondUnicodeMaximum() throws Exception {
 // A 4-byte encoding with data above the U+10ffff Unicode maximum.
 Buffer buffer = new Buffer();
 buffer.write(ByteString.decodeHex("f4908080"));
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertTrue(buffer.exhausted());
}

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

@Test public void readSurrogateCodePoint() throws Exception {
 Buffer buffer = new Buffer();
 buffer.write(ByteString.decodeHex("eda080"));
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertTrue(buffer.exhausted());
 buffer.write(ByteString.decodeHex("edbfbf"));
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertTrue(buffer.exhausted());
}

代码示例来源: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/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")));
}

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

@Test public void readTooLargeCodepointReturnsReplacementCharacter() throws Exception {
 // 5-byte and 6-byte code points are not supported.
 Buffer buffer = new Buffer();
 buffer.write(ByteString.decodeHex("f888808080"));
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertEquals(REPLACEMENT_CODE_POINT, buffer.readUtf8CodePoint());
 assertTrue(buffer.exhausted());
}

相关文章