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

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

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

ByteString.toByteArray介绍

[英]Returns a byte array containing a copy of the bytes in this ByteString.
[中]返回包含此ByteString中字节副本的字节数组。

代码示例

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

public synchronized void connectionPreface() throws IOException {
 if (closed) throw new IOException("closed");
 if (!client) return; // Nothing to write; servers don't send connection headers!
 if (logger.isLoggable(FINE)) {
  logger.fine(format(">> CONNECTION %s", CONNECTION_PREFACE.hex()));
 }
 sink.write(CONNECTION_PREFACE.toByteArray());
 sink.flush();
}

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

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

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

@Test
public void testDefaults() throws Exception {
 assertThat(AllTypes.DEFAULT_DEFAULT_BOOL).isEqualTo((Object) true);
 // original: "<c-cedilla>ok\a\b\f\n\r\t\v\1\01\001\17\017\176\x1\x01\x11\X1\X01\X11g<u umlaut>zel"
 assertThat(AllTypes.DEFAULT_DEFAULT_STRING).isEqualTo( "çok\u0007\b\f\n\r\t\u000b\u0001\u0001"
   + "\u0001\u000f\u000f~\u0001\u0001\u0011\u0001\u0001\u0011güzel");
 assertThat(new String(AllTypes.DEFAULT_DEFAULT_BYTES.toByteArray(), "ISO-8859-1")).isEqualTo(
   "çok\u0007\b\f\n\r\t\u000b\u0001\u0001\u0001\u000f\u000f~\u0001\u0001\u0011\u0001\u0001"
     + "\u0011güzel");
}

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

@Test public void upToRecursionLimit() throws Exception {
 // tag 2: nested message (64 times)
 // tag 1: signed varint32 456
 ByteString data = ByteString.decodeHex("127e127c127a12781276127412721270126e126c126a12681266126"
   + "412621260125e125c125a12581256125412521250124e124c124a12481246124412421240123e123c123a123"
   + "81236123412321230122e122c122a12281226122412221220121e121c121a12181216121412121210120e120"
   + "c120a1208120612041202120008c803");
 Recursive recursive = Recursive.ADAPTER.decode(data.toByteArray());
 assertThat(recursive.value.intValue()).isEqualTo(456);
}

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

@Test public void unknownTypeThrowsIOException() throws Exception {
 // tag 1 / type 0: 456
 // tag 2 / type 7: 789
 ByteString data = ByteString.decodeHex("08c803179506");
 try {
  OneField.ADAPTER.decode(data.toByteArray());
  fail();
 } catch (ProtocolException expected) {
  assertThat(expected).hasMessage("Unexpected field encoding: 7");
 }
}

代码示例来源: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 endsWithByteArray() throws Exception {
 ByteString byteString = factory.decodeHex("112233");
 assertTrue(byteString.endsWith(ByteString.decodeHex("").toByteArray()));
 assertTrue(byteString.endsWith(ByteString.decodeHex("33").toByteArray()));
 assertTrue(byteString.endsWith(ByteString.decodeHex("2233").toByteArray()));
 assertTrue(byteString.endsWith(ByteString.decodeHex("112233").toByteArray()));
 assertFalse(byteString.endsWith(ByteString.decodeHex("1122").toByteArray()));
 assertFalse(byteString.endsWith(ByteString.decodeHex("00112233").toByteArray()));
 assertFalse(byteString.endsWith(ByteString.decodeHex("002233").toByteArray()));
}

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

@Test public void gunzip_withHCRC() throws Exception {
 CRC32 hcrc = new CRC32();
 ByteString gzipHeader = gzipHeaderWithFlags((byte) 0x02);
 hcrc.update(gzipHeader.toByteArray());
 Buffer gzipped = new Buffer();
 gzipped.write(gzipHeader);
 gzipped.writeShort(TestUtil.reverseBytes((short) hcrc.getValue())); // little endian
 gzipped.write(deflated);
 gzipped.write(gzipTrailer);
 assertGzipped(gzipped);
}

代码示例来源: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 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 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 encodeDecodeStringAsciiIsLossy() throws Exception {
 Charset ascii = Charset.forName("US-ASCII");
 ByteString byteString = ByteString.encodeString(bronzeHorseman, ascii);
 assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(ascii));
 assertEquals(byteString,
   ByteString.decodeHex("3f3f203f3f3f3f3f3f203f3f3f3f3f3f3f3f3f203f3f3f3f"));
 assertEquals("?? ?????? ????????? ????", byteString.string(ascii));
}

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

@Test public void encodeDecodeStringUtf32be() throws Exception {
 Charset utf32be = Charset.forName("UTF-32BE");
 ByteString byteString = ByteString.encodeString(bronzeHorseman, utf32be);
 assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf32be));
 assertEquals(byteString, ByteString.decodeHex("0000041d0000043000000020000004310000043500000440"
   + "000004350000043300000443000000200000043f0000044300000441000004420000044b0000043d0000043d"
   + "0000044b0000044500000020000004320000043e0000043b0000043d"));
 assertEquals(bronzeHorseman, byteString.string(utf32be));
}

代码示例来源: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/wire

@Test public void lastValueWinsForRepeatedValueOfNonrepeatedField() throws Exception {
 // tag 1 / type 0: 456
 // tag 1 / type 0: 789
 ByteString data = ByteString.decodeHex("08c803089506");
 OneField oneField = OneField.ADAPTER.decode(data.toByteArray());
 assertThat(new OneField.Builder().opt_int32(789).build()).isEqualTo(oneField);
}

代码示例来源: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/wire

@Test public void unknownTagIgnored() throws Exception {
 // tag 1 / type 0: 456
 // tag 2 / type 0: 789
 ByteString data = ByteString.decodeHex("08c803109506");
 OneField oneField = OneField.ADAPTER.decode(data.toByteArray());
 OneField expected = new OneField.Builder().opt_int32(456).build();
 assertThat(oneField).isNotEqualTo(expected);
 assertThat(oneField.withoutUnknownFields()).isEqualTo(expected);
}

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

@Ignore("TODO(jwilson)")
@Test public void typeMismatchHonorsWireDeclaredType() throws Exception {
 // tag 1 / 3-byte length-delimited string: 0x109506
 // (0x109506 is a well-formed proto message that sets tag 2 to 456).
 ByteString data = ByteString.decodeHex("0a03109506");
 OneField oneField = OneField.ADAPTER.decode(data.toByteArray());
 assertThat(oneField).isEqualTo(new OneField.Builder().opt_int32(3).build());
}

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

@Test public void gunzipWhenCRCIncorrect() {
 Buffer gzipped = new Buffer();
 gzipped.write(gzipHeader);
 gzipped.write(deflated);
 gzipped.writeInt(TestUtil.reverseBytes(0x1234567)); // wrong CRC
 gzipped.write(gzipTrailer.toByteArray(), 3, 4);
 try {
  gunzip(gzipped);
  fail();
 } catch (IOException e) {
  assertEquals("CRC: actual 0x37ad8f8d != expected 0x01234567", e.getMessage());
 }
}

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

@Test public void gunzipWhenLengthIncorrect() {
 Buffer gzipped = new Buffer();
 gzipped.write(gzipHeader);
 gzipped.write(deflated);
 gzipped.write(gzipTrailer.toByteArray(), 0, 4);
 gzipped.writeInt(TestUtil.reverseBytes(0x123456)); // wrong length
 try {
  gunzip(gzipped);
  fail();
 } catch (IOException e) {
  assertEquals("ISIZE: actual 0x00000020 != expected 0x00123456", e.getMessage());
 }
}

相关文章