Package net.apartium.cocoabeans.utils
Class BufferUtils
java.lang.Object
net.apartium.cocoabeans.utils.BufferUtils
Utility class for binary data, encoding, and serialization operations.
Provides static helper methods for reading and writing unsigned integers, strings, enums, UUIDs, and for creating occupancy masks, CRC32 checksums, and boolean arrays from bitmasks. These methods are useful for binary protocols, serialization, and data manipulation, but do not perform file I/O.
Typical use cases include working with network buffers, binary serialization formats, and low-level data manipulation where precise control over byte representation is required.
- Since:
- 0.0.46
-
Method Summary
Modifier and TypeMethodDescriptionstatic longcrc32(byte[] data) Computes the CRC32 checksum of the given byte array.static <T> byte[]createOccupancyMask(List<T> list) Creates an occupancy mask (bitmask) for a list, where each bit represents whether the element passes the filter.static <T> byte[]createOccupancyMask(List<T> list, Predicate<T> filter) Creates an occupancy mask (bitmask) for a list, using a custom filter.static <T> byte[]createOccupancyMask(T[] arr) Creates an occupancy mask (bitmask) for an array, where each bit represents whether the element passes the filter.static <T> byte[]createOccupancyMask(T[] arr, Predicate<T> filter) Creates an occupancy mask (bitmask) for an array, using a custom filter.static <T extends Enum<T>>
TReads an enum value from the DataInput, using a fallback ordinal if the name is not found.readMapOfEnumBoolean(DataInputStream in, Class<K> keyType, Supplier<K[]> keyValues) Reads a map of enum keys to boolean values from a DataInputStream.readMapOfEnums(DataInputStream in, Class<K> keyType, Supplier<K[]> keyValues, Class<V> valueType, Supplier<V[]> valueValues) Reads a map of enum keys to enum values from a DataInputStream.static StringreadString(DataInput in) Reads a UTF-8 string from the given DataInput.static StringreadString(DataInput in, int len) Reads a UTF-8 string of the specified length from the given DataInput.static intReads an unsigned 16-bit integer (2 bytes) from the given DataInput.static intReads an unsigned 24-bit integer (3 bytes) from the given DataInput.static longReads an unsigned 32-bit integer (4 bytes) from the given DataInput.static longReads an unsigned 64-bit integer (8 bytes) from the given DataInput.static boolean[]toBooleanArray(long data) Converts a long value to a boolean array representing its bits.static UUIDtoUUID(byte[] in) Converts a 16-byte array to a UUID.static <T extends Enum<T>>
byte[]Serializes an enum value to a byte array (name as string, then ordinal as int).static <K extends Enum<K>>
byte[]writeMapOfEnumBoolean(Map<K, Boolean> map) Serializes a map of enum keys to boolean values to a byte array.writeMapOfEnums(Map<K, V> map) Serializes a map of enum keys to enum values to a byte array.static byte[]writeString(String value) Encodes a string as a byte array prefixed with its length as a 32-bit unsigned integer.writeStringAsList(String value) Encodes a string as a List of bytes, prefixed with its length as a 32-bit unsigned integer.static byte[]writeU16(int value) Writes a 16-bit unsigned integer to a byte array (big-endian).static byte[]writeU24(int value) Writes a 24-bit unsigned integer to a byte array (big-endian).static byte[]writeU32(int value) Writes a 32-bit unsigned integer to a byte array (big-endian).static byte[]writeU64(long value) Writes a 64-bit unsigned integer to a byte array (big-endian).
-
Method Details
-
readU16
Reads an unsigned 16-bit integer (2 bytes) from the given DataInput.- Parameters:
in- the DataInput to read from- Returns:
- the unsigned 16-bit integer as an int
- Throws:
IOException- if an I/O error occurs
-
readU24
Reads an unsigned 24-bit integer (3 bytes) from the given DataInput.- Parameters:
in- the DataInput to read from- Returns:
- the unsigned 24-bit integer as an int
- Throws:
IOException- if an I/O error occurs
-
readU32
Reads an unsigned 32-bit integer (4 bytes) from the given DataInput.- Parameters:
in- the DataInput to read from- Returns:
- the unsigned 32-bit integer as a long
- Throws:
IOException- if an I/O error occurs
-
readU64
Reads an unsigned 64-bit integer (8 bytes) from the given DataInput.- Parameters:
in- the DataInput to read from- Returns:
- the unsigned 64-bit integer as a long
- Throws:
IOException- if an I/O error occurs
-
toUUID
Converts a 16-byte array to a UUID.- Parameters:
in- the byte array (must be 16 bytes)- Returns:
- the UUID
- Throws:
IllegalArgumentException- if the array is not 16 bytes
-
readString
Reads a UTF-8 string from the given DataInput. The string is prefixed with its length as an unsigned 32-bit integer.- Parameters:
in- the DataInput to read from- Returns:
- the decoded string
- Throws:
IOException- if an I/O error occurs
-
readString
Reads a UTF-8 string of the specified length from the given DataInput.- Parameters:
in- the DataInput to read fromlen- the length of the string in bytes- Returns:
- the decoded string
- Throws:
IOException- if an I/O error occurs
-
writeU64
public static byte[] writeU64(long value) Writes a 64-bit unsigned integer to a byte array (big-endian).- Parameters:
value- the value to write- Returns:
- the byte array representing the value
-
writeU32
public static byte[] writeU32(int value) Writes a 32-bit unsigned integer to a byte array (big-endian).- Parameters:
value- the value to write- Returns:
- the byte array representing the value
-
writeU24
public static byte[] writeU24(int value) Writes a 24-bit unsigned integer to a byte array (big-endian).- Parameters:
value- the value to write- Returns:
- the byte array representing the value
-
writeU16
public static byte[] writeU16(int value) Writes a 16-bit unsigned integer to a byte array (big-endian).- Parameters:
value- the value to write- Returns:
- the byte array representing the value
-
writeString
Encodes a string as a byte array prefixed with its length as a 32-bit unsigned integer.- Parameters:
value- the string to encode- Returns:
- the byte array containing the length and UTF-8 encoded string
-
writeStringAsList
Encodes a string as a List of bytes, prefixed with its length as a 32-bit unsigned integer.- Parameters:
value- the string to encode- Returns:
- the list of bytes containing the length and UTF-8 encoded string
-
crc32
public static long crc32(byte[] data) Computes the CRC32 checksum of the given byte array.- Parameters:
data- the data to checksum- Returns:
- the CRC32 checksum value
-
toBooleanArray
public static boolean[] toBooleanArray(long data) Converts a long value to a boolean array representing its bits.- Parameters:
data- the long value- Returns:
- a boolean array of length 64, where each element represents a bit
-
readEnum
public static <T extends Enum<T>> T readEnum(DataInput in, Class<T> clazz, Supplier<T[]> values) throws IOException Reads an enum value from the DataInput, using a fallback ordinal if the name is not found.- Parameters:
in- the DataInput to read fromclazz- the enum classvalues- a supplier for the enum values array- Returns:
- the enum value
- Throws:
IOException
-
writeEnum
Serializes an enum value to a byte array (name as string, then ordinal as int).- Parameters:
value- the enum value- Returns:
- the byte array representing the enum
- Throws:
IOException
-
readMapOfEnumBoolean
public static <K extends Enum<K>> Map<K,Boolean> readMapOfEnumBoolean(DataInputStream in, Class<K> keyType, Supplier<K[]> keyValues) Reads a map of enum keys to boolean values from a DataInputStream.- Parameters:
in- the DataInputStream to read fromkeyType- the enum key classkeyValues- a supplier for the enum key values array- Returns:
- the map of enum keys to booleans
-
writeMapOfEnumBoolean
Serializes a map of enum keys to boolean values to a byte array.- Parameters:
map- the map to serialize- Returns:
- the byte array representing the map
-
readMapOfEnums
public static <K extends Enum<K>,V extends Enum<V>> Map<K,V> readMapOfEnums(DataInputStream in, Class<K> keyType, Supplier<K[]> keyValues, Class<V> valueType, Supplier<V[]> valueValues) Reads a map of enum keys to enum values from a DataInputStream.- Parameters:
in- the DataInputStream to read fromkeyType- the enum key classkeyValues- a supplier for the enum key values arrayvalueType- the enum value classvalueValues- a supplier for the enum value values array- Returns:
- the map of enum keys to enum values
-
writeMapOfEnums
Serializes a map of enum keys to enum values to a byte array.- Parameters:
map- the map to serialize- Returns:
- the byte array representing the map
-
createOccupancyMask
Creates an occupancy mask (bitmask) for a list, where each bit represents whether the element passes the filter.- Parameters:
list- the list to create a mask for- Returns:
- the occupancy mask as a byte array
-
createOccupancyMask
Creates an occupancy mask (bitmask) for a list, using a custom filter.- Parameters:
list- the list to create a mask forfilter- the filter to determine if an element is present- Returns:
- the occupancy mask as a byte array
-
createOccupancyMask
public static <T> byte[] createOccupancyMask(T[] arr) Creates an occupancy mask (bitmask) for an array, where each bit represents whether the element passes the filter.- Parameters:
arr- the array to create a mask for- Returns:
- the occupancy mask as a byte array
-
createOccupancyMask
Creates an occupancy mask (bitmask) for an array, using a custom filter.- Parameters:
arr- the array to create a mask forfilter- the filter to determine if an element is present- Returns:
- the occupancy mask as a byte array
-