org.helidb.lang.serializer
Class ConstantSizeStringSerializer

java.lang.Object
  extended by org.helidb.lang.serializer.ConstantSizeStringSerializer
All Implemented Interfaces:
Serializer<String>
Direct Known Subclasses:
ConstantSizeStringNullSerializer

public class ConstantSizeStringSerializer
extends Object
implements Serializer<String>

This Serializer serializes strings into constant size byte arrays. When instantiating this class, the client chooses the Charset to be used to serialize text.

It is important to know how the selected Charset encodes characters, how many bytes a character occupies and if the Charset adds a header to the encoded information. For instance

Since creating CharsetEncoder and CharsetDecoder objects are fairly expensive operations, this object keeps one CharsetEncoder and one CharsetDecoder instance for the target charset. It protects both instances with the object's intrinsic lock (synchronized). If several threads are to use a serializer for a certain charset, consider giving them one serializer instance each to avoid contention.

This serializer does not handle null values.

Since:
1.0
Author:
Karl Gustafsson
See Also:
StringSerializer, ConstantSizeStringNullSerializer
In_jar:
helidb-core

Constructor Summary
ConstantSizeStringSerializer(Charset cs, int length, boolean truncateInput)
          Create a new serializer.
 
Method Summary
 int getSerializedSize()
          Get the size of serialized data.
 String interpret(byte[] barr)
          Interpret the data in the byte array to an object of the type handled by the Serializer implementation.
 String 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?
 String read(InputStream is, int dataSize)
          Read a value from the InputStream.
 String read(RandomAccess ra, int dataSize)
          Read a value from the RandomAccess' current position.
 byte[] serialize(String s)
          Serialize the supplied object into a new byte array.
 int serialize(String s, 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

ConstantSizeStringSerializer

public ConstantSizeStringSerializer(Charset cs,
                                    int length,
                                    boolean truncateInput)
                             throws IllegalArgumentException
Create a new serializer.

Parameters:
cs - The Charset to use for encoding text.
length - The maximum length in bytes of serialized text. To calculate a good maximum length, it is important to know how the selected Charset encodes text. See the class code above.
truncateInput - If the input exceeds the maximum length, should it be truncated? If not, the serialize(String) methods throws an exception instead.
Throws:
IllegalArgumentException - If the Charset cannot encode text (its canEncode method returns false), or if the maximum length is too short to fit one character in the selected Charset.
Method Detail

isNullValuesPermitted

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

Specified by:
isNullValuesPermitted in interface Serializer<String>
Returns:
false.

getSerializedSize

public int getSerializedSize()
Get the size of serialized data.

Specified by:
getSerializedSize in interface Serializer<String>
Returns:
The length selected when creating this object.

serialize

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

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

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

interpret

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

read

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

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

read

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