Interface SerializationContextRead
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 TypeMethodDescriptionbooleanReads a boolean (1 byte).bytereadByte()Reads a single byte.voidreadBytes(byte[] b) Reads bytes into an existing array.voidreadBytes(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.doubleReads a double (8 bytes).floatReads a float (4 bytes).intreadInt()Reads an int (4 bytes).longreadLong()Reads a long (8 bytes).<T> TReads an object with automatic type identification.shortReads a short (2 bytes).readUTF()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
nullif 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
-
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.lengthbytes 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 intooff- The starting offset in the arraylen- The number of bytes to read
-