org.helidb.lang.serializer
Class IntegerSerializer

java.lang.Object
  extended by org.helidb.lang.serializer.IntegerSerializer
All Implemented Interfaces:
Serializer<Integer>
Direct Known Subclasses:
IntegerNullSerializer

public class IntegerSerializer
extends Object
implements Serializer<Integer>

A Serializer for Integer values. Integer values are serialized to a four bytes long, big-endian byte array.

This serializer does not handle null values.

The singleton instance INSTANCE may be used instead of creating a new object of this class.

Since:
1.0
Author:
Karl Gustafsson
See Also:
IntegerNullSerializer, ConfigurableLengthIntegerSerializer, Integer
In_jar:
helidb-core

Field Summary
static int DATA_SIZE
          Four bytes.
static IntegerSerializer INSTANCE
          The singleton instance.
 
Constructor Summary
IntegerSerializer()
           
 
Method Summary
static byte[] encodeInteger(int i)
          Encode an integer to a four bytes long, big-endian byte array.
static int getInteger(byte[] barr)
          Get the int value encoded in the big-endian, byte array.
 int getSerializedSize()
          If the serialized data produced by this serializer always is of the same size, return that size (in number of bytes).
 Integer interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 Integer 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?
 Integer read(InputStream is, int dataSize)
          Read a value from the InputStream.
 Integer read(RandomAccess ra, int dataSize)
          Read a value from the RandomAccess' current position.
 Integer readInteger(InputStream is)
          Read an Integer value from the current position of the InputStream.
 Integer readInteger(RandomAccess ra)
          Read an Integer value from the current position of the RandomAccess.
 byte[] serialize(Integer value)
          Serialize the supplied object into a new byte array.
 int serialize(Integer value, 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
 

Field Detail

INSTANCE

public static final IntegerSerializer INSTANCE
The singleton instance. May be used instead of instantiating this class.


DATA_SIZE

public static final int DATA_SIZE
Four bytes.

See Also:
Constant Field Values
Constructor Detail

IntegerSerializer

public IntegerSerializer()
Method Detail

getInteger

public static int getInteger(byte[] barr)
                      throws SerializationException,
                             NullPointerException
Get the int value encoded in the big-endian, byte array.

Parameters:
barr - The byte array. It must be four bytes long and big-endian.
Returns:
The int value encoded in the array.
Throws:
SerializationException - If the array length is different from four bytes.
NullPointerException - If barr is null
See Also:
encodeInteger(int)

interpret

public Integer 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<Integer>
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 Integer 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<Integer>
Parameters:
barr - The byte array containing the data.
Returns:
The interpreted object.
See Also:
Serializer.interpret(byte[], int, int)

encodeInteger

public static byte[] encodeInteger(int i)
Encode an integer to a four bytes long, big-endian byte array.

Parameters:
i - The integer to encode.
Returns:
A four bytes long byte array, big-endian.

serialize

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

Specified by:
serialize in interface Serializer<Integer>
Parameters:
value - 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(Integer value)
Description copied from interface: Serializer
Serialize the supplied object into a new byte array.

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

getSerializedSize

public 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<Integer>
Returns:
4, always.

isNullValuesPermitted

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

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

readInteger

public Integer readInteger(RandomAccess ra)
                    throws NotEnoughDataException,
                           WrappedIOException
Read an Integer value from the current position of the RandomAccess.

Parameters:
ra - The RandomAccess to read from. The current position of this is incremented by 4 bytes.
Returns:
The Integer value.
Throws:
NotEnoughDataException - If 4 bytes cannot be read.
WrappedIOException - On I/O errors.

readInteger

public Integer readInteger(InputStream is)
                    throws NotEnoughDataException,
                           WrappedIOException
Read an Integer value from the current position of the InputStream.

Parameters:
is - The InputStream to read from. The current position of the stream is incremented by 4 bytes.
Returns:
The Integer value.
Throws:
NotEnoughDataException - If 4 bytes cannot be read.
WrappedIOException - If an IOException is encountered while reading data.

read

public Integer 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<Integer>
Parameters:
ra - The RandomAccess to read from.
dataSize - The size of the value.
Returns:
The value.

read

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

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