org.helidb.lang.serializer
Class FixedSizeBigIntegerNullSerializer

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

public class FixedSizeBigIntegerNullSerializer
extends FixedSizeBigIntegerSerializer

This Serializer serializes BigInteger values to a big-endian byte array of configurable size using BigInteger's constructor and toByteArray methods.

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 uses a special, configurable value to represent null BigInteger:s.

Since:
1.0
Author:
Karl Gustafsson
See Also:
FixedSizeBigIntegerSerializer, ConfigurableLengthLongNullSerializer, BigInteger
In_jar:
helidb-core

Constructor Summary
FixedSizeBigIntegerNullSerializer(int dataSize)
          Create a new Serializer.
FixedSizeBigIntegerNullSerializer(int dataSize, BigInteger nullValue)
          Create a new Serializer.
 
Method Summary
 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?
 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 org.helidb.lang.serializer.FixedSizeBigIntegerSerializer
getMaxBigIntSize, getSerializedSize, read, read, readBigInteger, readBigInteger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FixedSizeBigIntegerNullSerializer

public FixedSizeBigIntegerNullSerializer(int dataSize)
                                  throws IllegalArgumentException
Create a new Serializer. The special null value is the lowest value allowed with the given data size - -2^(dataSize*8 - 1). For instance, if the data size is two bytes, the null value will be -32768. That null value has the most significant bit of the most significant byte set, which may be important to know when working with Hasher:s.

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 data size is less than one byte.

FixedSizeBigIntegerNullSerializer

public FixedSizeBigIntegerNullSerializer(int dataSize,
                                         BigInteger nullValue)
                                  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.
nullValue - The BigInteger value used to represent null values.
Throws:
IllegalArgumentException - If the data size is less than one byte or if the null value cannot be serialized to a byte array with a permitted size.
Method Detail

isNullValuesPermitted

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

Specified by:
isNullValuesPermitted in interface Serializer<BigInteger>
Overrides:
isNullValuesPermitted in class FixedSizeBigIntegerSerializer
Returns:
true, 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>
Overrides:
interpret in class FixedSizeBigIntegerSerializer
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>
Overrides:
interpret in class FixedSizeBigIntegerSerializer
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>
Overrides:
serialize in class FixedSizeBigIntegerSerializer
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>
Overrides:
serialize in class FixedSizeBigIntegerSerializer
Parameters:
val - The object to serialize.
Returns:
The object serialized into a byte array.