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

The following examples show how to use com.google.common.io.ByteArrayDataOutput#writeShort() . 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 apkfile 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 3
Source File: ResourceString.java    From android-chunk-utils 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: 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 5
Source File: ClassWriter.java    From turbine with Apache License 2.0 6 votes vote down vote up
/** Writes a {@link ClassFile} to bytecode. */
public static byte[] writeClass(ClassFile classfile) {
  ConstantPool pool = new ConstantPool();
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.writeShort(classfile.access());
  output.writeShort(pool.classInfo(classfile.name()));
  output.writeShort(classfile.superName() != null ? pool.classInfo(classfile.superName()) : 0);
  output.writeShort(classfile.interfaces().size());
  for (String i : classfile.interfaces()) {
    output.writeShort(pool.classInfo(i));
  }
  output.writeShort(classfile.fields().size());
  for (ClassFile.FieldInfo f : classfile.fields()) {
    writeField(pool, output, f);
  }
  output.writeShort(classfile.methods().size());
  for (ClassFile.MethodInfo m : classfile.methods()) {
    writeMethod(pool, output, m);
  }
  writeAttributes(pool, output, LowerAttributes.classAttributes(classfile));
  return finishClass(pool, output, classfile);
}
 
Example 6
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 7
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
private static void writeAttributes(
    ConstantPool pool, ByteArrayDataOutput body, List<Attribute> attributes) {
  body.writeShort(attributes.size());
  for (Attribute attribute : attributes) {
    new AttributeWriter(pool, body).write(attribute);
  }
}
 
Example 8
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 9
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
private void writeTypeAnnotation(TypeAnnotations attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  tmp.writeShort(attribute.annotations().size());
  for (TypeAnnotationInfo annotation : attribute.annotations()) {
    new AnnotationWriter(pool, tmp).writeTypeAnnotation(annotation);
  }
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 10
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
public void writeParameterAnnotations(Attribute.ParameterAnnotations attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  tmp.writeByte(attribute.annotations().size());
  for (List<AnnotationInfo> parameterAnnotations : attribute.annotations()) {
    tmp.writeShort(parameterAnnotations.size());
    for (AnnotationInfo annotation : parameterAnnotations) {
      new AnnotationWriter(pool, tmp).writeAnnotation(annotation);
    }
  }
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 11
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
public void writeAnnotation(Annotations attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  tmp.writeShort(attribute.annotations().size());
  for (AnnotationInfo annotation : attribute.annotations()) {
    new AnnotationWriter(pool, tmp).writeAnnotation(annotation);
  }
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 12
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 13
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
static void writeConstantPool(ConstantPool constantPool, ByteArrayDataOutput output) {
  output.writeShort(constantPool.nextEntry);
  for (ConstantPool.Entry e : constantPool.constants()) {
    output.writeByte(e.kind().tag());
    Value value = e.value();
    switch (e.kind()) {
      case CLASS_INFO:
      case STRING:
      case MODULE:
      case PACKAGE:
        output.writeShort(((IntValue) value).value());
        break;
      case INTEGER:
        output.writeInt(((IntValue) value).value());
        break;
      case DOUBLE:
        output.writeDouble(((DoubleValue) value).value());
        break;
      case FLOAT:
        output.writeFloat(((FloatValue) value).value());
        break;
      case LONG:
        output.writeLong(((LongValue) value).value());
        break;
      case UTF8:
        output.writeUTF(((StringValue) value).value());
        break;
    }
  }
}
 
Example 14
Source File: PacketOutHandshake.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(ByteArrayDataOutput out, int protocolId) {
    writeVarInt(FishingBot.getInstance().getServerProtocol(), out);
    writeString(serverName + "\0FML\0", out);
    out.writeShort(serverPort);
    writeVarInt(2, out); //next State = 2 -> LOGIN
}
 
Example 15
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
private static void writeField(
    ConstantPool pool, ByteArrayDataOutput output, ClassFile.FieldInfo field) {
  output.writeShort(field.access());
  output.writeShort(pool.utf8(field.name()));
  output.writeShort(pool.utf8(field.descriptor()));
  writeAttributes(pool, output, LowerAttributes.fieldAttributes(field));
}
 
Example 16
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
private static void writeMethod(
    ConstantPool pool, ByteArrayDataOutput output, ClassFile.MethodInfo method) {
  output.writeShort(method.access());
  output.writeShort(pool.utf8(method.name()));
  output.writeShort(pool.utf8(method.descriptor()));
  writeAttributes(pool, output, LowerAttributes.methodAttributes(method));
}
 
Example 17
Source File: BungeeCordImpl.java    From helper with MIT License 5 votes vote down vote up
@Override
public void appendPayload(ByteArrayDataOutput out) {
    out.writeUTF(this.playerName);
    out.writeUTF(this.channelName);
    out.writeShort(this.data.length);
    out.write(this.data);
}
 
Example 18
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);
}