com.devexperts.qd.kit
Class ByteArrayField

java.lang.Object
  extended by com.devexperts.qd.kit.AbstractDataField
      extended by com.devexperts.qd.kit.AbstractDataObjField
          extended by com.devexperts.qd.kit.ByteArrayField
All Implemented Interfaces:
DataField, DataObjField

public class ByteArrayField
extends AbstractDataObjField

The ByteArrayField represents a linear byte array field with plain serialized form. It uses CompactInt to encode byte array length first, then serializes bytes themselves. The value -1 for length is used as a marker to distinguish 'null' array from empty one. Default representation of the value is byte[] as returned by readObj(java.io.DataInput), but String, char[] and arbitrary serializable objects are also supported by writeObj(java.io.DataOutput, java.lang.Object) and toString(Object).


Constructor Summary
ByteArrayField(int index, java.lang.String name)
           
 
Method Summary
 boolean equals(java.lang.Object value1, java.lang.Object value2)
          Compares two specified field values for equality.
 java.lang.Object fromByteArray(byte[] bytes)
          This method is a hook to convert byte arrays into objects.
 java.lang.Object parseString(java.lang.String value)
          Parses string representation of specified field value.
 java.lang.Object readObj(com.devexperts.io.BufferedInput in)
          Reads field value from specified data input and returns it to the caller.
 java.lang.Object readObj(java.io.DataInput in)
          Reads field value from specified data input and returns it to the caller.
 byte[] toByteArray(java.lang.Object value)
          This method is a hook to provide custom conversion of objects to byte arrays for serialization.
 java.lang.String toString(java.lang.Object value)
          Returns string representation of the specified field value.
 void writeObj(com.devexperts.io.BufferedOutput out, java.lang.Object value)
          Writes specified field value into specified buffered output.
 void writeObj(java.io.DataOutput out, java.lang.Object value)
          Writes specified field value into specified data output.
 
Methods inherited from class com.devexperts.qd.kit.AbstractDataField
getIndex, getLocalName, getName, getRecord, getSerialType, setRecord, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.devexperts.qd.DataField
getIndex, getLocalName, getName, getRecord, getSerialType
 

Constructor Detail

ByteArrayField

public ByteArrayField(int index,
                      java.lang.String name)
Method Detail

fromByteArray

public java.lang.Object fromByteArray(byte[] bytes)
This method is a hook to convert byte arrays into objects. This implementation returns bytes. This method is invoked from readObj(java.io.DataInput).

This method can be overridden to provide custom conversion of byte arrays to objects. If you override this method, then you also typically need to override toByteArray(Object) and consider overriding toString(Object). For example, if you need to convert byte arrays to MyObject instances, write:

 public Object fromByteArray(byte[] bytes) {
     return MyObject.forByteArray(bytes);
 }
 
It is recommended that all such MyObject classes lazily reconstruct themselves from byte array to avoid potentially costly deserialization in multiplexor nodes.

Parameters:
bytes - Byte array to convert to object.
Returns:
Resulting object.

toByteArray

public byte[] toByteArray(java.lang.Object value)
This method is a hook to provide custom conversion of objects to byte arrays for serialization. This implementation works depending on the value class: This method is invoked from equals(Object, Object), toString(Object), and writeObj(DataOutput, Object).

This method can be overridden to provide custom conversion of objects to byte arrays. If you override this method, then you also typically need to override fromByteArray(byte[]) and consider overriding toString(Object). For example, if you need to convert MyObject instances to byte arrays, write:

 public byte[] toByteArray(Object value) {
     if (value instanceof MyObject)
         return ((MyObject)value).toByteArray();
     else
         super.toByteArray(value);
 }
 
It is recommended that all such MyObject classes cache their produced byte arrays to avoid potentially costly serialization in multiplexor nodes.

Parameters:
value - The object to convert to byte array.
Returns:
array of bytes or null if default conversion via IOUtil.objectToBytes(java.lang.Object) or IOUtil.writeObject(DataOutput, Object) shall be used.

toString

public java.lang.String toString(java.lang.Object value)
Returns string representation of the specified field value. This method is used for debugging purposes. This implementation coverts object to byte array via toByteArray(Object), if that returns null, then it uses IOUtil.objectToBytes(Object); then returns a hex representation of the resulting byte array.

Specified by:
toString in interface DataObjField
Overrides:
toString in class AbstractDataObjField

parseString

public java.lang.Object parseString(java.lang.String value)
Description copied from class: AbstractDataObjField
Parses string representation of specified field value. This method is used for debugging purposes. This implementation returns value.

Specified by:
parseString in interface DataObjField
Overrides:
parseString in class AbstractDataObjField

equals

public boolean equals(java.lang.Object value1,
                      java.lang.Object value2)
Description copied from class: AbstractDataObjField
Compares two specified field values for equality. This method is used for implementation of ticker contract. This implementation returns value1 == value2 || (value1 != null && value1.equals(value2)).

Specified by:
equals in interface DataObjField
Overrides:
equals in class AbstractDataObjField

writeObj

public final void writeObj(java.io.DataOutput out,
                           java.lang.Object value)
                    throws java.io.IOException
Description copied from interface: DataObjField
Writes specified field value into specified data output.

Throws:
java.io.IOException - as specified data output does.

writeObj

public final void writeObj(com.devexperts.io.BufferedOutput out,
                           java.lang.Object value)
                    throws java.io.IOException
Description copied from interface: DataObjField
Writes specified field value into specified buffered output.

Throws:
java.io.IOException - as specified data output does.

readObj

public final java.lang.Object readObj(java.io.DataInput in)
                               throws java.io.IOException
Description copied from interface: DataObjField
Reads field value from specified data input and returns it to the caller.

Throws:
java.io.IOException - as specified data input does.

readObj

public final java.lang.Object readObj(com.devexperts.io.BufferedInput in)
                               throws java.io.IOException
Description copied from interface: DataObjField
Reads field value from specified data input and returns it to the caller.

Throws:
java.io.IOException - as specified data input does.