Java Code Examples for com.google.common.io.ByteArrayDataOutput#write()

The following examples show how to use com.google.common.io.ByteArrayDataOutput#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SkyWarsReloaded.java    From SkyWarsReloaded with GNU General Public License v3.0 6 votes vote down vote up
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());
}
 
Example 2
Source File: ResourceString.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 3
Source File: ResourceString.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: TailFile.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example 5
Source File: TailFile.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example 6
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: ResourceFile.java    From android-chunk-utils with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  for (Chunk chunk : chunks) {
    output.write(chunk.toByteArray(shrink));
  }
  return output.toByteArray();
}
 
Example 10
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: Response.java    From redis-mock with MIT License 5 votes vote down vote up
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());
}
 
Example 13
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 14
Source File: BungeeCordImpl.java    From helper with MIT License 5 votes vote down vote up
@Override
public void appendPayload(ByteArrayDataOutput out) {
    out.writeUTF(this.serverName);
    out.writeUTF(this.channelName);
    out.writeShort(this.data.length);
    out.write(this.data);
}
 
Example 15
Source File: ServerPinger.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 16
Source File: Packet.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: BungeeSender.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
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());
}
 
Example 18
Source File: PacketOutEncryptionResponse.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 19
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static final void putVarInt(int input, ByteArrayDataOutput byteArrayDataOutput) {
  byte[] output = new byte[VarInt.varIntSize(input)];
  VarInt.putVarInt(input, output, 0);
  byteArrayDataOutput.write(output);
}
 
Example 20
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
private static final void encodeTag(Tag tag, ByteArrayDataOutput byteArrayDataOutput) {
  byteArrayDataOutput.write(TAG_FIELD_ID);
  encodeString(tag.getKey().getName(), byteArrayDataOutput);
  encodeString(tag.getValue().asString(), byteArrayDataOutput);
}