Interface DataSerializable

All Superinterfaces:
Serializable
All Known Subinterfaces:
CollectionType, MapType, ObjectType, PersistentID, StructType
All Known Implementing Classes:
ExpirationAttributes, MembershipAttributes, RegionSubRegionSnapshot, ServerLoad, SubscriptionAttributes

public interface DataSerializable extends Serializable
An interface for objects whose state can be written/read as primitive types and strings ("data"). That is, instead of serializing itself to an ObjectOutputStream, a DataSerializable can serialize itself to a DataOutput. By implementing this interface, objects can be serialized faster and in a more compact format than standard Java serialization. The DataSerializer class contains a number of static methods that may be helpful to implementations of DataSerializable.

When possible, GemFire respects the DataSerializable contract to provide optimal object serialization. For instance, if a DataSerializable object is placed into a distributed cache region, its toData method will be used to serialize it when it is sent to another member of the distributed system.

To avoid the overhead of Java reflection, DataSerializable classes may register an Instantiator to be used during deserialization. Alternatively, classes that implement DataSerializable can provide a zero-argument constructor that will be invoked when they are read with DataSerializer.readObject(java.io.DataInput).

Some classes (especially third-party classes that you may not have the source code to) cannot be modified to implement DataSerializable. These classes can be data serialized by an instance of DataSerializer.

DataSerializable offers improved performance over standard Java serialization, but does not offer all of the features of standard Java serialization. In particular, data serialization does not attempt to maintain referential integrity among the objects it is writing or reading. As a result, data serialization should not be used with complex object graphs. Attempting to data serialize graphs that contain object cycles will result in infinite recursion and a StackOverflowError. Attempting to deserialize an object graph that contains multiple reference paths to the same object will result in multiple copies of the objects that are referred to through multiple paths.

Examples of
 situations in which DataSerializable behaviour differs from Java serialization
Since:
GemFire 3.5
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Replaceable allows an object to write an alternative version of itself to a DataOutput.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Reads the state of this object as primitive data from the given DataInput.
    void
    Writes the state of this object as primitive data to the given DataOutput.
  • Method Details

    • toData

      void toData(DataOutput out) throws IOException
      Writes the state of this object as primitive data to the given DataOutput.

      Since 5.7 it is possible for any method call to the specified DataOutput to throw GemFireRethrowable. It should not be caught by user code. If it is it must be rethrown.

      Parameters:
      out - the DataOutput to write to
      Throws:
      IOException - A problem occurs while writing to out
    • fromData

      void fromData(DataInput in) throws IOException, ClassNotFoundException
      Reads the state of this object as primitive data from the given DataInput.
      Parameters:
      in - the DataInput to read from
      Throws:
      IOException - A problem occurs while reading from in
      ClassNotFoundException - A class could not be loaded while reading from in