Class StreamSerializer

java.lang.Object
com.comphenix.protocol.utility.StreamSerializer

public class StreamSerializer extends Object
Utility methods for reading and writing Minecraft objects to streams.
  • Constructor Details

    • StreamSerializer

      public StreamSerializer()
  • Method Details

    • getDefault

      public static StreamSerializer getDefault()
      Retrieve a default stream serializer.
      Returns:
      A serializer.
    • serializeVarInt

      public void serializeVarInt(DataOutputStream destination, int value) throws IOException
      Write a variable integer to an output stream.
      Parameters:
      destination - - the destination.
      value - - the value to write.
      Throws:
      IOException - The destination stream threw an exception.
    • deserializeVarInt

      public int deserializeVarInt(DataInputStream source) throws IOException
      Read a variable integer from an input stream.
      Parameters:
      source - - the source.
      Returns:
      The integer.
      Throws:
      IOException - The source stream threw an exception.
    • serializeCompound

      public void serializeCompound(DataOutputStream output, NbtCompound compound)
      Write or serialize a NBT compound to the given output stream.

      Note: An NBT compound can be written to a stream even if it's NULL.

      Parameters:
      output - - the target output stream.
      compound - - the NBT compound to be serialized, or NULL to represent nothing.
    • deserializeCompound

      public NbtCompound deserializeCompound(DataInputStream input)
      Read or deserialize an NBT compound from a input stream.
      Parameters:
      input - - the target input stream.
      Returns:
      The resulting compound, or NULL.
    • serializeString

      public void serializeString(DataOutputStream output, String text)
      Serialize a string using the standard Minecraft UTF-16 encoding.

      Note that strings cannot exceed 32767 characters, regardless if maximum lenght.

      Parameters:
      output - - the output stream.
      text - - the string to serialize.
    • deserializeString

      public String deserializeString(DataInputStream input, int maximumLength)
      Deserialize a string using the standard Minecraft UTF-16 encoding.

      Note that strings cannot exceed 32767 characters, regardless if maximum length.

      Parameters:
      input - - the input stream.
      maximumLength - - the maximum length of the string.
      Returns:
      The deserialized string.
    • serializeItemStack

      public String serializeItemStack(org.bukkit.inventory.ItemStack stack) throws IOException
      Serialize an item stack as a base-64 encoded string.

      Note: An ItemStack can be written to the serialized text even if it's NULL.

      Parameters:
      stack - - the item stack to serialize, or NULL to represent air/nothing.
      Returns:
      A base-64 representation of the given item stack.
      Throws:
      IOException - If the operation fails due to reflection problems.
    • deserializeItemStack

      public org.bukkit.inventory.ItemStack deserializeItemStack(String input)
      Deserialize an item stack from a base-64 encoded string.
      Parameters:
      input - - base-64 encoded string.
      Returns:
      A deserialized item stack, or NULL if the serialized ItemStack was also NULL.
    • serializeItemStackToByteArray

      public byte[] serializeItemStackToByteArray(org.bukkit.inventory.ItemStack stack) throws IOException
      Serialize an item stack as byte array.

      Note: An ItemStack can be written to the serialized text even if it's NULL.

      Parameters:
      stack - - the item stack to serialize, or NULL to represent air/nothing.
      Returns:
      A binary representation of the given item stack.
      Throws:
      IOException - If the operation fails due to reflection problems.
    • deserializeItemStackFromByteArray

      public org.bukkit.inventory.ItemStack deserializeItemStackFromByteArray(byte[] input)
      Deserialize an item stack from a byte array.
      Parameters:
      input - - serialized item.
      Returns:
      A deserialized item stack, or NULL if the serialized ItemStack was also NULL.
    • serializeItemStack

      public void serializeItemStack(DataOutputStream output, org.bukkit.inventory.ItemStack stack) throws IOException
      Write or serialize an item stack to the given output stream.

      To supply a byte array, wrap it in a ByteArrayOutputStream and DataOutputStream.

      Note: An ItemStack can be written to a stream even if it's NULL.

      Parameters:
      output - - the target output stream.
      stack - - the item stack that will be written, or NULL to represent air/nothing.
      Throws:
      IOException - If the operation fails due to reflection problems.
    • getBytesAndRelease

      public byte[] getBytesAndRelease(io.netty.buffer.ByteBuf buf)