下面列出了com.google.common.io.ByteArrayDataOutput#write ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void sendSWRMessage(Player player, String server, ArrayList<String> messages) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Forward");
out.writeUTF(server);
out.writeUTF("SWRMessaging");
ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
DataOutputStream msgout = new DataOutputStream(msgbytes);
try {
for (String msg: messages) {
msgout.writeUTF(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
out.writeShort(msgbytes.toByteArray().length);
out.write(msgbytes.toByteArray());
player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
}
private String readLine() throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
int i = 0;
int c;
while ((c = raf.read()) != -1) {
i++;
out.write((byte) c);
if (c == LINE_SEP.charAt(0)) {
break;
}
}
if (i == 0) {
return null;
}
return new String(out.toByteArray(), Charsets.UTF_8);
}
private String readLine() throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
int i = 0;
int c;
while ((c = raf.read()) != -1) {
i++;
out.write((byte) c);
if (c == LINE_SEP.charAt(0)) {
break;
}
}
if (i == 0) {
return null;
}
return new String(out.toByteArray(), Charsets.UTF_8);
}
/**
* Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
* Strings are prefixed by 2 values. The first is the number of characters in the string.
* The second is the encoding length (number of bytes in the string).
*
* <p>Here's an example UTF-8-encoded string of ab©:
* <pre>03 04 61 62 C2 A9 00</pre>
*
* @param str The string to be encoded.
* @param type The encoding type that the {@link ResourceString} should be encoded in.
* @return The encoded string.
*/
public static byte[] encodeString(String str, Type type) {
byte[] bytes = str.getBytes(type.charset());
// The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
encodeLength(output, str.length(), type);
if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
encodeLength(output, bytes.length, type);
}
output.write(bytes);
// NULL-terminate the string
if (type == Type.UTF8) {
output.write(0);
} else {
output.writeShort(0);
}
return output.toByteArray();
}
private static void encodeLength(ByteArrayDataOutput output, int length, Type type) {
if (length < 0) {
output.write(0);
return;
}
if (type == Type.UTF8) {
if (length > 0x7F) {
output.write(((length & 0x7F00) >> 8) | 0x80);
}
output.write(length & 0xFF);
} else { // UTF-16
// TODO(acornwall): Replace output with a little-endian output.
if (length > 0x7FFF) {
int highBytes = ((length & 0x7FFF0000) >> 16) | 0x8000;
output.write(highBytes & 0xFF);
output.write((highBytes & 0xFF00) >> 8);
}
int lowBytes = length & 0xFFFF;
output.write(lowBytes & 0xFF);
output.write((lowBytes & 0xFF00) >> 8);
}
}
@Override
public void write(ByteArrayDataOutput out, int protocolId) throws IOException {
byte[] sharedSecret = CryptManager.encryptData(getPublicKey(), getSecretKey().getEncoded());
byte[] verifyToken = CryptManager.encryptData(getPublicKey(), getVerifyToken());
writeVarInt(sharedSecret.length, out);
out.write(sharedSecret);
writeVarInt(verifyToken.length, out);
out.write(verifyToken);
}
private void sendForwardedBungeecordMessage(final String subChannel, final String... data) {
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Forward");
out.writeUTF("ONLINE");
out.writeUTF(subChannel);
final ByteArrayDataOutput dataOut = ByteStreams.newDataOutput();
for (final String element : data) {
dataOut.writeUTF(element);
}
final byte[] dataBytes = dataOut.toByteArray();
out.writeShort(dataBytes.length);
out.write(dataBytes);
bukkitService.sendBungeeMessage(out.toByteArray());
}
public static void writeString(String s, ByteArrayDataOutput buf) {
if (s.length() > Short.MAX_VALUE) {
throw new OverflowPacketException(String.format("Cannot send string longer than Short.MAX_VALUE (got %s characters)", s.length()));
}
byte[] b = s.getBytes(Charsets.UTF_8);
writeVarInt(b.length, buf);
buf.write(b);
}
private void send(ByteArrayDataOutput buf, DataOutputStream out) throws IOException {
ByteArrayDataOutput sender = ByteStreams.newDataOutput();
Packet.writeVarInt(buf.toByteArray().length, sender);
sender.write(buf.toByteArray());
out.write(sender.toByteArray());
out.flush();
}
@Override
public void appendPayload(ByteArrayDataOutput out) {
out.writeUTF(this.serverName);
out.writeUTF(this.channelName);
out.writeShort(this.data.length);
out.write(this.data);
}
private static void encodeString(String input, ByteArrayDataOutput output) {
int length = input.length();
byte[] bytes = new byte[VarInt.varIntSize(length)];
VarInt.putVarInt(length, bytes, 0);
output.write(bytes);
output.write(input.getBytes(Charsets.UTF_8));
}
public static Slice bulkString(Slice s) {
if (s == null) {
return NULL;
}
ByteArrayDataOutput bo = ByteStreams.newDataOutput();
bo.write(String.format("$%d\r\n", s.length()).getBytes());
bo.write(s.data());
bo.write("\r\n".getBytes());
return new Slice(bo.toByteArray());
}
@Test
public void testDeserializeDuplicateKeys() throws TagContextDeserializationException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.write(BinarySerializationUtils.VERSION_ID);
encodeTagToOutput("Key1", "Value1", output);
encodeTagToOutput("Key1", "Value2", output);
TagContext expected =
tagger.emptyBuilder().put(TagKey.create("Key1"), TagValue.create("Value2")).build();
assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
@Test
public void testDeserializeOneTag() throws TagContextDeserializationException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.write(BinarySerializationUtils.VERSION_ID);
encodeTagToOutput("Key", "Value", output);
TagContext expected =
tagger.emptyBuilder().put(TagKey.create("Key"), TagValue.create("Value")).build();
assertThat(serializer.fromByteArray(output.toByteArray())).isEqualTo(expected);
}
@Test
public void testDeserializeTooLargeByteArrayThrowException_WithDuplicateTagKeys()
throws TagContextDeserializationException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.write(BinarySerializationUtils.VERSION_ID);
for (int i = 0; i < BinarySerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) {
// Each tag will be with format {key : "key_", value : "0123"}, so the length of it is 8.
String str;
if (i < 10) {
str = "000" + i;
} else if (i < 100) {
str = "00" + i;
} else if (i < 1000) {
str = "0" + i;
} else {
str = String.valueOf(i);
}
encodeTagToOutput("key_", str, output);
}
// The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte
// more than limit.
encodeTagToOutput("key_", "last1", output);
byte[] bytes = output.toByteArray();
thrown.expect(TagContextDeserializationException.class);
thrown.expectMessage("Size of TagContext exceeds the maximum serialized size ");
serializer.fromByteArray(bytes);
}
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
for (Chunk chunk : chunks) {
output.write(chunk.toByteArray(shrink));
}
return output.toByteArray();
}
@Test
public void testDeserializeInvalidTagKey() throws TagContextDeserializationException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.write(BinarySerializationUtils.VERSION_ID);
// Encode an invalid tag key and a valid tag value:
encodeTagToOutput("\2key", "value", output);
final byte[] bytes = output.toByteArray();
thrown.expect(TagContextDeserializationException.class);
thrown.expectMessage("Invalid tag key: \2key");
serializer.fromByteArray(bytes);
}
private static byte[] finishClass(
ConstantPool pool, ByteArrayDataOutput body, ClassFile classfile) {
ByteArrayDataOutput result = ByteStreams.newDataOutput();
result.writeInt(MAGIC);
result.writeShort(MINOR_VERSION);
result.writeShort(classfile.module() != null ? MODULE_MAJOR_VERSION : MAJOR_VERSION);
writeConstantPool(pool, result);
result.write(body.toByteArray());
return result.toByteArray();
}
private static final void putVarInt(int input, ByteArrayDataOutput byteArrayDataOutput) {
byte[] output = new byte[VarInt.varIntSize(input)];
VarInt.putVarInt(input, output, 0);
byteArrayDataOutput.write(output);
}
private static final void encodeTag(Tag tag, ByteArrayDataOutput byteArrayDataOutput) {
byteArrayDataOutput.write(TAG_FIELD_ID);
encodeString(tag.getKey().getName(), byteArrayDataOutput);
encodeString(tag.getValue().asString(), byteArrayDataOutput);
}