T
- the marshalled object's type.public abstract class Marshaller<T> extends Object
Marshalled
object. Marshallers should write some magic number at the
beginning of their byte array as a fail-safe check, so if, by mistake, an array of bytes that was
created by one marshaller is given the different marshaller to deserialize it fails quickly
and cleanly.
There is a number of built-in marshallers provided:
SERIALIZATION
marshaller uses Java Object Serialization to convert object to/from bytes.
Its type-safe version that also performs run-time type check is available via
serialization(Class)
method. The corresponding byte array always starts with two bytes
0xAC and 0xED or with ZLIB compression header.
forClasses(Class[])
and forTypes(String)
methods.
The corresponding byte array always starts with 0xE8 byte or with ZLIB compression header.
Modifier and Type | Class and Description |
---|---|
static class |
Marshaller.Typed<T>
Typed marshaller that marshals instances of a single type or an array of types.
|
Modifier and Type | Field and Description |
---|---|
static Marshaller<Object> |
SERIALIZATION
Converts object to/from bytes using Java Object Serialization.
|
Modifier | Constructor and Description |
---|---|
protected |
Marshaller() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
static <T> Marshaller.Typed<T> |
forClass(Class<? extends T> clazz)
Returns typed marshaller for the given class.
|
static <T> Marshaller.Typed<T[]> |
forClasses(Class<? extends T>... classes)
Returns typed marshaller for the given array of classes.
|
static Marshaller.Typed<?> |
forType(String type)
Returns typed marshaller for the given type name.
|
static Marshaller.Typed<Object[]> |
forTypes(String types)
Returns typed marshaller for the given comma-separated list of type names.
|
int |
hashCode()
Returns a hash code value for the object.
|
int |
readMarshalledLength(BufferedInput in)
Reads the length of an object in serialized form from the input according to this marshalling strategy.
|
abstract T |
readObjectFrom(BufferedInput in,
int length,
SerialClassContext serialContext)
Reads object representation according to this marshalling strategy from the specified input.
|
boolean |
representsNullObjectAsNullBytes()
Returns
true if this marshaller represents null object with null byte array. |
static Marshaller<Object> |
serialization()
Returns marshaller that converts object to/from bytes using Java Object Serialization.
|
static <T> Marshaller<T> |
serialization(Class<T> clazz)
Returns marshaller that converts object to/from bytes using Java Object Serialization.
|
boolean |
supportsNullObject()
|
void |
writeMarshalledLength(BufferedOutput out,
int length)
Writes the length of an object in serialized form to the output according to this marshalling strategy.
|
abstract void |
writeObjectTo(BufferedOutput out,
T object)
Writes byte representation of the object according to this marshalling strategy into
the specified output.
|
public static final Marshaller<Object> SERIALIZATION
IOUtil.objectToBytes
and
IOUtil.bytesToObject
methods. For a type-safe generic version of this marshaller,
use serialization(Class)
.public static <T> Marshaller<T> serialization(Class<T> clazz)
IOUtil.objectToBytes
and
IOUtil.bytesToObject
methods. It performs run-time check that serialized and deserialized object is of a specified type.
Serialization marshallers with different types produce the same byte arrays which are binary compatible.clazz
- the type of the object to serialize and to expect after deserialization.public static Marshaller<Object> serialization()
IOUtil.objectToBytes
,
IOUtil.bytesToObject
and
IOUtil.bytesToObject
methods. It performs run-time check that serialized and deserialized object is of a specified type.
Serialization marshallers with different types produce the same byte arrays which are binary compatible.public static <T> Marshaller.Typed<T> forClass(Class<? extends T> clazz)
IOUtil
.
Otherwise, Java Object Serialization is used.
Unlike serialization(Class)
serialization} marshallers, typed marshallers for different
types are not the same and are not compatible on a byte-array level even if the actual serialized objects
are the same. The same exact marshaller shall be used for writing and for reading.clazz
- the class.@SafeVarargs public static <T> Marshaller.Typed<T[]> forClasses(Class<? extends T>... classes)
IOUtil
.
Otherwise, Java Object Serialization is used.
Unlike serialization(Class)
serialization} marshallers, typed marshallers for different
types are not the same and are not compatible on a byte-array level even if the actual serialized objects
are the same. The same exact marshaller shall be used for writing and for reading.classes
- the array of classes.public static Marshaller.Typed<?> forType(String type)
IOUtil
.
Otherwise, Java Object Serialization is used.
Unlike serialization(Class)
serialization} marshallers, typed marshallers for different
types are not the same and are not compatible on a byte-array level even if the actual serialized objects
are the same. The same exact marshaller shall be used for writing and for reading.type
- the type name.public static Marshaller.Typed<Object[]> forTypes(String types)
IOUtil
.
Otherwise, Java Object Serialization is used.
Unlike serialization(Class)
serialization} marshallers, typed marshallers for different
types are not the same and are not compatible on a byte-array level even if the actual serialized objects
are the same. The same exact marshaller shall be used for writing and for reading.types
- the comma-separated list of type names.public void writeMarshalledLength(BufferedOutput out, int length) throws IOException
writeCompactInt
.out
- the destination to write to.length
- of an object in serialized form to be written or -1 when the object is null.IOException
- if an I/O error occurs.public int readMarshalledLength(BufferedInput in) throws IOException
readCompactInt
.in
- the source to read from.-1
if the marshalled object is null.IOException
- if an I/O error occurs.public abstract void writeObjectTo(BufferedOutput out, T object) throws IOException
readObjectFrom
can fail fast
when trying to read wrong representation that was created with different marshaller.out
- the output.object
- the object to be serialized.NullPointerException
- if object is null.IllegalArgumentException
- if this marshaller does not support the specified object.IOException
- if object cannot be serialized.public abstract T readObjectFrom(BufferedInput in, int length, SerialClassContext serialContext) throws IOException
in
- the input.length
- the length in bytes of object representation.serialContext
- the serial class context.IllegalArgumentException
- if length is negative.IOException
- if object cannot be deserialized.public boolean supportsNullObject()
true
if this marshaller support marshalling of null
objects in
writeObjectTo
method.
If this method returns false
, then
an attempt to create marshalled object with
Marshalled.forObject
(null, this)
throws
NullPointerException
.
This implementation returns true
.
true
if this marshaller support marshalling of null
objects inpublic boolean representsNullObjectAsNullBytes()
true
if this marshaller represents null
object with null
byte array.
This method is never called if supportsNullObject()
returns false
.
If this method returns true
, then writeObjectTo
is never invoked for null object.
This implementation returns true
.
true
if this marshaller represents null
object with null
byte array.public boolean equals(Object obj)
Copyright © 2002–2025 Devexperts LLC. All rights reserved.