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[]>
A DynamicOps implementation that stores serialized values as raw byte arrays.

Values are encoded using a simple binary format. Variable-length structures such as strings, lists, and maps are prefixed with their length or entry count, while primitive numeric values are stored using their standard binary representation.

  • Field Details

    • INSTANCE

      public static final ByteOps INSTANCE
      Shared singleton instance.
  • Method Details

    • compressMaps

      public boolean compressMaps()
      Indicates that this format benefits from compressed map encoding.
      Overrides:
      compressMaps in class DynamicOps<byte[]>
      Returns:
      true
    • createString

      public byte[] createString(String value)
      Encodes a string as UTF-8 bytes prefixed by its length.
      Specified by:
      createString in class DynamicOps<byte[]>
      Parameters:
      value - the string to encode
      Returns:
      the encoded byte array
    • createByte

      public byte[] createByte(byte value)
      Encodes a byte value.
      Specified by:
      createByte in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createShort

      public byte[] createShort(short value)
      Encodes a short value using big-endian byte order.
      Specified by:
      createShort in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createInt

      public byte[] createInt(int value)
      Encodes an integer value using big-endian byte order.
      Specified by:
      createInt in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createLong

      public byte[] createLong(long value)
      Encodes a long value using big-endian byte order.
      Specified by:
      createLong in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createFloat

      public byte[] createFloat(float value)
      Encodes a float value.
      Specified by:
      createFloat in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createDouble

      public byte[] createDouble(double value)
      Encodes a double value.
      Specified by:
      createDouble in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      the encoded byte array
    • createBoolean

      public byte[] createBoolean(boolean value)
      Encodes a boolean value.
      Specified by:
      createBoolean in class DynamicOps<byte[]>
      Parameters:
      value - the value to encode
      Returns:
      a single-byte representation of the boolean
    • createList

      public byte[] createList(List<byte[]> elements)
      Encodes a list of serialized values.

      The resulting format stores the element count followed by each element prefixed with its length.

      Specified by:
      createList in class DynamicOps<byte[]>
      Parameters:
      elements - the serialized list elements
      Returns:
      the encoded list
    • createMap

      public byte[] createMap(Map<byte[],byte[]> map)
      Encodes a map of serialized key-value pairs.

      The resulting format stores the entry count followed by each key and value prefixed with its length.

      Specified by:
      createMap in class DynamicOps<byte[]>
      Parameters:
      map - the serialized map entries
      Returns:
      the encoded map
    • getStringValue

      public Optional<String> getStringValue(byte[] input)
      Attempts to decode a UTF-8 string from a length-prefixed byte array.
      Specified by:
      getStringValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded string, if the input is valid
    • getNumberValue

      public Optional<Number> getNumberValue(byte[] input)
      Attempts to decode a numeric value based on the size of the byte array.
      Specified by:
      getNumberValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded number, if the input matches a supported numeric size
    • getIntValue

      public Optional<Integer> getIntValue(byte[] input)
      Attempts to decode an integer value.
      Specified by:
      getIntValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded integer, if the input contains a valid integer
    • getLongValue

      public Optional<Long> getLongValue(byte[] input)
      Attempts to decode a long value.
      Specified by:
      getLongValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded long, if the input contains a valid long
    • getFloatValue

      public Optional<Float> getFloatValue(byte[] input)
      Attempts to decode a float value.
      Specified by:
      getFloatValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded float, if the input contains a valid float
    • getDoubleValue

      public Optional<Double> getDoubleValue(byte[] input)
      Attempts to decode a double value.
      Specified by:
      getDoubleValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded double, if the input contains a valid double
    • getBooleanValue

      public Optional<Boolean> getBooleanValue(byte[] input)
      Attempts to decode a boolean value.
      Specified by:
      getBooleanValue in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded boolean, if the input contains a valid boolean
    • getList

      public Optional<List<byte[]>> getList(byte[] input)
      Attempts to decode a serialized list.
      Specified by:
      getList in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded list, if the input contains a valid list
    • getMap

      public Optional<Map<byte[],byte[]>> getMap(byte[] input)
      Attempts to decode a serialized map.
      Specified by:
      getMap in class DynamicOps<byte[]>
      Parameters:
      input - the encoded byte array
      Returns:
      the decoded map, if the input contains a valid map
    • getKeys

      public Optional<Iterable<String>> getKeys(byte[] input)
      Returns the decoded string keys contained in a serialized map.
      Specified by:
      getKeys in class DynamicOps<byte[]>
      Parameters:
      input - the encoded map value
      Returns:
      the decoded keys, if the input contains a valid map
    • size

      public OptionalInt size(byte[] input)
      Returns the number of entries contained in a serialized list or map.
      Specified by:
      size in class DynamicOps<byte[]>
      Parameters:
      input - the encoded value
      Returns:
      the collection size, if the value is a valid list or map
    • copy

      public byte[] copy(byte[] input)
      Creates a copy of the provided byte array.
      Specified by:
      copy in class DynamicOps<byte[]>
      Parameters:
      input - the array to copy
      Returns:
      a new array containing the same bytes
    • empty

      public byte[] empty()
      Returns the empty value representation for this format.
      Specified by:
      empty in class DynamicOps<byte[]>
      Returns:
      an empty byte array