public class ByteEncoder extends Object
Modifier and Type | Field and Description |
---|---|
static ByteEncoder |
encoder
A general purpose encoder - Synchronize on it before using.
|
Constructor and Description |
---|
ByteEncoder() |
Modifier and Type | Method and Description |
---|---|
static String |
classOfData(byte[] source,
int offset,
int lengthLeft)
Return the class of the data as a string as it would be returned if
you had called getClass().getName() on the original data.
|
static int |
countEncoded(byte[] source,
int offset,
int lengthLeft)
Count how many data units (primitive values or object values) are stored in sequence
in the specified sequence of bytes.
|
Wrapper[] |
decode(byte[] source,
int offset,
int lengthLeft)
Decode a list of values (Object or primitive) stored in a byte sequence.
|
int |
decode(byte[] source,
int offset,
int lengthLeft,
Wrapper dest)
Decode a data unit at the specified location, placing it in the destination Wrapper.
|
void |
decodeEncodableObjectData(byte[] source,
int offset,
int dataLength,
Object obj)
Decode the fields of an Encodable object, as encoded by encodeEncodableObjectData().
|
Wrapper |
decodeField(byte[] source,
int offset,
int length,
String fieldName,
Wrapper dest)
This searchs for a field name and returns the encoded value associated with the field.
|
Object |
decodeObject(byte[] source,
int offset,
int lengthLeft,
Object dest)
Decode an object into the specified destination object.
|
void |
decodeObjectData(byte[] source,
int offset,
int dataLength,
Object obj)
Decode the data bytes of the object, as encoded by encodeObjectData.
|
Object |
decodeObjectField(byte[] source,
int offset,
int length,
String fieldName,
Object dest) |
Object |
decodeObjectField(ByteArray source,
String fieldName) |
Object[] |
decodeObjects(byte[] source,
int offset,
int lengthLeft,
Object[] dest)
Decode an array of Objects.
|
int |
decodeString(byte[] source,
int offset,
int lengthLeft,
Wrapper dest) |
String |
decodeStringField(byte[] source,
int offset,
int length,
String fieldName) |
String |
decodeStringField(ByteArray source,
String fieldName) |
int |
encode(ByteArray dest,
byte[] source,
int offset,
int length)
This encodes the bytes as a byte array data unit.
|
int |
encode(ByteArray dest,
Wrapper w)
Fully encode any data type, including a null object reference.
|
int |
encodeEncodableObjectData(ByteArray dest,
Encodable obj)
Encode the fields of an Encodable object.
|
int |
encodeField(ByteArray dest,
String fieldName,
byte[] data,
int offset,
int length) |
int |
encodeField(ByteArray dest,
String fieldName,
Object data) |
int |
encodeField(ByteArray dest,
String fieldName,
Wrapper data) |
int |
encodeObject(ByteArray dest,
Object obj)
Encode an object, specifying the full class of the object.
|
int |
encodeObjectData(ByteArray dest,
Object obj)
Encode only the data for the Object, not any information about the class of the Object.
|
ArraySection |
findField(byte[] source,
int offset,
int length,
String fieldName,
ArraySection dest)
Find the section of a byte array that contains the data for the specified field
name.
|
ArraySection |
getByteArrayBytes(ArraySection foundData,
ArraySection dest) |
ArraySection |
getByteArrayBytes(byte[] source,
int offset,
int length,
ArraySection dest)
Return an ArraySection that specifies the bytes in an encoded byte array.
|
ArraySection |
getFieldByteArray(byte[] source,
int offset,
int length,
String fieldName,
ArraySection dest) |
static boolean |
isObjectOrArray(byte[] source,
int offset,
int lengthLeft) |
static boolean |
isString(byte[] source,
int offset,
int lengthLeft) |
static int |
sizeOfData(byte[] source,
int offset,
int lengthLeft)
Return the number of bytes stored starting at the particular offset.
|
static int |
typeOfData(byte[] source,
int offset,
int lengthLeft)
Return the Wrapper type of the data stored at the specified location.
|
public static ByteEncoder encoder
public int encodeEncodableObjectData(ByteArray dest, Encodable obj)
Note that this method does not store information about the class of the object. This means you must know the object class and have a pre-created object to call the corresponding decodeEncodableObjectData() when you are ready to decode it.
dest
- The destination ByteArray or null to just get the number of bytes needed.obj
- The object to encode.public int encodeObjectData(ByteArray dest, Object obj)
The method used to encode the object depends on the class of the object or the interfaces it implements. The type of Objects you can encode are:
dest
- The destination ByteArray or null to just get the number of bytes needed.obj
- The object to encode.public int encode(ByteArray dest, Wrapper w)
dest
- the destination ByteArray or null to determine the number of bytes needed.w
- a wrapper that holds the data to be encoded.public int encodeObject(ByteArray dest, Object obj)
dest
- The destination ByteArray or null to just find out how many bytes are needed.obj
- The object to encode.public int encodeField(ByteArray dest, String fieldName, byte[] data, int offset, int length)
public int encode(ByteArray dest, byte[] source, int offset, int length)
dest
- the destination ByteArraysource
- the bytes to be encoded.offset
- the offset of the first byte to be encoded.length
- the number of bytes to be encoded.public void decodeObjectData(byte[] source, int offset, int dataLength, Object obj) throws StreamCorruptedException, ClassNotFoundException
source
- the source of the data bytes.offset
- the offset of the first byte.dataLength
- the number of bytes.obj
- the destination object.StreamCorruptedException
ClassNotFoundException
public void decodeEncodableObjectData(byte[] source, int offset, int dataLength, Object obj) throws StreamCorruptedException
source
- the source of the data bytes.offset
- the offset of the first byte.dataLength
- the number of bytes.obj
- the destination object.StreamCorruptedException
public int decode(byte[] source, int offset, int lengthLeft, Wrapper dest) throws StreamCorruptedException, ClassNotFoundException
source
- the source of the data bytes.offset
- the offset of the first byte.lengthLeft
- the number of bytes.dest
- a non-null destination Wrapper to hold the data.StreamCorruptedException
ClassNotFoundException
public int decodeString(byte[] source, int offset, int lengthLeft, Wrapper dest) throws StreamCorruptedException
StreamCorruptedException
public ArraySection findField(byte[] source, int offset, int length, String fieldName, ArraySection dest) throws StreamCorruptedException
source
- the source of the data.offset
- the index of the source of the start of the data.length
- the number of bytes in the data.fieldName
- the name of the field to look for.dest
- an optional destination ArraySection for the field.StreamCorruptedException
- if the Stream is not properly formatted.public ArraySection getByteArrayBytes(byte[] source, int offset, int length, ArraySection dest) throws StreamCorruptedException
source
- the source of the bytes.offset
- the start of the encoded bytes.length
- the number of encoded bytes.dest
- an optional destination ArraySection for the field.StreamCorruptedException
- if the Stream is not properly formatted.public ArraySection getByteArrayBytes(ArraySection foundData, ArraySection dest) throws StreamCorruptedException
StreamCorruptedException
public ArraySection getFieldByteArray(byte[] source, int offset, int length, String fieldName, ArraySection dest) throws StreamCorruptedException
StreamCorruptedException
public Wrapper decodeField(byte[] source, int offset, int length, String fieldName, Wrapper dest) throws StreamCorruptedException, ClassNotFoundException
public Object decodeObjectField(byte[] source, int offset, int length, String fieldName, Object dest) throws StreamCorruptedException, ClassNotFoundException
public Object decodeObjectField(ByteArray source, String fieldName) throws StreamCorruptedException, ClassNotFoundException
public String decodeStringField(byte[] source, int offset, int length, String fieldName) throws StreamCorruptedException
StreamCorruptedException
public String decodeStringField(ByteArray source, String fieldName) throws StreamCorruptedException
StreamCorruptedException
public static int sizeOfData(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
source
- the source of the data bytes.offset
- the offset of the first byte.dataLength
- the number of bytes.StreamCorruptedException
public static int typeOfData(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
source
- the source of the data bytes.offset
- the offset of the first byte.lengthLeft
- the number of bytes.obj
- the destination object.StreamCorruptedException
public static boolean isString(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
StreamCorruptedException
public static boolean isObjectOrArray(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
StreamCorruptedException
public static String classOfData(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
source
- the source of the data bytes.offset
- the offset of the first byte.lengthLeft
- the number of bytes.obj
- the destination object.IllegalArgumentException
- if the data at the specified location is not
an Object or Array class.StreamCorruptedException
public static int countEncoded(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException
source
- the source of the data bytes.offset
- the offset of the first byte.lengthLeft
- the number of bytes.StreamCorruptedException
- if the stream is not properly formatted.public Object[] decodeObjects(byte[] source, int offset, int lengthLeft, Object[] dest) throws StreamCorruptedException, ClassNotFoundException
source
- the source of the data bytes.offset
- the offset of the first byte.lengthLeft
- the number of bytes.dest
- the destination objects which may be null, in which case a new instance of the
objects will be returned.StreamCorruptedException
ClassNotFoundException
public Wrapper[] decode(byte[] source, int offset, int lengthLeft) throws StreamCorruptedException, ClassNotFoundException
source
- the source of the bytes.offset
- the index of the start of the encoded bytes.lengthLeft
- the number of bytes left to decode.StreamCorruptedException
ClassNotFoundException
public Object decodeObject(byte[] source, int offset, int lengthLeft, Object dest) throws StreamCorruptedException, ClassNotFoundException
source
- the source of the data bytes.offset
- the offset of the first byte.length
- the number of bytes.dest
- the destination object which may be null, in which case a new instance of the
object will be created (if possible).StreamCorruptedException
ClassNotFoundException