org.helidb.lang.serializer
Class FixedSizeBigIntegerSerializer

java.lang.Object
  extended by org.helidb.lang.serializer.FixedSizeBigIntegerSerializer
All Implemented Interfaces:
Serializer<BigInteger>
Direct Known Subclasses:
FixedSizeBigIntegerNullSerializer

public class FixedSizeBigIntegerSerializer
extends Object
implements Serializer<BigInteger>

This is a Serializer for BigInteger values to byte arrays of a fixed size.

A value is serialized to a big-endian byte arrays using BigInteger 's constructor and toByteArray methods. Before the array containing the BigInteger data, the serialized value contains a size value of the BigInteger data (which may be less than the fixed size of this object). For fixed sizes of less than 256 bytes, one unsigned byte is used to represent the size. For sizes of 256 bytes or larger, a signed integer (four bytes) is used for the size.

The configured size of this object determines how big the serialized BigInteger:s can be. For an object size of of n bytes, where n is less than or equal to 256 bytes, the maximum BigInteger value is 2^((n - 1)*8 - 1) - 1 and the minimum value is -2^((n - 1)*8 - 1). For n values larger than 256, the maximum value is 2^((n - 4)*8 - 1) - 1 and the minimum value is -2^((n - 4)*8 - 1).

This serializer does not handle null values.

Since:
1.0
Author:
Karl Gustafsson
See Also:
BigInteger, FixedSizeBigIntegerNullSerializer, ConfigurableLengthLongSerializer
In_jar:
helidb-core

Constructor Summary
FixedSizeBigIntegerSerializer(int dataSize)
          Create a new Serializer.
 
Method Summary
protected  int getMaxBigIntSize()
          Get the maximum size of BigInteger data.
 int getSerializedSize()
          If the serialized data produced by this serializer always is of the same size, return that size (in number of bytes).
 BigInteger interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 BigInteger 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?
 BigInteger read(InputStream is, int dataSize)
          Read a value from the InputStream.
 BigInteger read(RandomAccess ra, int dataSize)
          Read a value from the RandomAccess' current position.
 BigInteger readBigInteger(InputStream is)
          Read a BigInteger value from the current position of the InputStream.
 BigInteger readBigInteger(RandomAccess ra)
          Read a BigInteger value from the current position of the RandomAccess.
 byte[] serialize(BigInteger val)
          Serialize the supplied object into a new byte array.
 int serialize(BigInteger val, byte[] barr, int offset)
          Serialize the supplied object into the byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FixedSizeBigIntegerSerializer

public FixedSizeBigIntegerSerializer(int dataSize)
                              throws IllegalArgumentException
Create a new Serializer.

Parameters:
dataSize - The size of serialized values in bytes. If the data size is 256 bytes or less, one unsigned byte is used to represent the size of the BigInteger byte array. If the data size is greater than 256 bytes, a total of five bytes is used to represent the size of the BigInteger byte array. The byte or bytes used for representing the data size are included in this parameter.
Throws:
IllegalArgumentException - If the size is less than two bytes.
Method Detail

getMaxBigIntSize

protected int getMaxBigIntSize()
Get the maximum size of BigInteger data.


getSerializedSize

public final int getSerializedSize()
Description copied from interface: Serializer
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.

Specified by:
getSerializedSize in interface Serializer<BigInteger>
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

public boolean isNullValuesPermitted()
Description copied from interface: Serializer
Does this serializer permit null values?

Specified by:
isNullValuesPermitted in interface Serializer<BigInteger>
Returns:
false, always.

interpret

public BigInteger interpret(byte[] barr,
                            int offset,
                            int length)
Description copied from interface: Serializer
Interpret the data in the byte array to an object of the type handled by the Serializer implementation.

Specified by:
interpret in interface Serializer<BigInteger>
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.
See Also:
Serializer.interpret(byte[])

interpret

public BigInteger interpret(byte[] barr)
Description copied from interface: Serializer
Interpret the data in the byte array to an object of the type handled by the Serializer implementation.

Specified by:
interpret in interface Serializer<BigInteger>
Parameters:
barr - The byte array containing the data.
Returns:
The interpreted object.
See Also:
Serializer.interpret(byte[], int, int)

serialize

public int serialize(BigInteger val,
                     byte[] barr,
                     int offset)
Description copied from interface: Serializer
Serialize the supplied object into the byte array.

Specified by:
serialize in interface Serializer<BigInteger>
Parameters:
val - 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.

serialize

public byte[] serialize(BigInteger val)
Description copied from interface: Serializer
Serialize the supplied object into a new byte array.

Specified by:
serialize in interface Serializer<BigInteger>
Parameters:
val - The object to serialize.
Returns:
The object serialized into a byte array.

readBigInteger

public BigInteger readBigInteger(RandomAccess ra)
                          throws NotEnoughDataException,
                                 WrappedIOException
Read a BigInteger value from the current position of the RandomAccess.

Parameters:
ra - The RandomAccess to read from. The current position of this is incremented by the number of bytes that are used to represent the BigInteger value.
Returns:
The BigInteger value.
Throws:
NotEnoughDataException - If enough data cannot be read.
WrappedIOException - On I/O errors.

readBigInteger

public BigInteger readBigInteger(InputStream is)
                          throws NotEnoughDataException,
                                 WrappedIOException
Read a BigInteger value from the current position of the InputStream.

Parameters:
is - The InputStream to read from. The current position of the stream is incremented by the number of bytes that are used to represent the BigInteger value.
Returns:
The BigInteger value.
Throws:
NotEnoughDataException - If enough data cannot be read.
WrappedIOException - If an IOException is encountered while reading data.

read

public BigInteger read(RandomAccess ra,
                       int dataSize)
Description copied from interface: Serializer
Read a value from the RandomAccess' current position. The position in the RandomAccess is incremented by size bytes.

Specified by:
read in interface Serializer<BigInteger>
Parameters:
ra - The RandomAccess to read from.
dataSize - The size of the value.
Returns:
The value.

read

public BigInteger read(InputStream is,
                       int dataSize)
Description copied from interface: Serializer
Read a value from the InputStream.

Specified by:
read in interface Serializer<BigInteger>
Parameters:
is - The InputStream to read from.
dataSize - The size of the value.
Returns:
The value.