Class ByteOps

java.lang.Object
com.github.darksoulq.abyssallib.common.serialization.DynamicOps<byte[]>
com.github.darksoulq.abyssallib.common.serialization.ops.ByteOps

public class ByteOps extends DynamicOps<byte[]>
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 Details

    • INSTANCE

      public static final ByteOps INSTANCE
      The singleton instance of ByteOps.
  • Method Details

    • createString

      public byte[] createString(String value)
      Serializes a string as UTF-8 bytes with a 4-byte length prefix.
      Specified by:
      createString in class DynamicOps<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:
      createInt in class DynamicOps<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:
      createLong in class DynamicOps<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:
      createFloat in class DynamicOps<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:
      createDouble in class DynamicOps<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:
      createBoolean in class DynamicOps<byte[]>
      Parameters:
      value - The boolean value.
      Returns:
      An array containing 1 for true or 0 for false.
    • createList

      public byte[] createList(List<byte[]> elements)
      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:
      createList in class DynamicOps<byte[]>
      Parameters:
      elements - The list of serialized byte arrays.
      Returns:
      The combined byte array.
    • createMap

      public byte[] createMap(Map<byte[],byte[]> map)
      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:
      createMap in class DynamicOps<byte[]>
      Parameters:
      map - The map of serialized key-value pairs.
      Returns:
      The combined byte array.
    • getStringValue

      public Optional<String> getStringValue(byte[] input)
      Decodes a length-prefixed byte array into a String.
      Specified by:
      getStringValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing the decoded String, or empty if decoding fails.
    • getIntValue

      public Optional<Integer> getIntValue(byte[] input)
      Decodes a 4-byte array into an Integer.
      Specified by:
      getIntValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing the Integer.
    • getLongValue

      public Optional<Long> getLongValue(byte[] input)
      Decodes an 8-byte array into a Long.
      Specified by:
      getLongValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing the Long.
    • getFloatValue

      public Optional<Float> getFloatValue(byte[] input)
      Decodes a 4-byte array into a Float.
      Specified by:
      getFloatValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing the Float.
    • getDoubleValue

      public Optional<Double> getDoubleValue(byte[] input)
      Decodes an 8-byte array into a Double.
      Specified by:
      getDoubleValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing the Double.
    • getBooleanValue

      public Optional<Boolean> getBooleanValue(byte[] input)
      Decodes a 1-byte array into a Boolean.
      Specified by:
      getBooleanValue in class DynamicOps<byte[]>
      Parameters:
      input - The raw byte data.
      Returns:
      An Optional containing true if the byte is non-zero.
    • getList

      public Optional<List<byte[]>> getList(byte[] input)
      Decodes a combined byte array into a list of its constituent byte array elements.
      Specified by:
      getList in class DynamicOps<byte[]>
      Parameters:
      input - The raw binary list data.
      Returns:
      An Optional containing the list of byte arrays.
    • getMap

      public Optional<Map<byte[],byte[]>> getMap(byte[] input)
      Decodes a combined byte array into a map of its constituent key-value byte arrays.
      Specified by:
      getMap in class DynamicOps<byte[]>
      Parameters:
      input - The raw binary map data.
      Returns:
      An Optional containing the map.
    • empty

      public byte[] empty()
      Specified by:
      empty in class DynamicOps<byte[]>
      Returns:
      An empty 0-length byte array.