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.
Author:
Kristian
  • Constructor Details

    • StreamSerializer

      public StreamSerializer()
  • Method Details

    • getDefault

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

      public void serializeVarInt(@Nonnull 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(@Nonnull 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(@Nonnull DataOutputStream output, NbtCompound compound) throws IOException
      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.
      Throws:
      IOException - If the operation fails due to reflection problems.
    • deserializeCompound

      public NbtCompound deserializeCompound(@Nonnull DataInputStream input) throws IOException
      Read or deserialize an NBT compound from a input stream.
      Parameters:
      input - - the target input stream.
      Returns:
      The resulting compound, or NULL.
      Throws:
      IOException - If the operation failed due to reflection or corrupt data.
    • serializeString

      public void serializeString(@Nonnull DataOutputStream output, String text) throws IOException
      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.
      Throws:
      IOException - If the data in the string cannot be written.
    • deserializeString

      public String deserializeString(@Nonnull DataInputStream input, int maximumLength) throws IOException
      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.
      Throws:
      IOException - If deserializing fails
    • 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) throws IOException
      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.
      Throws:
      IOException - If the operation failed due to reflection or corrupt data.
    • 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) throws IOException
      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.
      Throws:
      IOException - If the operation failed due to reflection or corrupt data.
    • 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.
    • deserializeItemStack

      @Deprecated public org.bukkit.inventory.ItemStack deserializeItemStack(DataInputStream input) throws IOException
      Deprecated.
      This is a pretty hacky solution for backwards compatibility. See deserializeItemStack(DataInputStream)
      Read or deserialize an item stack from an underlying input stream.

      To supply a byte array, wrap it in a ByteArrayInputStream and DataInputStream.

      Parameters:
      input - - the target input stream.
      Returns:
      The resulting item stack, or NULL if the serialized item stack was NULL.
      Throws:
      IOException - If the operation failed due to reflection or corrupt data.