Package net.minestom.server.utils.binary
Class BinaryWriter
java.lang.Object
java.io.OutputStream
net.minestom.server.utils.binary.BinaryWriter
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
Class used to write to a byte array.
WARNING: not thread-safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBuffer
concat
(ByteBuffer... buffers) protected void
ensureSize
(int length) @NotNull ByteBuffer
Gets the raw buffer used by this binary writer.static byte[]
makeArray
(@NotNull Consumer<@NotNull BinaryWriter> writing) Returns a byte[] with the contents written via BinaryWritervoid
setBuffer
(ByteBuffer buffer) Changes the buffer used by this binary writer.byte[]
Converts the internal buffer to a byte array.static BinaryWriter
view
(ByteBuffer buffer) void
write
(int b) void
write
(@NotNull ByteBuffer buffer) void
write
(@NotNull BinaryWriter writer) void
Writes the given writeable object into this writer.void
writeArray
(@NotNull Writeable[] writeables) Writes an array of writeable objects to this writer.void
writeAtEnd
(@NotNull BinaryWriter footerWriter) Adds aBinaryWriter
'sByteBuffer
at the end of this writer.void
writeAtStart
(@NotNull BinaryWriter headerWriter) Adds aBinaryWriter
'sByteBuffer
at the beginning of this writer.void
writeBlockPosition
(int x, int y, int z) void
writeBlockPosition
(@NotNull Point point) void
writeBoolean
(boolean b) Writes a single boolean to the buffer.void
writeByte
(byte b) Writes a single byte to the buffer.void
writeByteArray
(byte[] array) <T> void
writeByteList
(Collection<T> list, @NotNull BiConsumer<BinaryWriter, T> consumer) void
writeBytes
(byte @NotNull [] bytes) Writes a byte array.void
writeChar
(char c) Writes a single char to the buffer.void
writeComponent
(@NotNull Component component) Writes a component to the buffer as a sized string.void
writeDouble
(double d) Writes a single double to the buffer.void
writeFloat
(float f) Writes a single float to the buffer.void
writeInt
(int i) Writes a single int to the buffer.void
writeItemStack
(@NotNull ItemStack itemStack) void
writeLong
(long l) Writes a single long to the buffer.void
writeLongArray
(long[] array) void
void
writeNullTerminatedString
(@NotNull String string, @NotNull Charset charset) Writes a null terminated string to the buffer.void
writeShort
(short s) Writes a single short to the buffer.void
writeSizedString
(@NotNull String string) Writes a string to the buffer.void
writeStringArray
(@NotNull String[] array) Writes a string to the buffer.void
writeUnsignedShort
(int yourShort) void
Writes anUUID
.void
writeVarInt
(int i) Writes a single var-int to the buffer.void
writeVarIntArray
(int[] array) Writes a var-int array to the buffer.<T> void
writeVarIntList
(Collection<T> list, @NotNull BiConsumer<BinaryWriter, T> consumer) void
writeVarLong
(long l) Writes a single var-long to the buffer.void
writeVarLongArray
(long[] array) Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write, write
-
Constructor Details
-
BinaryWriter
-
BinaryWriter
public BinaryWriter(int initialCapacity) -
BinaryWriter
public BinaryWriter()
-
-
Method Details
-
view
-
ensureSize
protected void ensureSize(int length) -
writeComponent
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
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 writecharset
- 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
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
Writes anUUID
. It is done by writing both long, the most and least significant bits.- Parameters:
uuid
- theUUID
to write
-
writeBlockPosition
-
writeBlockPosition
public void writeBlockPosition(int x, int y, int z) -
writeItemStack
-
writeNBT
public void writeNBT(@NotNull @NotNull String name, @NotNull @NotNull org.jglrxavpok.hephaistos.nbt.NBT tag) -
write
Writes the given writeable object into this writer.- Parameters:
writeable
- the object to write
-
write
-
write
-
writeArray
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
Adds aBinaryWriter
'sByteBuffer
at the beginning of this writer.- Parameters:
headerWriter
- theBinaryWriter
to add at the beginning
-
writeAtEnd
Adds aBinaryWriter
'sByteBuffer
at the end of this writer.- Parameters:
footerWriter
- theBinaryWriter
to add at the end
-
concat
-
getBuffer
Gets the raw buffer used by this binary writer.- Returns:
- the raw buffer
-
setBuffer
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 classOutputStream
-
writeUnsignedShort
public void writeUnsignedShort(int yourShort) -
makeArray
Returns a byte[] with the contents written via BinaryWriter
-