Package uk.ac.starlink.table.storage
Class Codec
- java.lang.Object
-
- uk.ac.starlink.table.storage.Codec
-
public abstract class Codec extends java.lang.ObjectSerializes and deserializes objects to/from a data stream. A given instance of this class is only able to de/serialize an object of one class. Obtain an instance from the staticgetCodec(uk.ac.starlink.table.ValueInfo)method, or use one of the static members. The static members are all safe for concurrent use from multiple threads.This is (supposed to be) considerably more lightweight than all the
Serializablebusiness - for one thing there's no way to tell from the stream what kind of item has been serialized, you have to make sure the right Codec instance is on hand. In general it deals with primitive wrapper objects and arrays of same, but new Codec instances for different classes can be added.- Since:
- 3 Aug 2004
- Author:
- Mark Taylor (Starlink)
-
-
Field Summary
Fields Modifier and Type Field Description static CodecBOOLEANCodec for byte-serialized boolean values, null handled.static CodecBYTECodec for 8-bit byte, no null handling.static CodecBYTE_ARRAYCodec for variable-length array of 8-bit bytes.static CodecCHARCodec for 16-bit character, no null handling.static CodecDOUBLECodec for 64-bit floating point, nulls treated like NaN.static CodecDOUBLE_ARRAYCodec for variable-length array of 64-bit doubles.static CodecFLOATCodec for 32-bit floating point, nulls treated like NaN.static CodecFLOAT_ARRAYCodec for variable-length array of 32-bit floats.static CodecINTCodec for 32-bit integer, no null handling.static CodecINT_ARRAYCodec for variable-length array of 32-bit integers.static CodecLONGCodec for 64-bit integer, no null handling.static CodecLONG_ARRAYCodec for variable-length array of 64-bit integers.static CodecSHORTCodec for 16-bit integer, no null handling.static CodecSHORT_ARRAYCodec for variable-length array of 16-bit integers.static CodecSTRINGCodec for variable-length string.
-
Constructor Summary
Constructors Constructor Description Codec()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract booleandecodeBoolean(ByteStoreAccess in)Deserialises an item from a stream, and presents it as a boolean if possible.abstract doubledecodeDouble(ByteStoreAccess in)Deserialises an item from a stream, and presents it as a double if possible.abstract intdecodeInt(ByteStoreAccess in)Deserialises an item from a stream, and presents it as an integer if possible.abstract longdecodeLong(ByteStoreAccess in)Deserialises an item from a stream, and presents it as a long integer if possible.abstract java.lang.ObjectdecodeObject(ByteStoreAccess in)Deserializes an item from a stream.abstract intencode(java.lang.Object value, java.io.DataOutput out)Serializes an object to a stream.static CodecgetCodec(ValueInfo info)Returns a codec suitable for serializing/deserializing the contents of a given ValueInfo.abstract intgetItemSize()Returns the number of bytes a call to encode will write.protected voidwarnCorrupt()Logs a warning that unexpected data has been found in the stream during decoding.
-
-
-
Field Detail
-
BYTE
public static final Codec BYTE
Codec for 8-bit byte, no null handling.
-
SHORT
public static final Codec SHORT
Codec for 16-bit integer, no null handling.
-
INT
public static final Codec INT
Codec for 32-bit integer, no null handling.
-
LONG
public static final Codec LONG
Codec for 64-bit integer, no null handling.
-
FLOAT
public static final Codec FLOAT
Codec for 32-bit floating point, nulls treated like NaN.
-
DOUBLE
public static final Codec DOUBLE
Codec for 64-bit floating point, nulls treated like NaN.
-
CHAR
public static final Codec CHAR
Codec for 16-bit character, no null handling.
-
BOOLEAN
public static final Codec BOOLEAN
Codec for byte-serialized boolean values, null handled.
-
BYTE_ARRAY
public static final Codec BYTE_ARRAY
Codec for variable-length array of 8-bit bytes.
-
SHORT_ARRAY
public static final Codec SHORT_ARRAY
Codec for variable-length array of 16-bit integers.
-
INT_ARRAY
public static final Codec INT_ARRAY
Codec for variable-length array of 32-bit integers.
-
LONG_ARRAY
public static final Codec LONG_ARRAY
Codec for variable-length array of 64-bit integers.
-
FLOAT_ARRAY
public static final Codec FLOAT_ARRAY
Codec for variable-length array of 32-bit floats.
-
DOUBLE_ARRAY
public static final Codec DOUBLE_ARRAY
Codec for variable-length array of 64-bit doubles.
-
STRING
public static final Codec STRING
Codec for variable-length string.
-
-
Method Detail
-
encode
public abstract int encode(java.lang.Object value, java.io.DataOutput out) throws java.io.IOExceptionSerializes an object to a stream.- Parameters:
value- object to serializeout- destination stream, positioned at place to write- Returns:
- number of bytes written
- Throws:
java.io.IOException
-
decodeObject
public abstract java.lang.Object decodeObject(ByteStoreAccess in) throws java.io.IOException
Deserializes an item from a stream.- Parameters:
in- source stream, positioned at start of item- Returns:
- deserialized item
- Throws:
java.io.IOException
-
decodeInt
public abstract int decodeInt(ByteStoreAccess in) throws java.io.IOException
Deserialises an item from a stream, and presents it as an integer if possible. If the stream does not contain integer-like items, the result may not be useful.- Parameters:
in- source stream, positioned at start of item- Returns:
- best-effort integer equivalent of deserialised item
- Throws:
java.io.IOException
-
decodeLong
public abstract long decodeLong(ByteStoreAccess in) throws java.io.IOException
Deserialises an item from a stream, and presents it as a long integer if possible. If the stream does not contain integer-like items, the result may not be useful.- Parameters:
in- source stream, positioned at start of item- Returns:
- best-effort long equivalent of deserialised item
- Throws:
java.io.IOException
-
decodeDouble
public abstract double decodeDouble(ByteStoreAccess in) throws java.io.IOException
Deserialises an item from a stream, and presents it as a double if possible. If the stream does not contain numeric items, the result may not be useful.- Parameters:
in- source stream, positioned at start of item- Returns:
- best-effort double equivalent of deserialised item
- Throws:
java.io.IOException
-
decodeBoolean
public abstract boolean decodeBoolean(ByteStoreAccess in) throws java.io.IOException
Deserialises an item from a stream, and presents it as a boolean if possible. If the stream does not contain boolean-like items, the result may not be useful.- Parameters:
in- source stream, positioned at start of item- Returns:
- best-effort boolean equivalent of deserialised item
- Throws:
java.io.IOException
-
getItemSize
public abstract int getItemSize()
Returns the number of bytes a call to encode will write. If this value may vary, -1 is returned.- Returns:
- size in bytes of serialized items, or -1
-
getCodec
public static Codec getCodec(ValueInfo info)
Returns a codec suitable for serializing/deserializing the contents of a given ValueInfo. If no codec can be supplied to match info, null is returned.- Parameters:
info- object describing the kind of item which is required to be de/serialized- Returns:
- codec for the job
-
warnCorrupt
protected void warnCorrupt()
Logs a warning that unexpected data has been found in the stream during decoding.
-
-