T
- Type this interpreter can encode into and decode from binary data.public abstract class BinaryFieldInterpreter<T>
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
arraySeparator
Separator of items in the string representation of an array.
|
protected IntegerInterpreter |
arraySizeInterpreter
Interpreter used only to encode or to decode the array size.
|
protected int[] |
fixArraySizes
Indicates the dimension(s) of the data to encode/decode.
|
protected int |
NB_BYTES
Number of bytes occupied by the type managed in this BinaryFieldInterpreter.
|
protected int |
nbItems
Total number of items to encode/decode, all dimensions confounded.
|
protected java.lang.String |
strNullValue
String representation of a
null value. |
protected java.lang.String |
TYPE_LABEL
Name/Label of the type managed by this BinaryFieldInterpreter.
|
Constructor and Description |
---|
BinaryFieldInterpreter(int[] arraysizes,
java.lang.String typeLabel,
int nbBytes)
Builds a BinaryFieldInterpreter.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
arraySizeToString(int[] arraysize)
Lets serialize the given array size in String.
|
protected java.util.ArrayList<T> |
convertIntoArray(java.lang.Object value)
Converts the given value (single value or multidimensional array) into one array of one dimension.
|
protected abstract T |
convertPrimary(java.lang.Object value)
Converts/Casts the given object into an object of type T.
|
java.lang.String |
convertToString(java.lang.Object cellValue)
Serializes the given cell value.
|
protected T[] |
createEmptyArray(int arraysize)
Creates an array (1 dimension) of type T with the given length.
|
static BinaryFieldInterpreter<?> |
createInterpreter(SavotField field)
Creates the BinaryFieldInterpreter corresponding to the given
SavotField . |
T[] |
decode(java.io.InputStream input)
Decodes the binary data coming from the given input stream.
|
abstract T |
decodePrimary(byte[] bytes,
int offset)
Decodes only one data of type T from the given bytes array.
|
void |
encode(java.io.OutputStream output,
java.lang.Object value)
Encodes the given value in binary and writes it in the given output stream.
|
abstract byte[] |
encodePrimary(T value)
Encodes a single value of type T in binary.
|
protected abstract java.lang.Class<T[]> |
getArrayClass()
Gets the precise array type.
|
protected int |
getArraySize(java.io.InputStream input)
Gets the number of data of type T to get currently.
|
byte[] |
getPadding()
Creates an array of the length of the type T (
NB_BYTES ) with padding values (0x00). |
byte[] |
getPadding(int length)
Creates an array of the given length with padding values (0x00).
|
protected byte[] |
readBytes(java.io.InputStream input,
int length)
Reads
length data of type T from the given input stream considering
the number of bytes one data of this type is supposed to occupied (see NB_BYTES ). |
protected final java.lang.String TYPE_LABEL
protected final int NB_BYTES
protected final IntegerInterpreter arraySizeInterpreter
protected final int[] fixArraySizes
protected final int nbItems
protected java.lang.String strNullValue
null
value.protected java.lang.String arraySeparator
public BinaryFieldInterpreter(int[] arraysizes, java.lang.String typeLabel, int nbBytes) throws BinaryInterpreterException
arraysizes
- Dimensions of the primary data type to encode/decode.typeLabel
- Name/Label of the type to manage.nbBytes
- Number of bytes of a primary data type.BinaryInterpreterException
- If the dimensions are not valid or if there is a problem while creating the array size interpreter.public T[] decode(java.io.InputStream input) throws java.io.IOException
Decodes the binary data coming from the given input stream.
Basically, this method gets the array-size (particularly if variable), creates an empty array of the good dimension(s) and fills it by decoding one by one data of type T.
input
- Data to decode.null
if EOF, else the decoded data of type T.java.io.IOException
- If the EOF has been reached in an unexpected manner
or if an error occurs while reading bytes from the given input stream.getArraySize(InputStream)
,
readBytes(InputStream, int)
,
createEmptyArray(int)
,
decodePrimary(byte[],int)
protected int getArraySize(java.io.InputStream input) throws java.io.IOException
Gets the number of data of type T to get currently.
Either the array-size is defined at the creation, or it is variable. In one hand the given array-size is just returned. In another hand the array-size is an integer value which prefixes the data to read. In that case, we must read one integer from the given input stream. This integer corresponds to the length of the data of type T to get.
input
- The data to decode.-1
if EOF, else the length of data of type T to return.java.io.IOException
- If an error occurs while decoding the array-size from the given input stream.protected byte[] readBytes(java.io.InputStream input, int length) throws java.io.IOException
Reads length
data of type T from the given input stream considering
the number of bytes one data of this type is supposed to occupied (see NB_BYTES
).
input
- Input stream from which bytes must be read.length
- null
if EOF, else the read bytes (the length of this array is a multiple of NB_BYTES
).java.io.IOException
- If the end of file has been reached before getting length
full items of type T
or if there is an error while reading bytes from the given input stream.public java.lang.String convertToString(java.lang.Object cellValue)
Serializes the given cell value.
NOTE:
The given value can be a single value or a multidimensional array.
In this last case all items (whatever is their dimension)
are written in a String separated by arraySeparator
(which depends of the type managed by the BinaryFieldInterpreter ; by default ' ').
cellValue
- Value (single value or array) of a column/cell/field. (may be null
)protected T[] createEmptyArray(int arraysize) throws java.lang.ClassCastException, java.lang.NegativeArraySizeException
arraysize
- Length of the array to create. (MUST BE >= 0)arraysize
.java.lang.ClassCastException
- If it is impossible to create an array of T.java.lang.NegativeArraySizeException
- If the given array size is negative.getArrayClass()
protected abstract java.lang.Class<T[]> getArrayClass()
Gets the precise array type.
Generally: T[].class
, where T must be a concrete class.
public abstract T decodePrimary(byte[] bytes, int offset) throws BinaryInterpreterException
Decodes only one data of type T from the given bytes array.
WARNING: bytes
is supposed to contain enough bytes (>= NB_BYTES
) from the given offset.
bytes
- Array to use to extract enough bytes so that decoding one data of type T.offset
- Position from which bytes must be read.BinaryInterpreterException
- If an error occurs while decoding bytes.public void encode(java.io.OutputStream output, java.lang.Object value) throws java.io.IOException, BinaryInterpreterException
output
- Stream in which the encoded value must be written.value
- The value to write once encoded in binary.java.io.IOException
- If there is an error while writing in the given stream.BinaryInterpreterException
- If there is an error while encoding the given value.convertIntoArray(Object)
,
encodePrimary(Object)
,
getPadding()
public byte[] getPadding(int length)
public final byte[] getPadding()
NB_BYTES
) with padding values (0x00).getPadding(int)
protected java.util.ArrayList<T> convertIntoArray(java.lang.Object value) throws BinaryInterpreterException
Converts the given value (single value or multidimensional array) into one array of one dimension.
NOTE:
A String value will be considered as an array whose items are separated by arraySeparator
.
Another type (except an array) must be understandable by convertPrimary(Object)
.
value
- Value to convert (single value or multidimensional array).BinaryInterpreterException
- If convertPrimary(Object)
fails.convertPrimary(Object)
protected abstract T convertPrimary(java.lang.Object value) throws BinaryInterpreterException
value
- The value to cast. (MAY BE NULL)BinaryInterpreterException
- If there is an error while converting the given value.public abstract byte[] encodePrimary(T value) throws BinaryInterpreterException
Encodes a single value of type T in binary.
NOTE: If the given value is null, getPadding()
will be returned.
value
- The value to encode. (MAY BE NULL).BinaryInterpreterException
- If there is an error while encoding the given value.public static final BinaryFieldInterpreter<?> createInterpreter(SavotField field) throws BinaryInterpreterException
Creates the BinaryFieldInterpreter corresponding to the given SavotField
.
Only the null
attribute is managed. It indicates the string representation of a null
value.
It is used to identify a null
s and to write them while converting a cell value in string (see convertToString(Object)
).
field
- A SavotField
.BinaryInterpreterException
- If there is an error while creating the interpreter.public static final java.lang.String arraySizeToString(int[] arraysize)
arraysize
- Array-size to serialize.