org.helidb.lang.serializer
Interface Serializer<T>

Type Parameters:
T - The type of objects serialized and interpreted by this Serializer.
All Known Implementing Classes:
BooleanSerializer, ByteSerializer, CharacterNullSerializer, CharacterSerializer, ConfigurableLengthIntegerNullSerializer, ConfigurableLengthIntegerSerializer, ConfigurableLengthLongNullSerializer, ConfigurableLengthLongSerializer, ConstantSizeStringNullSerializer, ConstantSizeStringSerializer, DoubleSerializer, FixedSizeBigIntegerNullSerializer, FixedSizeBigIntegerSerializer, FloatSerializer, IntegerNullSerializer, IntegerSerializer, LongNullSerializer, LongSerializer, SerializableSerializer, ShortNullSerializer, ShortSerializer, StringNullSerializer, StringSerializer, UnsignedByteSerializer

public interface Serializer<T>

Serializers are used by DatabaseBackend:s to serialize data when writing it to disk, and to interpret serialized data when reading it again. HeliDB comes with serializers for primitive datatypes and String:s. Database-using applications may also provide their own Serializer implementations for data types that are not supported by HeliDB out of the box.

Serializer implementations should not contain any mutable internal state. Object instances are reused many times over and may be used simultaneously by several execution threads.

An implementation decides if it permits null values and, in that case, how to serialize them. See isNullValuesPermitted().

Since:
1.0
Author:
Karl Gustafsson
In_jar:
helidb-core

Method Summary
 int getSerializedSize()
          If the serialized data produced by this serializer always is of the same size, return that size (in number of bytes).
 T interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 T interpret(byte[] barr, int offset, int length)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 boolean isNullValuesPermitted()
          Does this serializer permit null values?
 T read(InputStream is, int size)
          Read a value from the InputStream.
 T read(RandomAccess ra, int size)
          Read a value from the RandomAccess' current position.
 byte[] serialize(T o)
          Serialize the supplied object into a new byte array.
 int serialize(T o, byte[] barr, int offset)
          Serialize the supplied object into the byte array.
 

Method Detail

getSerializedSize

int getSerializedSize()
If the serialized data produced by this serializer always is of the same size, return that size (in number of bytes). Otherwise -1 is returned.

Returns:
The size of the serialized data in number of byte if it is always the same, or -1 if the size of the serialized data is variable.

isNullValuesPermitted

boolean isNullValuesPermitted()
Does this serializer permit null values?

Returns:
true if this serializer permits null values.

interpret

T interpret(byte[] barr)
            throws SerializationException,
                   ArrayIndexOutOfBoundsException
Interpret the data in the byte array to an object of the type handled by the Serializer implementation.

Parameters:
barr - The byte array containing the data.
Returns:
The interpreted object.
Throws:
SerializationException - If the data in the byte array cannot be interpreted.
ArrayIndexOutOfBoundsException - If the array is to small.
See Also:
interpret(byte[], int, int)

interpret

T interpret(byte[] barr,
            int offset,
            int length)
            throws SerializationException,
                   ArrayIndexOutOfBoundsException
Interpret the data in the byte array to an object of the type handled by the Serializer implementation.

Parameters:
barr - The byte array containing the data.
offset - The start position of the data.
length - The length of the data.
Returns:
The interpreted object.
Throws:
SerializationException - If the data in the byte array cannot be interpreted.
ArrayIndexOutOfBoundsException - If enough data cannot be read from the array at the supplied position.
See Also:
interpret(byte[])

serialize

byte[] serialize(T o)
                 throws NullPointerException,
                        SerializationException
Serialize the supplied object into a new byte array.

Parameters:
o - The object to serialize.
Returns:
The object serialized into a byte array.
Throws:
NullPointerException - If o is null and the Serializer implementation does not support null objects.
SerializationException - If o cannot be serialized.

serialize

int serialize(T o,
              byte[] barr,
              int offset)
              throws NullPointerException,
                     SerializationException,
                     ArrayIndexOutOfBoundsException
Serialize the supplied object into the byte array.

Parameters:
o - The object to serialize.
barr - The byte array to serialize it to.
offset - The start position to write data at in the byte array.
Returns:
The number of bytes written to the array.
Throws:
NullPointerException - If o is null and the Serializer implementation does not support null objects.
SerializationException - If o cannot be serialized or if the data does not fit into barr.
ArrayIndexOutOfBoundsException - If the array cannot accommodate all data.

read

T read(RandomAccess ra,
       int size)
       throws WrappedIOException,
              NotEnoughDataException,
              SerializationException
Read a value from the RandomAccess' current position. The position in the RandomAccess is incremented by size bytes.

Parameters:
ra - The RandomAccess to read from.
size - The size of the value.
Returns:
The value.
Throws:
WrappedIOException - On I/O errors.
NotEnoughDataException - If there is not enough data left in the RandomAccess.
SerializationException - If the supplied data size is invalid.

read

T read(InputStream is,
       int size)
       throws WrappedIOException,
              NotEnoughDataException,
              SerializationException
Read a value from the InputStream.

Parameters:
is - The InputStream to read from.
size - The size of the value.
Returns:
The value.
Throws:
WrappedIOException - On I/O errors.
NotEnoughDataException - If there is not enough data left to read from the InputStream.
SerializationException - If the supplied data size is invalid.