GemFire 5.5

com.gemstone.gemfire
Class DataSerializer

java.lang.Object
  extended by com.gemstone.gemfire.DataSerializer

public abstract class DataSerializer
extends Object

Provides static helper methods for reading and writing non-primitive data when working with a DataSerializable. For instance, classes that implement DataSerializable can use the DataSerializer in their toData and fromData methods:

public class Employee implements DataSerializable {
  private int id;
  private String name;
  private Date birthday;
  private Company employer;

  public void toData(DataOutput out) throws IOException {
    out.writeInt(this.id);
    out.writeUTF(this.name);
    DataSerializer.writeDate(this.birthday, out);
    DataSerializer.writeObject(this.employer, out);
  }

  public void fromData(DataInput in)
    throws IOException, ClassNotFoundException {

    this.id = in.readInt();
    this.name = in.readUTF();
    this.birthday = DataSerializer.readDate(in);
    this.employer = (Company) DataSerializer.readObject(in);
  }
}

 

Instances of DataSerializer are used to data serialize objects (such as instances of standard Java classes or third-party classes for which the source code is not available) that do not implement the DataSerializable interface.

The following DataSerializer data serializes instances of Company. In order for the data serialization framework to consult this custom serializer, it must be registered with the framework.

public class CompanySerializer extends DataSerializer {

  static {
    DataSerializer.register(CompanySerializer.class, (byte) 42);
  }

  /**
 May be invoked reflectively if instances of Company are
 distributed to other VMs.
/
  public CompanySerializer() {

  }

  public Class[] getSupportedClasses() {
    return new Class[] { Company.class };
  }

  public boolean toData(Object o, DataOutput out)
    throws IOException {
    if (o instanceof Company) {
      Company company = (Company) o;
      out.writeUTF(company.getName());

      // Let's assume that Address is java.io.Serializable
      Address address = company.getAddress();
      writeObject(address, out);
      return true;

    } else {
      return false;
    }
  }

  public Object fromData(DataInput in)
    throws IOException, ClassNotFoundException {

    String name = in.readUTF();
    Address address = (Address) readObject(in);
    return new Company(name, address);
  }
}
 
Just like Instantiators, a DataSerializer may be sent to other members of the distributed system when it is registered. The data serialization framework does not require that a DataSerializer be Serializable, but it does require that it provide a zero-argument constructor.

Since:
3.5
See Also:
writeObject(Object, DataOutput), readObject(java.io.DataInput)

Field Summary
static boolean DUMP_SERIALIZED
          If the "DataSerializer.DUMP_SERIALIZED" system property is set, the class names of the objects that are serialized by writeObject(Object, DataOutput) using standard Java serialization are logged to standard out.
static byte NULL
          A header byte meaning that the next element in the stream is a null
 
Constructor Summary
DataSerializer()
          Creates a new DataSerializer.
 
Method Summary
 boolean equals(Object o)
          Two DataSerializers are consider to be equal if they have the same id.
abstract  Object fromData(DataInput in)
          Reads an object from a DataInput.
 byte getId()
          Returns the id of this DataSerializer.
abstract  Class[] getSupportedClasses()
          Returns the Classes whose instances are data serialized by this DataSerializer.
static ArrayList readArrayList(DataInput in)
          Reads an ArrayList from a DataInput.
static Boolean readBoolean(DataInput in)
          Reads an instance of Boolean from a DataInput.
static Byte readByte(DataInput in)
          Reads an instance of Byte from a DataInput.
static byte[] readByteArray(DataInput in)
          Reads an array of bytes from a DataInput.
static Character readCharacter(DataInput in)
          Reads an instance of Character from a DataInput.
static Class readClass(DataInput in)
          Reads an instance of Class from a DataInput.
static Date readDate(DataInput in)
          Reads an instance of Date from a DataInput.
static Double readDouble(DataInput in)
          Reads an instance of Double from a DataInput.
static double[] readDoubleArray(DataInput in)
          Reads an array of doubles from a DataInput.
static File readFile(DataInput in)
          Reads an instance of File from a DataInput.
static Float readFloat(DataInput in)
          Reads an instance of Float from a DataInput.
static float[] readFloatArray(DataInput in)
          Reads an array of floats from a DataInput.
static HashMap readHashMap(DataInput in)
          Reads a HashMap from a DataInput.
static HashSet readHashSet(DataInput in)
          Reads a HashSet from a DataInput.
static InetAddress readInetAddress(DataInput in)
          Reads an instance of InetAddress from a DataInput.
static int[] readIntArray(DataInput in)
          Reads an int array from a DataInput.
static Integer readInteger(DataInput in)
          Reads an instance of Integer from a DataInput.
static LinkedList readLinkedList(DataInput in)
          Reads an LinkedList from a DataInput.
static Long readLong(DataInput in)
          Reads an instance of Long from a DataInput.
static long[] readLongArray(DataInput in)
          Reads an array of longs from a DataInput.
static Object readObject(DataInput in)
          Reads an arbitrary object from a DataInput.
static Object[] readObjectArray(DataInput in)
          Reads an array of Objects from a DataInput.
static boolean readPrimitiveBoolean(DataInput in)
          Reads a primitive boolean from a DataInput.
static byte readPrimitiveByte(DataInput in)
          Reads a primitive byte from a DataInput.
static char readPrimitiveChar(DataInput in)
          Reads a primitive char from a DataInput.
static double readPrimitiveDouble(DataInput in)
          Reads a primitive double from a DataInput.
static float readPrimitiveFloat(DataInput in)
          Reads a primitive float from a DataInput.
static int readPrimitiveInt(DataInput in)
          Reads a primitive int from a DataInput.
static long readPrimitiveLong(DataInput in)
          Reads a primitive long from a DataInput.
static short readPrimitiveShort(DataInput in)
          Reads a primitive short from a DataInput.
static Properties readProperties(DataInput in)
          Reads a serialized Properties object from a DataInput.
static Region readRegion(DataInput in)
          Reads an instance of Region.
static Short readShort(DataInput in)
          Reads an instance of Short from a DataInput.
static short[] readShortArray(DataInput in)
          Reads an array of shorts from a DataInput.
static String readString(DataInput in)
          Reads an instance of String from a DataInput.
static String[] readStringArray(DataInput in)
          Reads an array of Strings from a DataInput.
static com.gemstone.bp.edu.emory.mathcs.backport.java.util.concurrent.TimeUnit readTimeUnit(DataInput in)
          Reads a TimeUnit from a DataInput.
static int readUnsignedByte(DataInput in)
          Reads a primitive int as an unsigned byte from a DataInput using DataInput.readUnsignedByte().
static int readUnsignedShort(DataInput in)
          Reads a primitive int as an unsigned short from a DataInput using DataInput.readUnsignedShort().
static DataSerializer register(Class c, byte id)
          Registers a DataSerializer class with the data serialization framework.
abstract  boolean toData(Object o, DataOutput out)
          Data serializes an object to a DataOutput.
static void writeArrayList(ArrayList list, DataOutput out)
          Writes an ArrayList to a DataOutput.
static void writeBoolean(Boolean value, DataOutput out)
          Writes an instance of Boolean to a DataOutput.
static void writeByte(Byte value, DataOutput out)
          Writes an instance of Byte to a DataOutput.
static void writeByteArray(byte[] array, DataOutput out)
          Writes an array of bytes to a DataOutput.
static void writeByteArray(byte[] array, int len, DataOutput out)
          Writes the first len elements of an array of bytes to a DataOutput.
static void writeCharacter(Character value, DataOutput out)
          Writes an instance of Character to a DataOutput.
static void writeClass(Class c, DataOutput out)
          Writes an instance of Class to a DataOutput.
static void writeDate(Date date, DataOutput out)
          Writes an instance of Date to a DataOutput.
static void writeDouble(Double value, DataOutput out)
          Writes an instance of Double to a DataOutput.
static void writeDoubleArray(double[] array, DataOutput out)
          Writes an array of doubles to a DataOutput.
static void writeFile(File file, DataOutput out)
          Writes an instance of File to a DataOutput.
static void writeFloat(Float value, DataOutput out)
          Writes an instance of Float to a DataOutput.
static void writeFloatArray(float[] array, DataOutput out)
          Writes an array of floats to a DataOutput.
static void writeHashMap(HashMap map, DataOutput out)
          Writes a HashMap to a DataOutput.
static void writeHashSet(HashSet set, DataOutput out)
          Writes a HashSet to a DataOutput.
static void writeInetAddress(InetAddress address, DataOutput out)
          Writes an instance of InetAddress to a DataOutput.
static void writeIntArray(int[] array, DataOutput out)
          Writes an int array to a DataOutput.
static void writeInteger(Integer value, DataOutput out)
          Writes an instance of Integer to a DataOutput.
static void writeLinkedList(LinkedList list, DataOutput out)
          Writes an LinkedList to a DataOutput.
static void writeLong(Long value, DataOutput out)
          Writes an instance of Long to a DataOutput.
static void writeLongArray(long[] array, DataOutput out)
          Writes an array of longs to a DataOutput.
static void writeObject(Object o, DataOutput out)
          Writes an arbitrary object to a DataOutput.
static void writeObject(Object o, DataOutput out, boolean allowJavaSerialization)
          Writes an arbitrary object to a DataOutput.
static void writeObjectArray(Object[] array, DataOutput out)
          Writes an array of Objects to a DataOutput.
static void writeObjectAsByteArray(Object obj, DataOutput out)
          Serialize the given object obj into a byte array using writeObject(Object, DataOutput) and then writes the byte array to the given data output out in the same format writeByteArray(byte[], DataOutput) does.
static void writePrimitiveBoolean(boolean value, DataOutput out)
          Writes a primitive boolean to a DataOutput.
static void writePrimitiveByte(byte value, DataOutput out)
          Writes a primitive byte to a DataOutput.
static void writePrimitiveChar(char value, DataOutput out)
          Writes a primitive char to a DataOutput.
static void writePrimitiveDouble(double value, DataOutput out)
          Writes a primtive double to a DataOutput.
static void writePrimitiveFloat(float value, DataOutput out)
          Writes a primitive float to a DataOutput.
static void writePrimitiveInt(int value, DataOutput out)
          Writes a primitive int to a DataOutput.
static void writePrimitiveLong(long value, DataOutput out)
          Writes a primitive long to a DataOutput.
static void writePrimitiveShort(short value, DataOutput out)
          Writes a primitive short to a DataOutput.
static void writeProperties(Properties props, DataOutput out)
          Writes a Properties object to a DataOutput.
static void writeRegion(Region rgn, DataOutput out)
          Writes an instance of Region.
static void writeShort(Short value, DataOutput out)
          Writes an instance of Short to a DataOutput.
static void writeShortArray(short[] array, DataOutput out)
          Writes an array of shorts to a DataOutput.
static void writeString(String value, DataOutput out)
          Writes an instance of String to a DataOutput.
static void writeStringArray(String[] array, DataOutput out)
          Writes an array of Strings to a DataOutput.
static void writeTimeUnit(com.gemstone.bp.edu.emory.mathcs.backport.java.util.concurrent.TimeUnit unit, DataOutput out)
          Writes a TimeUnit to a DataOutput.
static void writeUnsignedByte(int value, DataOutput out)
          Writes a primitive int as an unsigned byte to a DataOutput.
static void writeUnsignedShort(int value, DataOutput out)
          Writes a primitive int as an unsigned short to a DataOutput.
static void writeUserDataSerializableHeader(byte classId, DataOutput out)
           
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DUMP_SERIALIZED

public static final boolean DUMP_SERIALIZED
If the "DataSerializer.DUMP_SERIALIZED" system property is set, the class names of the objects that are serialized by writeObject(Object, DataOutput) using standard Java serialization are logged to standard out. This aids in determining which classes should implement DataSerializable (or should be special cased by a custom DataSerializer).


NULL

public static final byte NULL
A header byte meaning that the next element in the stream is a null

See Also:
Constant Field Values
Constructor Detail

DataSerializer

public DataSerializer()
Creates a new DataSerializer. All class that implement DataSerializer must provide a zero-argument constructor.

See Also:
register(Class, byte)
Method Detail

writeClass

public static void writeClass(Class c,
                              DataOutput out)
                       throws IOException
Writes an instance of Class to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readClass(java.io.DataInput)

readClass

public static Class readClass(DataInput in)
                       throws IOException,
                              ClassNotFoundException
Reads an instance of Class from a DataInput. The class will be loaded using the current content class loader.

Throws:
IOException - A problem occurs while reading from in
ClassNotFoundException - The class cannot be loaded

writeRegion

public static void writeRegion(Region rgn,
                               DataOutput out)
                        throws IOException
Writes an instance of Region. A Region is serialized as just a reference to a full path only. It will be recreated on the other end by calling CacheFactory.getAnyInstance() and then calling getRegion on it.

Throws:
IOException

readRegion

public static Region readRegion(DataInput in)
                         throws IOException,
                                ClassNotFoundException
Reads an instance of Region. A Region is serialized as a reference to a full path only. It is recreated on the other end by calling CacheFactory.getAnyInstance() and then calling getRegion on it.

Parameters:
in - the input stream
Returns:
the Region instance
Throws:
CacheClosedException - if a cache has not been created or the only created one is closed.
RegionNotFoundException - if there is no region by this name in the Cache
IOException
ClassNotFoundException

writeDate

public static void writeDate(Date date,
                             DataOutput out)
                      throws IOException
Writes an instance of Date to a DataOutput. Note that even though date may be an instance of a subclass of Date, readDate will always return an instance of Date, not an instance of the subclass. To preserve the class type of date,\ writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readDate(java.io.DataInput)

readDate

public static Date readDate(DataInput in)
                     throws IOException
Reads an instance of Date from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeFile

public static void writeFile(File file,
                             DataOutput out)
                      throws IOException
Writes an instance of File to a DataOutput. Note that even though file may be an instance of a subclass of File, readFile will always return an instance of File, not an instance of the subclass. To preserve the class type of file, writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readFile(java.io.DataInput), File.getCanonicalPath()

readFile

public static File readFile(DataInput in)
                     throws IOException
Reads an instance of File from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeInetAddress

public static void writeInetAddress(InetAddress address,
                                    DataOutput out)
                             throws IOException
Writes an instance of InetAddress to a DataOutput. The InetAddress is data serialized by writing its byte representation to the DataOutput. readInetAddress(java.io.DataInput) converts the byte representation to an instance of InetAddress using InetAddress.getAddress(). As a result, if address is an instance of a user-defined subclass of InetAddress (that is, not an instance of one of the subclasses from the java.net package), its class will not be preserved. In order to be able to read an instance of the user-defined class, writeObject(Object, DataOutput) should be used.

Throws:
IOException - A problem occurs while writing to out
See Also:
readInetAddress(java.io.DataInput)

readInetAddress

public static InetAddress readInetAddress(DataInput in)
                                   throws IOException
Reads an instance of InetAddress from a DataInput.

Throws:
IOException - A problem occurs while reading from in or the address read from in is unknown
See Also:
InetAddress.getAddress()

writeString

public static void writeString(String value,
                               DataOutput out)
                        throws IOException
Writes an instance of String to a DataOutput. This method will handle a null value and not throw a NullPointerException like DataOutput.writeUTF(java.lang.String), DataOutput.writeChars(java.lang.String), and DataOutput.writeBytes(java.lang.String) do.

Throws:
IOException - A problem occurs while writing to out
See Also:
readString(java.io.DataInput)

readString

public static String readString(DataInput in)
                         throws IOException
Reads an instance of String from a DataInput. The return value may be null.

Throws:
IOException - A problem occurs while reading from in
See Also:
writeString(java.lang.String, java.io.DataOutput)

writeBoolean

public static void writeBoolean(Boolean value,
                                DataOutput out)
                         throws IOException
Writes an instance of Boolean to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readBoolean(java.io.DataInput)

readBoolean

public static Boolean readBoolean(DataInput in)
                           throws IOException
Reads an instance of Boolean from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeCharacter

public static void writeCharacter(Character value,
                                  DataOutput out)
                           throws IOException
Writes an instance of Character to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readCharacter(java.io.DataInput)

readCharacter

public static Character readCharacter(DataInput in)
                               throws IOException
Reads an instance of Character from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeByte

public static void writeByte(Byte value,
                             DataOutput out)
                      throws IOException
Writes an instance of Byte to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readByte(java.io.DataInput)

readByte

public static Byte readByte(DataInput in)
                     throws IOException
Reads an instance of Byte from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeShort

public static void writeShort(Short value,
                              DataOutput out)
                       throws IOException
Writes an instance of Short to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readShort(java.io.DataInput)

readShort

public static Short readShort(DataInput in)
                       throws IOException
Reads an instance of Short from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeInteger

public static void writeInteger(Integer value,
                                DataOutput out)
                         throws IOException
Writes an instance of Integer to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readInteger(java.io.DataInput)

readInteger

public static Integer readInteger(DataInput in)
                           throws IOException
Reads an instance of Integer from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeLong

public static void writeLong(Long value,
                             DataOutput out)
                      throws IOException
Writes an instance of Long to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readLong(java.io.DataInput)

readLong

public static Long readLong(DataInput in)
                     throws IOException
Reads an instance of Long from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeFloat

public static void writeFloat(Float value,
                              DataOutput out)
                       throws IOException
Writes an instance of Float to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readFloat(java.io.DataInput)

readFloat

public static Float readFloat(DataInput in)
                       throws IOException
Reads an instance of Float from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writeDouble

public static void writeDouble(Double value,
                               DataOutput out)
                        throws IOException
Writes an instance of Double to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readDouble(java.io.DataInput)

readDouble

public static Double readDouble(DataInput in)
                         throws IOException
Reads an instance of Double from a DataInput.

Throws:
IOException - A problem occurs while reading from in

writePrimitiveBoolean

public static void writePrimitiveBoolean(boolean value,
                                         DataOutput out)
                                  throws IOException
Writes a primitive boolean to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeBoolean(boolean)

readPrimitiveBoolean

public static boolean readPrimitiveBoolean(DataInput in)
                                    throws IOException
Reads a primitive boolean from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readBoolean()

writePrimitiveByte

public static void writePrimitiveByte(byte value,
                                      DataOutput out)
                               throws IOException
Writes a primitive byte to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeByte(int)

readPrimitiveByte

public static byte readPrimitiveByte(DataInput in)
                              throws IOException
Reads a primitive byte from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readByte()

writePrimitiveChar

public static void writePrimitiveChar(char value,
                                      DataOutput out)
                               throws IOException
Writes a primitive char to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeChar(int)

readPrimitiveChar

public static char readPrimitiveChar(DataInput in)
                              throws IOException
Reads a primitive char from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readChar()

writePrimitiveShort

public static void writePrimitiveShort(short value,
                                       DataOutput out)
                                throws IOException
Writes a primitive short to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeShort(int)

readPrimitiveShort

public static short readPrimitiveShort(DataInput in)
                                throws IOException
Reads a primitive short from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readShort()

writeUnsignedByte

public static void writeUnsignedByte(int value,
                                     DataOutput out)
                              throws IOException
Writes a primitive int as an unsigned byte to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeByte(int), DataInput.readUnsignedByte()

readUnsignedByte

public static int readUnsignedByte(DataInput in)
                            throws IOException
Reads a primitive int as an unsigned byte from a DataInput using DataInput.readUnsignedByte().

Throws:
IOException - A problem occurs while reading from in
Since:
5.1

writeUnsignedShort

public static void writeUnsignedShort(int value,
                                      DataOutput out)
                               throws IOException
Writes a primitive int as an unsigned short to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeShort(int), DataInput.readUnsignedShort()

readUnsignedShort

public static int readUnsignedShort(DataInput in)
                             throws IOException
Reads a primitive int as an unsigned short from a DataInput using DataInput.readUnsignedShort().

Throws:
IOException - A problem occurs while reading from in
Since:
5.1

writePrimitiveInt

public static void writePrimitiveInt(int value,
                                     DataOutput out)
                              throws IOException
Writes a primitive int to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
DataOutput.writeInt(int)

readPrimitiveInt

public static int readPrimitiveInt(DataInput in)
                            throws IOException
Reads a primitive int from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readInt()

writePrimitiveLong

public static void writePrimitiveLong(long value,
                                      DataOutput out)
                               throws IOException
Writes a primitive long to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeLong(long)

readPrimitiveLong

public static long readPrimitiveLong(DataInput in)
                              throws IOException
Reads a primitive long from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readLong()

writePrimitiveFloat

public static void writePrimitiveFloat(float value,
                                       DataOutput out)
                                throws IOException
Writes a primitive float to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeFloat(float)

readPrimitiveFloat

public static float readPrimitiveFloat(DataInput in)
                                throws IOException
Reads a primitive float from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readFloat()

writePrimitiveDouble

public static void writePrimitiveDouble(double value,
                                        DataOutput out)
                                 throws IOException
Writes a primtive double to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
Since:
5.1
See Also:
DataOutput.writeDouble(double)

readPrimitiveDouble

public static double readPrimitiveDouble(DataInput in)
                                  throws IOException
Reads a primitive double from a DataInput.

Throws:
IOException - A problem occurs while reading from in
Since:
5.1
See Also:
DataInput.readDouble()

writeByteArray

public static void writeByteArray(byte[] array,
                                  DataOutput out)
                           throws IOException
Writes an array of bytes to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readByteArray(java.io.DataInput)

writeByteArray

public static void writeByteArray(byte[] array,
                                  int len,
                                  DataOutput out)
                           throws IOException
Writes the first len elements of an array of bytes to a DataOutput.

Parameters:
len - the actual number of entries to write. If len is greater than then length of the array then the entire array is written.
Throws:
IOException - A problem occurs while writing to out
See Also:
readByteArray(java.io.DataInput)

writeObjectAsByteArray

public static void writeObjectAsByteArray(Object obj,
                                          DataOutput out)
                                   throws IOException
Serialize the given object obj into a byte array using writeObject(Object, DataOutput) and then writes the byte array to the given data output out in the same format writeByteArray(byte[], DataOutput) does.

Parameters:
obj - the object to serialize and write
out - the data output to write the byte array to
Throws:
IllegalArgumentException - if a problem occurs while serialize obj
IOException - if a problem occurs while writing to out
Since:
5.0.2
See Also:
readByteArray(java.io.DataInput)

readByteArray

public static byte[] readByteArray(DataInput in)
                            throws IOException
Reads an array of bytes from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeByteArray(byte[], DataOutput)

writeStringArray

public static void writeStringArray(String[] array,
                                    DataOutput out)
                             throws IOException
Writes an array of Strings to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readStringArray(java.io.DataInput), writeString(java.lang.String, java.io.DataOutput)

readStringArray

public static String[] readStringArray(DataInput in)
                                throws IOException
Reads an array of Strings from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeStringArray(java.lang.String[], java.io.DataOutput)

writeShortArray

public static void writeShortArray(short[] array,
                                   DataOutput out)
                            throws IOException
Writes an array of shorts to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readShortArray(java.io.DataInput)

readShortArray

public static short[] readShortArray(DataInput in)
                              throws IOException
Reads an array of shorts from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeShortArray(short[], java.io.DataOutput)

writeIntArray

public static void writeIntArray(int[] array,
                                 DataOutput out)
                          throws IOException
Writes an int array to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readIntArray(java.io.DataInput)

readIntArray

public static int[] readIntArray(DataInput in)
                          throws IOException
Reads an int array from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeIntArray(int[], java.io.DataOutput)

writeLongArray

public static void writeLongArray(long[] array,
                                  DataOutput out)
                           throws IOException
Writes an array of longs to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readLongArray(java.io.DataInput)

readLongArray

public static long[] readLongArray(DataInput in)
                            throws IOException
Reads an array of longs from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeLongArray(long[], java.io.DataOutput)

writeFloatArray

public static void writeFloatArray(float[] array,
                                   DataOutput out)
                            throws IOException
Writes an array of floats to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readFloatArray(java.io.DataInput)

readFloatArray

public static float[] readFloatArray(DataInput in)
                              throws IOException
Reads an array of floats from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeFloatArray(float[], java.io.DataOutput)

writeDoubleArray

public static void writeDoubleArray(double[] array,
                                    DataOutput out)
                             throws IOException
Writes an array of doubles to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readDoubleArray(java.io.DataInput)

readDoubleArray

public static double[] readDoubleArray(DataInput in)
                                throws IOException
Reads an array of doubles from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeDoubleArray(double[], java.io.DataOutput)

writeObjectArray

public static void writeObjectArray(Object[] array,
                                    DataOutput out)
                             throws IOException
Writes an array of Objects to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readObjectArray(java.io.DataInput), writeObject(Object, DataOutput)

readObjectArray

public static Object[] readObjectArray(DataInput in)
                                throws IOException,
                                       ClassNotFoundException
Reads an array of Objects from a DataInput.

Throws:
IOException - A problem occurs while writing to out
ClassNotFoundException
See Also:
writeObjectArray(java.lang.Object[], java.io.DataOutput), readObject(java.io.DataInput)

writeArrayList

public static void writeArrayList(ArrayList list,
                                  DataOutput out)
                           throws IOException
Writes an ArrayList to a DataOutput. Note that even though list may be an instance of a subclass of ArrayList, readArrayList will always return an instance of ArrayList, not an instance of the subclass. To preserve the class type of list, writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readArrayList(java.io.DataInput)

readArrayList

public static ArrayList readArrayList(DataInput in)
                               throws IOException,
                                      ClassNotFoundException
Reads an ArrayList from a DataInput.

Throws:
IOException - A problem occurs while writing to out
ClassNotFoundException - The class of one of the ArrayList's elements cannot be found.
See Also:
writeArrayList(java.util.ArrayList, java.io.DataOutput)

writeLinkedList

public static void writeLinkedList(LinkedList list,
                                   DataOutput out)
                            throws IOException
Writes an LinkedList to a DataOutput. Note that even though list may be an instance of a subclass of LinkedList, readLinkedList will always return an instance of LinkedList, not an instance of the subclass. To preserve the class type of list, writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readLinkedList(java.io.DataInput)

readLinkedList

public static LinkedList readLinkedList(DataInput in)
                                 throws IOException,
                                        ClassNotFoundException
Reads an LinkedList from a DataInput.

Throws:
IOException - A problem occurs while writing to out
ClassNotFoundException - The class of one of the LinkedList's elements cannot be found.
See Also:
writeLinkedList(java.util.LinkedList, java.io.DataOutput)

writeHashSet

public static void writeHashSet(HashSet set,
                                DataOutput out)
                         throws IOException
Writes a HashSet to a DataOutput. Note that even though set may be an instance of a subclass of HashSet, readHashSet will always return an instance of HashSet, not an instance of the subclass. To preserve the class type of set, writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readHashSet(java.io.DataInput)

readHashSet

public static HashSet readHashSet(DataInput in)
                           throws IOException,
                                  ClassNotFoundException
Reads a HashSet from a DataInput.

Throws:
IOException - A problem occurs while writing to out
ClassNotFoundException - The class of one of the HashSet's elements cannot be found.
See Also:
writeHashSet(java.util.HashSet, java.io.DataOutput)

writeHashMap

public static void writeHashMap(HashMap map,
                                DataOutput out)
                         throws IOException
Writes a HashMap to a DataOutput. Note that even though map may be an instance of a subclass of HashMap, readHashMap will always return an instance of HashMap, not an instance of the subclass. To preserve the class type of map, writeObject(Object, DataOutput) should be used for data serialization.

Throws:
IOException - A problem occurs while writing to out
See Also:
readHashMap(java.io.DataInput)

readHashMap

public static HashMap readHashMap(DataInput in)
                           throws IOException,
                                  ClassNotFoundException
Reads a HashMap from a DataInput.

Throws:
IOException - A problem occurs while writing to out
ClassNotFoundException - The class of one of the HashMap's elements cannot be found.
See Also:
writeHashMap(java.util.HashMap, java.io.DataOutput)

writeProperties

public static void writeProperties(Properties props,
                                   DataOutput out)
                            throws IOException
Writes a Properties object to a DataOutput.

Throws:
IOException - A problem occurred while writing to out
See Also:
readProperties(java.io.DataInput)

readProperties

public static Properties readProperties(DataInput in)
                                 throws IOException,
                                        ClassNotFoundException
Reads a serialized Properties object from a DataInput.

Throws:
IOException - A problem occurred while reading from DataInput
ClassNotFoundException - The class of one of the Properties's elements cannot be found.
See Also:
writeProperties(java.util.Properties, java.io.DataOutput)

writeTimeUnit

public static void writeTimeUnit(com.gemstone.bp.edu.emory.mathcs.backport.java.util.concurrent.TimeUnit unit,
                                 DataOutput out)
                          throws IOException
Writes a TimeUnit to a DataOutput.

Throws:
IOException - A problem occurs while writing to out
See Also:
readTimeUnit(java.io.DataInput)

readTimeUnit

public static com.gemstone.bp.edu.emory.mathcs.backport.java.util.concurrent.TimeUnit readTimeUnit(DataInput in)
                                                                                            throws IOException
Reads a TimeUnit from a DataInput.

Throws:
IOException - A problem occurs while writing to out
See Also:
writeTimeUnit(com.gemstone.bp.edu.emory.mathcs.backport.java.util.concurrent.TimeUnit, java.io.DataOutput)

writeUserDataSerializableHeader

public static void writeUserDataSerializableHeader(byte classId,
                                                   DataOutput out)
                                            throws IOException
Throws:
IOException

writeObject

public static void writeObject(Object o,
                               DataOutput out,
                               boolean allowJavaSerialization)
                        throws IOException
Writes an arbitrary object to a DataOutput. If o is not an instance of a specially-handled standard Java class (such as Date, Integer, or ArrayList), the toData method of each registered DataSerializer is invoked until the object is serialized. If no registered serializer can serialize the object and o does not implement DataSerializable, then it is serialized to out using standard Java serialization.

Parameters:
allowJavaSerialization - If false, then a NotSerializableException is thrown in the case where standard Java serialization would otherwise be used for object o or for any nested subobject of o. This is used to prevent Java serialization from being used when sending data to a non-Java client
Throws:
IOException - A problem occurs while writing o to out
See Also:
readObject(DataInput), Instantiator, ObjectOutputStream.writeObject(java.lang.Object)

writeObject

public static void writeObject(Object o,
                               DataOutput out)
                        throws IOException
Writes an arbitrary object to a DataOutput. If o is not an instance of a specially-handled standard Java class (such as Date, Integer, or ArrayList), the toData method of each registered DataSerializer is invoked until the object is serialized. If no registered serializer can serialize the object and o does not implement DataSerializable, then it is serialized to out using standard Java serialization.

Throws:
IOException - A problem occurs while writing o to out
See Also:
readObject(DataInput), Instantiator, ObjectOutputStream.writeObject(java.lang.Object)

readObject

public static Object readObject(DataInput in)
                         throws IOException,
                                ClassNotFoundException
Reads an arbitrary object from a DataInput. Instances of classes that are not handled specially (such as String, Class, and DataSerializable) are read using standard Java serialization.

Note that if an object is deserialized using standard Java serialization, its class will be loaded using the current thread's context class loader before the one normally used by Java serialization is consulted.

Throws:
IOException - A problem occured while reading from in (may wrap another exception)
ClassNotFoundException - The class of an object read from in could not be found
See Also:
writeObject(Object, DataOutput), ObjectInputStream.readObject()

register

public static DataSerializer register(Class c,
                                      byte id)
Registers a DataSerializer class with the data serialization framework. This method uses reflection to create an instance of the DataSerializer class by invoking its zero-argument constructor.

The DataSerializer instance will be consulted by the writeObject(Object, DataOutput) and readObject(java.io.DataInput) methods. Note that the order in which serializers are consulted is not specified. Therefore, no two serializers should not be able to serialize instances of the same class.

This method invokes the DataSerializer instance's getSupportedClasses() method and keeps track of which classes can have their instances serialized by s.

Parameters:
c - A DataSerializer class whose instance should be registered with the data serialization framework.
id - The unique id of the DataSerializer
Returns:
The instance of DataSerializer that was registered.
Throws:
IllegalArgumentException - If c does not subclass DataSerializer, if c does not have a zero-argument constructor, if another serializer instance with id id has already been registered, if id is 0.
See Also:
getSupportedClasses()

getSupportedClasses

public abstract Class[] getSupportedClasses()
Returns the Classes whose instances are data serialized by this DataSerializer. This method is invoked when this serializer is registered. The information provided by this method allows the data serialization framework to optimize the behavior of the writeObject(Object, DataOutput) method. Instead of performing a handful of instanceof operations and potentially invoking DataSerializers that cannot serialize the object, the framework can quickly select which serializer should be invoked.


toData

public abstract boolean toData(Object o,
                               DataOutput out)
                        throws IOException
Data serializes an object to a DataOutput. It is very important that when performing the "switch" on o's class, your code test for a subclass before it tests for a superclass. Otherwise, the incorrect class id could be written to the serialization stream.

Parameters:
o - The object to data serialize. It will never be null.
Returns:
false if this DataSerializer does not know how to data serialize o.
Throws:
IOException

fromData

public abstract Object fromData(DataInput in)
                         throws IOException,
                                ClassNotFoundException
Reads an object from a DataInput. An implementation of this method may take the form:

 

Throws:
IOException - If this serializer cannot read an object from in.
ClassNotFoundException
See Also:
toData(java.lang.Object, java.io.DataOutput)

getId

public byte getId()
Returns the id of this DataSerializer.


equals

public boolean equals(Object o)
Two DataSerializers are consider to be equal if they have the same id.

Overrides:
equals in class Object

GemFire 5.5

Copyright © 2002-2008 GemStone Systems, Inc. All Rights Reserved.