Interface SerializationContextRead


public sealed interface SerializationContextRead
Context for reading objects and primitives during deserialization.

This context is provided to SingleBinarySerializer.read(SerializationContextRead, byte) and offers methods to read primitives and nested objects from the input stream.

Reading Primitives

Use the provided read methods for primitive types and strings, in the same order they were written:

@Override
public MyObject read(SerializationContextRead ctx, byte version) {
    byte byteValue = ctx.readByte();
    short shortValue = ctx.readShort();
    int intValue = ctx.readInt();
    long longValue = ctx.readLong();
    boolean booleanValue = ctx.readBoolean();
    float floatValue = ctx.readFloat();
    double doubleValue = ctx.readDouble();
    String stringValue = ctx.readUTF();
    byte[] byteArray = ctx.readBytes(intValue);  // Assuming intValue is length

    return new MyObject(byteValue, shortValue, intValue, longValue,
                       booleanValue, floatValue, doubleValue, stringValue, byteArray);
}

Reading Nested Objects

Use readObject() to read nested objects. The framework will automatically determine the object's type and invoke the appropriate deserializer:

@Override
public Order read(SerializationContextRead ctx, byte version) {
    Person customer = ctx.readObject();  // Automatically deserializes Person
    Address address = ctx.readObject();  // Automatically deserializes Address
    double total = ctx.readDouble();
    return new Order(customer, address, total);
}

Null Values

readObject() may return null if a null value was written:

@Override
public Person read(SerializationContextRead ctx, byte version) {
    String name = ctx.readUTF();
    Address address = ctx.readObject();  // May be null
    return new Person(name, address);
}

Byte Arrays

For byte arrays, read the length first, then the bytes:

@Override
public MyObject read(SerializationContextRead ctx, byte version) {
    int length = ctx.readInt();  // Read length prefix
    byte[] data = ctx.readBytes(length);  // Read the bytes
    return new MyObject(data);
}

Alternatively, read into an existing array:

byte[] buffer = new byte[1024];
ctx.readBytes(buffer);  // Reads exactly 1024 bytes

Read Order

Critical: The order in which you read fields must exactly match the order in which they were written in SingleBinarySerializer.write(SerializationContextWrite, Object).

// Write order:
ctx.writeUTF(name);
ctx.writeInt(age);

// Read order MUST match:
String name = ctx.readUTF();
int age = ctx.readInt();
Since:
2.0
Author:
José Bolina
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Reads a boolean (1 byte).
    byte
    Reads a single byte.
    void
    readBytes(byte[] b)
    Reads bytes into an existing array.
    void
    readBytes(byte[] b, int off, int len)
    Reads bytes into a portion of an existing array.
    byte[]
    readBytes(int length)
    Reads bytes into a new array.
    double
    Reads a double (8 bytes).
    float
    Reads a float (4 bytes).
    int
    Reads an int (4 bytes).
    long
    Reads a long (8 bytes).
    <T> T
    Reads an object with automatic type identification.
    short
    Reads a short (2 bytes).
    Reads a UTF-8 encoded string with length prefix.
  • Method Details

    • readObject

      <T> T readObject()
      Reads an object with automatic type identification.

      The object's type ID is read, the appropriate deserializer is determined, and the object is deserialized. May return null if a null value was written.

      Type Parameters:
      T - The expected type
      Returns:
      The deserialized object (can be null)
      Throws:
      IllegalStateException - if no serializer is registered for the type ID
    • readByte

      byte readByte()
      Reads a single byte.
      Returns:
      The byte value
    • readShort

      short readShort()
      Reads a short (2 bytes).
      Returns:
      The short value
    • readInt

      int readInt()
      Reads an int (4 bytes).
      Returns:
      The int value
    • readLong

      long readLong()
      Reads a long (8 bytes).
      Returns:
      The long value
    • readBoolean

      boolean readBoolean()
      Reads a boolean (1 byte).
      Returns:
      The boolean value
    • readFloat

      float readFloat()
      Reads a float (4 bytes).
      Returns:
      The float value
    • readDouble

      double readDouble()
      Reads a double (8 bytes).
      Returns:
      The double value
    • readUTF

      String readUTF()
      Reads a UTF-8 encoded string with length prefix.
      Returns:
      The string value
    • readBytes

      byte[] readBytes(int length)
      Reads bytes into a new array.
      Parameters:
      length - The number of bytes to read
      Returns:
      A new byte array containing the read bytes
    • readBytes

      void readBytes(byte[] b)
      Reads bytes into an existing array.

      Reads exactly b.length bytes from the stream.

      Parameters:
      b - The array to read into
    • readBytes

      void readBytes(byte[] b, int off, int len)
      Reads bytes into a portion of an existing array.
      Parameters:
      b - The array to read into
      off - The starting offset in the array
      len - The number of bytes to read