Class ByteOps
java.lang.Object
com.github.darksoulq.abyssallib.common.serialization.DynamicOps<byte[]>
com.github.darksoulq.abyssallib.common.serialization.ops.ByteOps
An implementation of
DynamicOps that serializes data into raw byte arrays.
This format uses a binary protocol where variable-length data (Strings, Lists, Maps) is prefixed with a 4-byte integer indicating the length or size, followed by the raw data. Fixed-size primitives use their standard IEEE 754 or two's complement binary representations.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbyte[]createBoolean(boolean value) Serializes a boolean into a 1-byte array.byte[]createDouble(double value) Serializes a double into an 8-byte array.byte[]createFloat(float value) Serializes a float into a 4-byte array.byte[]createInt(int value) Serializes an integer into a 4-byte array (Big-Endian).byte[]createList(List<byte[]> elements) Serializes a list of byte arrays into a single contiguous byte array.byte[]createLong(long value) Serializes a long into an 8-byte array (Big-Endian).byte[]Serializes a map of byte arrays into a single contiguous byte array.byte[]createString(String value) Serializes a string as UTF-8 bytes with a 4-byte length prefix.byte[]empty()getBooleanValue(byte[] input) Decodes a 1-byte array into a Boolean.getDoubleValue(byte[] input) Decodes an 8-byte array into a Double.getFloatValue(byte[] input) Decodes a 4-byte array into a Float.getIntValue(byte[] input) Decodes a 4-byte array into an Integer.getList(byte[] input) Decodes a combined byte array into a list of its constituent byte array elements.getLongValue(byte[] input) Decodes an 8-byte array into a Long.getMap(byte[] input) Decodes a combined byte array into a map of its constituent key-value byte arrays.getStringValue(byte[] input) Decodes a length-prefixed byte array into a String.
-
Field Details
-
INSTANCE
The singleton instance of ByteOps.
-
-
Method Details
-
createString
Serializes a string as UTF-8 bytes with a 4-byte length prefix.- Specified by:
createStringin classDynamicOps<byte[]>- Parameters:
value- The string to serialize.- Returns:
- A byte array containing [length(4 bytes)][data(n bytes)].
-
createInt
public byte[] createInt(int value) Serializes an integer into a 4-byte array (Big-Endian).- Specified by:
createIntin classDynamicOps<byte[]>- Parameters:
value- The integer value.- Returns:
- A 4-byte array.
-
createLong
public byte[] createLong(long value) Serializes a long into an 8-byte array (Big-Endian).- Specified by:
createLongin classDynamicOps<byte[]>- Parameters:
value- The long value.- Returns:
- An 8-byte array.
-
createFloat
public byte[] createFloat(float value) Serializes a float into a 4-byte array.- Specified by:
createFloatin classDynamicOps<byte[]>- Parameters:
value- The float value.- Returns:
- A 4-byte array.
-
createDouble
public byte[] createDouble(double value) Serializes a double into an 8-byte array.- Specified by:
createDoublein classDynamicOps<byte[]>- Parameters:
value- The double value.- Returns:
- An 8-byte array.
-
createBoolean
public byte[] createBoolean(boolean value) Serializes a boolean into a 1-byte array.- Specified by:
createBooleanin classDynamicOps<byte[]>- Parameters:
value- The boolean value.- Returns:
- An array containing
1for true or0for false.
-
createList
Serializes a list of byte arrays into a single contiguous byte array. Format: [List Size(4)][Elem1 Length(4)][Elem1 Data...][Elem2 Length(4)][Elem2 Data...]- Specified by:
createListin classDynamicOps<byte[]>- Parameters:
elements- The list of serialized byte arrays.- Returns:
- The combined byte array.
-
createMap
Serializes a map of byte arrays into a single contiguous byte array. Format: [Map Size(4)][Key1 Len(4)][Key1 Data...][Val1 Len(4)][Val1 Data...]- Specified by:
createMapin classDynamicOps<byte[]>- Parameters:
map- The map of serialized key-value pairs.- Returns:
- The combined byte array.
-
getStringValue
Decodes a length-prefixed byte array into a String.- Specified by:
getStringValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing the decoded String, or empty if decoding fails.
-
getIntValue
Decodes a 4-byte array into an Integer.- Specified by:
getIntValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing the Integer.
-
getLongValue
Decodes an 8-byte array into a Long.- Specified by:
getLongValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing the Long.
-
getFloatValue
Decodes a 4-byte array into a Float.- Specified by:
getFloatValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing the Float.
-
getDoubleValue
Decodes an 8-byte array into a Double.- Specified by:
getDoubleValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing the Double.
-
getBooleanValue
Decodes a 1-byte array into a Boolean.- Specified by:
getBooleanValuein classDynamicOps<byte[]>- Parameters:
input- The raw byte data.- Returns:
- An Optional containing true if the byte is non-zero.
-
getList
Decodes a combined byte array into a list of its constituent byte array elements.- Specified by:
getListin classDynamicOps<byte[]>- Parameters:
input- The raw binary list data.- Returns:
- An Optional containing the list of byte arrays.
-
getMap
Decodes a combined byte array into a map of its constituent key-value byte arrays.- Specified by:
getMapin classDynamicOps<byte[]>- Parameters:
input- The raw binary map data.- Returns:
- An Optional containing the map.
-
empty
public byte[] empty()- Specified by:
emptyin classDynamicOps<byte[]>- Returns:
- An empty 0-length byte array.
-