Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ObjectOutputStream

public class ObjectOutputStream
extends OutputStream implements ObjectOutput, ObjectStreamConstants

java.lang.Object
   ↳ java.io.OutputStream
     ↳ java.io.ObjectOutputStream


A specialized OutputStream that is able to write (serialize) Java objects as well as primitive data types (int, byte, char etc.). The data can later be loaded using an ObjectInputStream.

See also:

Summary

Nested classes

class ObjectOutputStream.PutField

PutField is an inner class to provide access to the persistent fields that are written to the target stream. 

Inherited constants

From interface java.io.ObjectStreamConstants

Inherited fields

From interface java.io.ObjectStreamConstants

Public constructors

ObjectOutputStream(OutputStream output)

Constructs a new ObjectOutputStream that writes to the OutputStream output.

Protected constructors

ObjectOutputStream()

Constructs a new ObjectOutputStream.

Public methods

void close()

Closes this stream.

void defaultWriteObject()

Default method to write objects to this stream.

void flush()

Writes buffered data to the target stream and calls the flush method of the target stream.

ObjectOutputStream.PutField putFields()

Gets this stream's PutField object.

void reset()

Resets the state of this stream.

void useProtocolVersion(int version)

Sets the specified protocol version to be used by this stream.

void write(int value)

Writes a single byte to the target stream.

void write(byte[] buffer, int offset, int length)

Writes count bytes from the byte array buffer starting at offset index to the target stream.

void writeBoolean(boolean value)

Writes a boolean to the target stream.

void writeByte(int value)

Writes a byte (8 bit) to the target stream.

void writeBytes(String value)

Writes the string value as a sequence of bytes to the target stream.

void writeChar(int value)

Writes a character (16 bit) to the target stream.

void writeChars(String value)

Writes the string value as a sequence of characters to the target stream.

void writeDouble(double value)

Writes a double (64 bit) to the target stream.

void writeFields()

Writes the fields of the object currently being written to the target stream.

void writeFloat(float value)

Writes a float (32 bit) to the target stream.

void writeInt(int value)

Writes an integer (32 bit) to the target stream.

void writeLong(long value)

Writes a long (64 bit) to the target stream.

final void writeObject(Object object)

Writes an object to the target stream.

void writeShort(int value)

Writes a short (16 bit) to the target stream.

void writeUTF(String value)

Writes a string encoded with modified UTF-8 to the target stream.

void writeUnshared(Object object)

Writes an unshared object to the target stream.

Protected methods

void annotateClass(Class<?> aClass)

Writes optional information for class aClass to the output stream.

void annotateProxyClass(Class<?> aClass)

Writes optional information for a proxy class to the target stream.

void drain()

Writes buffered data to the target stream.

boolean enableReplaceObject(boolean enable)

Enables object replacement for this stream.

Object replaceObject(Object object)

Allows trusted subclasses to substitute the specified original object with a new object.

void writeClassDescriptor(ObjectStreamClass classDesc)

Writes a class descriptor to the target stream.

void writeObjectOverride(Object object)

Method to be overridden by subclasses to write object to the target stream.

void writeStreamHeader()

Writes the ObjectOutputStream header to the target stream.

Inherited methods

From class java.io.OutputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.io.ObjectOutput
From interface java.lang.AutoCloseable
From interface java.io.DataOutput

Public constructors

ObjectOutputStream

Added in API level 1
ObjectOutputStream (OutputStream output)

Constructs a new ObjectOutputStream that writes to the OutputStream output.

Parameters
output OutputStream: the non-null OutputStream to filter writes on.
Throws
IOException if an error occurs while writing the object stream header

Protected constructors

ObjectOutputStream

Added in API level 1
ObjectOutputStream ()

Constructs a new ObjectOutputStream. This default constructor can be used by subclasses that do not want to use the public constructor if it allocates unneeded data.

Throws
IOException if an error occurs when creating this stream.

Public methods

close

Added in API level 1
void close ()

Closes this stream. Any buffered data is flushed. This implementation closes the target stream.

Throws
IOException if an error occurs while closing this stream.

defaultWriteObject

Added in API level 1
void defaultWriteObject ()

Default method to write objects to this stream. Serializable fields defined in the object's class and superclasses are written to the output stream.

Throws
IOException if an error occurs while writing to the target stream.
NotActiveException if this method is not called from writeObject().

See also:

flush

Added in API level 1
void flush ()

Writes buffered data to the target stream and calls the flush method of the target stream.

Throws
IOException if an error occurs while writing to or flushing the output stream.

putFields

Added in API level 1
ObjectOutputStream.PutField putFields ()

Gets this stream's PutField object. This object provides access to the persistent fields that are eventually written to the output stream. It is used to transfer the values from the fields of the object that is currently being written to the persistent fields.

Returns
ObjectOutputStream.PutField the PutField object from which persistent fields can be accessed by name.
Throws
IOException if an I/O error occurs.
NotActiveException if this method is not called from writeObject().

See also:

reset

Added in API level 1
void reset ()

Resets the state of this stream. A marker is written to the stream, so that the corresponding input stream will also perform a reset at the same point. Objects previously written are no longer remembered, so they will be written again (instead of a cyclical reference) if found in the object graph.

Throws
IOException if reset() is called during the serialization of an object.

useProtocolVersion

Added in API level 1
void useProtocolVersion (int version)

Sets the specified protocol version to be used by this stream.

Parameters
version int: the protocol version to be used. Use a PROTOCOL_VERSION_x constant from java.io.ObjectStreamConstants.
Throws
IllegalArgumentException if an invalid version is specified.
IOException if an I/O error occurs.

See also:

write

Added in API level 1
void write (int value)

Writes a single byte to the target stream. Only the least significant byte of the integer value is written to the stream. Blocks until the byte is actually written.

Parameters
value int: the byte to write.
Throws
IOException if an error occurs while writing to the target stream.

write

Added in API level 1
void write (byte[] buffer, 
                int offset, 
                int length)

Writes count bytes from the byte array buffer starting at offset index to the target stream. Blocks until all bytes are written.

Parameters
buffer byte: the buffer to write.
offset int: the index of the first byte in buffer to write.
length int: the number of bytes from buffer to write to the output stream.
Throws
IOException if an error occurs while writing to the target stream.

writeBoolean

Added in API level 1
void writeBoolean (boolean value)

Writes a boolean to the target stream.

Parameters
value boolean: the boolean value to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeByte

Added in API level 1
void writeByte (int value)

Writes a byte (8 bit) to the target stream.

Parameters
value int: the byte to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeBytes

Added in API level 1
void writeBytes (String value)

Writes the string value as a sequence of bytes to the target stream. Only the least significant byte of each character in the string is written.

Parameters
value String: the string to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeChar

Added in API level 1
void writeChar (int value)

Writes a character (16 bit) to the target stream.

Parameters
value int: the character to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeChars

Added in API level 1
void writeChars (String value)

Writes the string value as a sequence of characters to the target stream.

Parameters
value String: the string to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeDouble

Added in API level 1
void writeDouble (double value)

Writes a double (64 bit) to the target stream.

Parameters
value double: the double to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeFields

Added in API level 1
void writeFields ()

Writes the fields of the object currently being written to the target stream. The field values are buffered in the currently active PutField object, which can be accessed by calling putFields().

Throws
IOException if an error occurs while writing to the target stream.
NotActiveException if there are no fields to write to the target stream.

See also:

writeFloat

Added in API level 1
void writeFloat (float value)

Writes a float (32 bit) to the target stream.

Parameters
value float: the float to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeInt

Added in API level 1
void writeInt (int value)

Writes an integer (32 bit) to the target stream.

Parameters
value int: the integer to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeLong

Added in API level 1
void writeLong (long value)

Writes a long (64 bit) to the target stream.

Parameters
value long: the long to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeObject

Added in API level 1
void writeObject (Object object)

Writes an object to the target stream.

Parameters
object Object: the object to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

See also:

writeShort

Added in API level 1
void writeShort (int value)

Writes a short (16 bit) to the target stream.

Parameters
value int: the short to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeUTF

Added in API level 1
void writeUTF (String value)

Writes a string encoded with modified UTF-8 to the target stream.

Parameters
value String: the string to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeUnshared

Added in API level 1
void writeUnshared (Object object)

Writes an unshared object to the target stream. This method is identical to writeObject, except that it always writes a new object to the stream versus the use of back-referencing for identical objects by writeObject.

Parameters
object Object: the object to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

See also:

Protected methods

annotateClass

Added in API level 1
void annotateClass (Class<?> aClass)

Writes optional information for class aClass to the output stream. This optional data can be read when deserializing the class descriptor (ObjectStreamClass) for this class from an input stream. By default, no extra data is saved.

Parameters
aClass Class: the class to annotate.
Throws
IOException if an error occurs while writing to the target stream.

See also:

annotateProxyClass

Added in API level 1
void annotateProxyClass (Class<?> aClass)

Writes optional information for a proxy class to the target stream. This optional data can be read when deserializing the proxy class from an input stream. By default, no extra data is saved.

Parameters
aClass Class: the proxy class to annotate.
Throws
IOException if an error occurs while writing to the target stream.

See also:

drain

Added in API level 1
void drain ()

Writes buffered data to the target stream. This is similar to flush but the flush is not propagated to the target stream.

Throws
IOException if an error occurs while writing to the target stream.

enableReplaceObject

Added in API level 1
boolean enableReplaceObject (boolean enable)

Enables object replacement for this stream. By default this is not enabled. Only trusted subclasses (loaded with system class loader) are allowed to change this status.

Parameters
enable boolean: true to enable object replacement; false to disable it.
Returns
boolean the previous setting.

See also:

replaceObject

Added in API level 1
Object replaceObject (Object object)

Allows trusted subclasses to substitute the specified original object with a new object. Object substitution has to be activated first with calling enableReplaceObject(true). This implementation just returns object.

Parameters
object Object: the original object for which a replacement may be defined.
Returns
Object the replacement object for object.
Throws
IOException if any I/O error occurs while creating the replacement object.

See also:

writeClassDescriptor

Added in API level 1
void writeClassDescriptor (ObjectStreamClass classDesc)

Writes a class descriptor to the target stream.

Parameters
classDesc ObjectStreamClass: the class descriptor to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeObjectOverride

Added in API level 1
void writeObjectOverride (Object object)

Method to be overridden by subclasses to write object to the target stream.

Parameters
object Object: the object to write to the target stream.
Throws
IOException if an error occurs while writing to the target stream.

writeStreamHeader

Added in API level 1
void writeStreamHeader ()

Writes the ObjectOutputStream header to the target stream.

Throws
IOException if an error occurs while writing to the target stream.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.