org.helidb.lang.serializer
Class LongSerializer

java.lang.Object
  extended by org.helidb.lang.serializer.LongSerializer
All Implemented Interfaces:
Serializer<Long>
Direct Known Subclasses:
LongNullSerializer

public class LongSerializer
extends Object
implements Serializer<Long>

A Serializer for Long values. Long values are serialized to eight bytes long, big-endian byte arrays.

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:
LongNullSerializer, ConfigurableLengthLongNullSerializer, Long
In_jar:
helidb-core

Field Summary
static int DATA_SIZE
          Eight bytes.
static LongSerializer INSTANCE
          The singleton instance.
 
Constructor Summary
LongSerializer()
           
 
Method Summary
static byte[] encodeLong(long l)
          Encode the long value to an eight bytes long, big-endian byte array.
static long getLong(byte[] barr)
          Get the long value encoded in the 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).
 Long interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 Long 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?
 Long read(InputStream is, int dataSize)
          Read a value from the InputStream.
 Long read(RandomAccess ra, int dataSize)
          Read a value from the RandomAccess' current position.
 Long readLong(InputStream is)
          Read a Long value from the current position of the InputStream.
 Long readLong(RandomAccess ra)
          Read a Long value from the current position of the RandomAccess.
 byte[] serialize(Long value)
          Serialize the supplied object into a new byte array.
 int serialize(Long 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 LongSerializer INSTANCE
The singleton instance. May be used instead of instantiating this class.


DATA_SIZE

public static final int DATA_SIZE
Eight bytes.

See Also:
Constant Field Values
Constructor Detail

LongSerializer

public LongSerializer()
Method Detail

getLong

public static long getLong(byte[] barr)
                    throws SerializationException,
                           NullPointerException
Get the long value encoded in the byte array.

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

interpret

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

encodeLong

public static byte[] encodeLong(long l)
Encode the long value to an eight bytes long, big-endian byte array.

Parameters:
l - The long value to encode.
Returns:
A byte array containing the encoded value.
See Also:
getLong(byte[])

serialize

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

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

Specified by:
serialize in interface Serializer<Long>
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<Long>
Returns:
8, always.

isNullValuesPermitted

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

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

readLong

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

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

readLong

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

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

read

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

read

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

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