com.google.common.io.ByteArrayDataOutput#write ( )源码实例Demo

下面列出了com.google.common.io.ByteArrayDataOutput#write ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SkyWarsReloaded   文件: SkyWarsReloaded.java
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());
}
 
源代码2 项目: uavstack   文件: TailFile.java
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);
    }
 
源代码3 项目: uavstack   文件: TailFile.java
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);
    }
 
源代码4 项目: android-arscblamer   文件: ResourceString.java
/**
 * 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();
}
 
源代码5 项目: android-arscblamer   文件: ResourceString.java
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);
  }
}
 
源代码6 项目: FishingBot   文件: PacketOutEncryptionResponse.java
@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);
}
 
源代码7 项目: AuthMeReloaded   文件: BungeeSender.java
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());
}
 
源代码8 项目: FishingBot   文件: Packet.java
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);
}
 
源代码9 项目: FishingBot   文件: ServerPinger.java
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();
}
 
源代码10 项目: helper   文件: BungeeCordImpl.java
@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));
}
 
源代码12 项目: redis-mock   文件: Response.java
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);
}
 
源代码16 项目: android-chunk-utils   文件: ResourceFile.java
@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);
}
 
源代码18 项目: turbine   文件: ClassWriter.java
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();
}
 
源代码19 项目: opencensus-java   文件: BinarySerializationUtils.java
private static final void putVarInt(int input, ByteArrayDataOutput byteArrayDataOutput) {
  byte[] output = new byte[VarInt.varIntSize(input)];
  VarInt.putVarInt(input, output, 0);
  byteArrayDataOutput.write(output);
}
 
源代码20 项目: opencensus-java   文件: BinarySerializationUtils.java
private static final void encodeTag(Tag tag, ByteArrayDataOutput byteArrayDataOutput) {
  byteArrayDataOutput.write(TAG_FIELD_ID);
  encodeString(tag.getKey().getName(), byteArrayDataOutput);
  encodeString(tag.getValue().asString(), byteArrayDataOutput);
}