Package wtf.choco.veinminer.network
Class PluginMessageByteBuffer
java.lang.Object
wtf.choco.veinminer.network.PluginMessageByteBuffer
A utility class to allow for reading and writing of complex types to/from a byte array.
-
Constructor Summary
ConstructorDescriptionConstruct a newPluginMessageByteBuffer
.PluginMessageByteBuffer
(byte[] data) Construct a newPluginMessageByteBuffer
wrapping a byte array.PluginMessageByteBuffer
(@NotNull ByteBuffer buffer) Construct a newPluginMessageByteBuffer
wrapping aByteBuffer
. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
Get this byte buffer as a byte array (for writing).@NotNull BlockPosition
Read aBlockPosition
.boolean
Read a boolean primitive.byte
readByte()
Read a raw byte.byte[]
Read an array of bytes prefixed by a variable-length int.byte[]
Read the remaining bytes in this buffer as an array.void
readBytes
(byte[] destination) Read a set amount of bytes from this buffer into a destination array.byte[]
readBytes
(int size) Read a set amount of bytes from this buffer as an array.int
readInt()
Read a 4 byte integer.long
readLong()
Read an 8 byte long.@NotNull NamespacedKey
Read aNamespacedKey
.@NotNull String
Read a UTF-8 String.int
Read a variable-length integer.long
Read a variable-length long.void
writeBlockPosition
(@NotNull BlockPosition position) Write aBlockPosition
.void
writeBoolean
(boolean value) Write a boolean primitive.void
writeByte
(byte value) Write a raw byte.void
writeByte
(int value) Write a raw byte.void
writeByteArray
(byte[] bytes) Write an array of bytes prefixed by a variable-length int.void
writeBytes
(byte[] bytes) Write an array of bytes.void
writeInt
(int value) Write a 4 byte integer.void
writeLong
(long value) Write an 8 byte long.void
writeNamespacedKey
(@NotNull NamespacedKey key) Write aNamespacedKey
.void
writeString
(@NotNull String string) Write a UTF-8 String.void
writeVarInt
(int value) Write a variable-length integer.void
writeVarLong
(long value) Write a variable-length long.
-
Constructor Details
-
PluginMessageByteBuffer
Construct a newPluginMessageByteBuffer
wrapping aByteBuffer
. Intended for reading data.- Parameters:
buffer
- the buffer to wrap
-
PluginMessageByteBuffer
public PluginMessageByteBuffer(byte[] data) Construct a newPluginMessageByteBuffer
wrapping a byte array. Intended for reading data.- Parameters:
data
- the data to wrap
-
PluginMessageByteBuffer
public PluginMessageByteBuffer()Construct a newPluginMessageByteBuffer
. Intended for writing data.
-
-
Method Details
-
writeInt
public void writeInt(int value) Write a 4 byte integer.- Parameters:
value
- the value to write
-
readInt
public int readInt()Read a 4 byte integer.- Returns:
- the read value
-
writeVarInt
public void writeVarInt(int value) Write a variable-length integer.- Parameters:
value
- the value to write
-
readVarInt
public int readVarInt()Read a variable-length integer.- Returns:
- the read value
- Throws:
IllegalStateException
- if the var int is too large
-
writeLong
public void writeLong(long value) Write an 8 byte long.- Parameters:
value
- the value to write
-
readLong
public long readLong()Read an 8 byte long.- Returns:
- the read value
-
writeVarLong
public void writeVarLong(long value) Write a variable-length long.- Parameters:
value
- the value to write
-
readVarLong
public long readVarLong()Read a variable-length long.- Returns:
- the read value
- Throws:
IllegalStateException
- if the var long is too large
-
writeBoolean
public void writeBoolean(boolean value) Write a boolean primitive.- Parameters:
value
- the value to write
-
readBoolean
public boolean readBoolean()Read a boolean primitive.- Returns:
- the read value
-
writeString
Write a UTF-8 String.- Parameters:
string
- the string to write
-
readString
Read a UTF-8 String.- Returns:
- the string
-
writeBytes
public void writeBytes(byte[] bytes) Write an array of bytes.- Parameters:
bytes
- the bytes to write
-
writeByteArray
public void writeByteArray(byte[] bytes) Write an array of bytes prefixed by a variable-length int.- Parameters:
bytes
- the bytes to write
-
readByteArray
public byte[] readByteArray()Read an array of bytes prefixed by a variable-length int.- Returns:
- the byte array
-
readBytes
public byte[] readBytes()Read the remaining bytes in this buffer as an array.- Returns:
- the bytes
-
readBytes
public byte[] readBytes(int size) Read a set amount of bytes from this buffer as an array.- Parameters:
size
- the amount of bytes to read- Returns:
- the bytes
-
readBytes
public void readBytes(byte[] destination) Read a set amount of bytes from this buffer into a destination array. This method will read up todestination.length
bytes.- Parameters:
destination
- the array in which the bytes should be written
-
writeByte
public void writeByte(byte value) Write a raw byte.- Parameters:
value
- the value to write
-
writeByte
public void writeByte(int value) Write a raw byte.- Parameters:
value
- the value to write
-
readByte
public byte readByte()Read a raw byte.- Returns:
- the byte
-
writeBlockPosition
Write aBlockPosition
.- Parameters:
position
- the position to write
-
readBlockPosition
Read aBlockPosition
.- Returns:
- the BlockPosition
-
writeNamespacedKey
Write aNamespacedKey
.- Parameters:
key
- the key to write
-
readNamespacedKey
Read aNamespacedKey
.- Returns:
- the NamespacedKey
-
asByteArray
public byte[] asByteArray()Get this byte buffer as a byte array (for writing).- Returns:
- the byte array
-