Class BufferUtils

java.lang.Object
net.apartium.cocoabeans.utils.BufferUtils

@AvailableSince("0.0.46") public class BufferUtils extends Object
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 Type
    Method
    Description
    static long
    crc32(byte[] data)
    Computes the CRC32 checksum of the given byte array.
    static <T> byte[]
    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[]
    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>>
    T
    readEnum(DataInput in, Class<T> clazz, Supplier<T[]> values)
    Reads an enum value from the DataInput, using a fallback ordinal if the name is not found.
    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.
    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.
    static String
    Reads a UTF-8 string from the given DataInput.
    static String
    readString(DataInput in, int len)
    Reads a UTF-8 string of the specified length from the given DataInput.
    static int
    Reads an unsigned 16-bit integer (2 bytes) from the given DataInput.
    static int
    Reads an unsigned 24-bit integer (3 bytes) from the given DataInput.
    static long
    Reads an unsigned 32-bit integer (4 bytes) from the given DataInput.
    static long
    Reads 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 UUID
    toUUID(byte[] in)
    Converts a 16-byte array to a UUID.
    static <T extends Enum<T>>
    byte[]
    writeEnum(Enum<T> value)
    Serializes an enum value to a byte array (name as string, then ordinal as int).
    static <K extends Enum<K>>
    byte[]
    Serializes a map of enum keys to boolean values to a byte array.
    static <K extends Enum<K>, V extends Enum<V>>
    byte[]
    writeMapOfEnums(Map<K,V> map)
    Serializes a map of enum keys to enum values to a byte array.
    static byte[]
    Encodes a string as a byte array prefixed with its length as a 32-bit unsigned integer.
    static List<Byte>
    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).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • readU16

      public static int readU16(DataInput in) throws IOException
      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

      public static int readU24(DataInput in) throws IOException
      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

      public static long readU32(DataInput in) throws IOException
      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

      public static long readU64(DataInput in) throws IOException
      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

      public static UUID toUUID(byte[] in)
      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

      public static String readString(DataInput in) throws IOException
      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

      public static String readString(DataInput in, int len) throws IOException
      Reads a UTF-8 string of the specified length from the given DataInput.
      Parameters:
      in - the DataInput to read from
      len - 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

      public static byte[] writeString(String value)
      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

      public static List<Byte> writeStringAsList(String value)
      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 from
      clazz - the enum class
      values - a supplier for the enum values array
      Returns:
      the enum value
      Throws:
      IOException
    • writeEnum

      public static <T extends Enum<T>> byte[] writeEnum(Enum<T> value) throws IOException
      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 from
      keyType - the enum key class
      keyValues - a supplier for the enum key values array
      Returns:
      the map of enum keys to booleans
    • writeMapOfEnumBoolean

      public static <K extends Enum<K>> byte[] writeMapOfEnumBoolean(Map<K,Boolean> map)
      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 from
      keyType - the enum key class
      keyValues - a supplier for the enum key values array
      valueType - the enum value class
      valueValues - a supplier for the enum value values array
      Returns:
      the map of enum keys to enum values
    • writeMapOfEnums

      public static <K extends Enum<K>, V extends Enum<V>> byte[] writeMapOfEnums(Map<K,V> map)
      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

      public 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.
      Parameters:
      list - the list to create a mask for
      Returns:
      the occupancy mask as a byte array
    • createOccupancyMask

      public static <T> byte[] createOccupancyMask(List<T> list, Predicate<T> filter)
      Creates an occupancy mask (bitmask) for a list, using a custom filter.
      Parameters:
      list - the list to create a mask for
      filter - 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

      public static <T> byte[] createOccupancyMask(T[] arr, Predicate<T> filter)
      Creates an occupancy mask (bitmask) for an array, using a custom filter.
      Parameters:
      arr - the array to create a mask for
      filter - the filter to determine if an element is present
      Returns:
      the occupancy mask as a byte array