ObjectOutputStream.ReplaceObject(Object) Method

Definition

This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization.

[Android.Runtime.Register("replaceObject", "(Ljava/lang/Object;)Ljava/lang/Object;", "GetReplaceObject_Ljava_lang_Object_Handler")]
protected virtual Java.Lang.Object? ReplaceObject (Java.Lang.Object? obj);
[<Android.Runtime.Register("replaceObject", "(Ljava/lang/Object;)Ljava/lang/Object;", "GetReplaceObject_Ljava_lang_Object_Handler")>]
abstract member ReplaceObject : Java.Lang.Object -> Java.Lang.Object
override this.ReplaceObject : Java.Lang.Object -> Java.Lang.Object

Parameters

obj
Object

the object to be replaced

Returns

the alternate object that replaced the specified one

Attributes

Exceptions

if any I/O error occurs while creating the replacement object.

Remarks

This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization. Replacing objects is disabled until enableReplaceObject is called. The enableReplaceObject method checks that the stream requesting to do replacement can be trusted. The first occurrence of each object written into the serialization stream is passed to replaceObject. Subsequent references to the object are replaced by the object returned by the original call to replaceObject. To ensure that the private state of objects is not unintentionally exposed, only trusted streams may use replaceObject.

The ObjectOutputStream.writeObject method takes a parameter of type Object (as opposed to type Serializable) to allow for cases where non-serializable objects are replaced by serializable ones.

When a subclass is replacing objects it must insure that either a complementary substitution must be made during deserialization or that the substituted object is compatible with every field where the reference will be stored. Objects whose type is not a subclass of the type of the field or array element abort the serialization by raising an exception and the object is not be stored.

This method is called only once when each object is first encountered. All subsequent references to the object will be redirected to the new object. This method should return the object to be substituted or the original object.

Null can be returned as the object to be substituted, but may cause NullReferenceException in classes that contain references to the original object since they may be expecting an object instead of null.

Java documentation for java.io.ObjectOutputStream.replaceObject(java.lang.Object).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also