Class BinaryWriter

java.lang.Object
java.io.OutputStream
net.minestom.server.utils.binary.BinaryWriter
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BinaryWriter extends OutputStream
Class used to write to a byte array. WARNING: not thread-safe.
  • Constructor Details

    • BinaryWriter

      public BinaryWriter(@NotNull @NotNull ByteBuffer buffer)
    • BinaryWriter

      public BinaryWriter(int initialCapacity)
    • BinaryWriter

      public BinaryWriter()
  • Method Details

    • view

      @Experimental public static BinaryWriter view(ByteBuffer buffer)
    • ensureSize

      protected void ensureSize(int length)
    • writeComponent

      public void writeComponent(@NotNull @NotNull Component component)
      Writes a component to the buffer as a sized string.
      Parameters:
      component - the component
    • writeByte

      public void writeByte(byte b)
      Writes a single byte to the buffer.
      Parameters:
      b - the byte to write
    • writeBoolean

      public void writeBoolean(boolean b)
      Writes a single boolean to the buffer.
      Parameters:
      b - the boolean to write
    • writeChar

      public void writeChar(char c)
      Writes a single char to the buffer.
      Parameters:
      c - the char to write
    • writeShort

      public void writeShort(short s)
      Writes a single short to the buffer.
      Parameters:
      s - the short to write
    • writeInt

      public void writeInt(int i)
      Writes a single int to the buffer.
      Parameters:
      i - the int to write
    • writeLong

      public void writeLong(long l)
      Writes a single long to the buffer.
      Parameters:
      l - the long to write
    • writeFloat

      public void writeFloat(float f)
      Writes a single float to the buffer.
      Parameters:
      f - the float to write
    • writeDouble

      public void writeDouble(double d)
      Writes a single double to the buffer.
      Parameters:
      d - the double to write
    • writeVarInt

      public void writeVarInt(int i)
      Writes a single var-int to the buffer.
      Parameters:
      i - the int to write
    • writeVarLong

      public void writeVarLong(long l)
      Writes a single var-long to the buffer.
      Parameters:
      l - the long to write
    • writeSizedString

      public void writeSizedString(@NotNull @NotNull String string)
      Writes a string to the buffer.

      The size is a var-int type.

      Parameters:
      string - the string to write
    • writeNullTerminatedString

      public void writeNullTerminatedString(@NotNull @NotNull String string, @NotNull @NotNull Charset charset)
      Writes a null terminated string to the buffer. This method adds the null character to the end of the string before writing.
      Parameters:
      string - the string to write
      charset - the charset to encode in
    • writeVarIntArray

      public void writeVarIntArray(int[] array)
      Writes a var-int array to the buffer.

      It is sized by another var-int at the beginning.

      Parameters:
      array - the integers to write
    • writeVarLongArray

      public void writeVarLongArray(long[] array)
    • writeLongArray

      public void writeLongArray(long[] array)
    • writeByteArray

      public void writeByteArray(byte[] array)
    • writeBytes

      public void writeBytes(byte @NotNull [] bytes)
      Writes a byte array.

      WARNING: it doesn't write the length of bytes.

      Parameters:
      bytes - the byte array to write
    • writeStringArray

      public void writeStringArray(@NotNull @NotNull String[] array)
      Writes a string to the buffer.

      The array is sized by a var-int and all strings are wrote using writeSizedString(String).

      Parameters:
      array - the string array to write
    • writeUuid

      public void writeUuid(@NotNull @NotNull UUID uuid)
      Writes an UUID. It is done by writing both long, the most and least significant bits.
      Parameters:
      uuid - the UUID to write
    • writeBlockPosition

      public void writeBlockPosition(@NotNull @NotNull Point point)
    • writeBlockPosition

      public void writeBlockPosition(int x, int y, int z)
    • writeItemStack

      public void writeItemStack(@NotNull @NotNull ItemStack itemStack)
    • writeNBT

      public void writeNBT(@NotNull @NotNull String name, @NotNull @NotNull org.jglrxavpok.hephaistos.nbt.NBT tag)
    • write

      public void write(@NotNull @NotNull Writeable writeable)
      Writes the given writeable object into this writer.
      Parameters:
      writeable - the object to write
    • write

      public void write(@NotNull @NotNull ByteBuffer buffer)
    • write

      public void write(@NotNull @NotNull BinaryWriter writer)
    • writeArray

      public void writeArray(@NotNull @NotNull Writeable[] writeables)
      Writes an array of writeable objects to this writer. Will prepend the binary stream with a var int to denote the length of the array.
      Parameters:
      writeables - the array of writeables to write
    • writeVarIntList

      public <T> void writeVarIntList(Collection<T> list, @NotNull @NotNull BiConsumer<BinaryWriter,T> consumer)
    • writeByteList

      public <T> void writeByteList(Collection<T> list, @NotNull @NotNull BiConsumer<BinaryWriter,T> consumer)
    • toByteArray

      public byte[] toByteArray()
      Converts the internal buffer to a byte array.
      Returns:
      the byte array containing all the BinaryWriter data
    • writeAtStart

      public void writeAtStart(@NotNull @NotNull BinaryWriter headerWriter)
      Adds a BinaryWriter's ByteBuffer at the beginning of this writer.
      Parameters:
      headerWriter - the BinaryWriter to add at the beginning
    • writeAtEnd

      public void writeAtEnd(@NotNull @NotNull BinaryWriter footerWriter)
      Adds a BinaryWriter's ByteBuffer at the end of this writer.
      Parameters:
      footerWriter - the BinaryWriter to add at the end
    • concat

      public static ByteBuffer concat(ByteBuffer... buffers)
    • getBuffer

      @NotNull public @NotNull ByteBuffer getBuffer()
      Gets the raw buffer used by this binary writer.
      Returns:
      the raw buffer
    • setBuffer

      public void setBuffer(ByteBuffer buffer)
      Changes the buffer used by this binary writer.
      Parameters:
      buffer - the new buffer used by this binary writer
    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • writeUnsignedShort

      public void writeUnsignedShort(int yourShort)
    • makeArray

      public static byte[] makeArray(@NotNull @NotNull Consumer<@NotNull BinaryWriter> writing)
      Returns a byte[] with the contents written via BinaryWriter