org.helidb.lang.serializer
Class CharacterSerializer

java.lang.Object
  extended by org.helidb.lang.serializer.CharacterSerializer
All Implemented Interfaces:
Serializer<Character>
Direct Known Subclasses:
CharacterNullSerializer

public class CharacterSerializer
extends Object
implements Serializer<Character>

A Serializer for Character values. Every Character value serialized by this object is represented as a byte array of length two.

This object only serializes 16 bit char values. See Character for information on how to use 32 bit Unicode characters.

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:
Character, CharacterNullSerializer
In_jar:
helidb-core

Field Summary
static int DATA_SIZE
          Two bytes.
static CharacterSerializer INSTANCE
          The singleton instance.
 
Constructor Summary
CharacterSerializer()
           
 
Method Summary
static byte[] encodeCharacter(char c)
          Encode a character to a two bytes long, big-endian byte array.
static char getCharacter(byte[] barr)
          Get the char 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).
 Character interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 Character 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?
 Character read(InputStream is, int dataSize)
          Read a value from the InputStream.
 Character read(RandomAccess ra, int dataSize)
          Read a value from the RandomAccess' current position.
 Character readCharacter(InputStream is)
          Read a Character value from the current position of the InputStream.
 Character readCharacter(RandomAccess ra)
          Read a Character value from the current position of the RandomAccess.
 byte[] serialize(Character value)
          Serialize the supplied object into a new byte array.
 int serialize(Character 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 CharacterSerializer INSTANCE
The singleton instance. May be used instead of instantiating this class.


DATA_SIZE

public static final int DATA_SIZE
Two bytes.

See Also:
Constant Field Values
Constructor Detail

CharacterSerializer

public CharacterSerializer()
Method Detail

interpret

public Character 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<Character>
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[])

getCharacter

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

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

interpret

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

encodeCharacter

public static byte[] encodeCharacter(char c)
Encode a character to a two bytes long, big-endian byte array.

Parameters:
c - The char to encode.
Returns:
A two bytes long byte array, big-endian.

serialize

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

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

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

isNullValuesPermitted

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

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

readCharacter

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

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

readCharacter

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

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

read

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

read

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

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