public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants
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.ObjectInputStream,
ObjectOutput,
Serializable,
Externalizable| Modifier and Type | Class and Description |
|---|---|
static class |
ObjectOutputStream.PutField
PutField is an inner class to provide access to the persistent fields
that are written to the target stream.
|
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING| Modifier | Constructor and Description |
|---|---|
protected |
ObjectOutputStream()
Constructs a new
ObjectOutputStream. |
|
ObjectOutputStream(OutputStream output)
Constructs a new ObjectOutputStream that writes to the OutputStream
output. |
| Modifier and Type | Method and Description |
|---|---|
protected void |
annotateClass(Class<?> aClass)
Writes optional information for class
aClass to the output
stream. |
protected void |
annotateProxyClass(Class<?> aClass)
Writes optional information for a proxy class to the target stream.
|
void |
close()
Closes this stream.
|
void |
defaultWriteObject()
Default method to write objects to this stream.
|
protected void |
drain()
Writes buffered data to the target stream.
|
protected boolean |
enableReplaceObject(boolean enable)
Enables object replacement for 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. |
protected Object |
replaceObject(Object object)
Allows trusted subclasses to substitute the specified original
object with a new 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(byte[] buffer,
int offset,
int length)
Writes
count bytes from the byte array buffer starting at
offset index to the target stream. |
void |
write(int value)
Writes a single byte 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. |
protected void |
writeClassDescriptor(ObjectStreamClass classDesc)
Writes a class descriptor 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.
|
void |
writeObject(Object object)
Writes an object to the target stream.
|
protected void |
writeObjectOverride(Object object)
Method to be overridden by subclasses to write
object to the
target stream. |
void |
writeShort(int value)
Writes a short (16 bit) to the target stream.
|
protected void |
writeStreamHeader()
Writes the
ObjectOutputStream header to the target stream. |
void |
writeUnshared(Object object)
Writes an unshared object to the target stream.
|
void |
writeUTF(String value)
Writes a string encoded with
modified UTF-8 to the
target stream. |
nullOutputStream, writeclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitwriteprotected ObjectOutputStream()
throws IOException
ObjectOutputStream. This default constructor can
be used by subclasses that do not want to use the public constructor if
it allocates unneeded data.IOException - if an error occurs when creating this stream.public ObjectOutputStream(OutputStream output) throws IOException
output.output - the non-null OutputStream to filter writes on.IOException - if an error occurs while writing the object stream
headerprotected void annotateClass(Class<?> aClass) throws IOException
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.aClass - the class to annotate.IOException - if an error occurs while writing to the target stream.ObjectInputStream.resolveClass(ObjectStreamClass)protected void annotateProxyClass(Class<?> aClass) throws IOException
aClass - the proxy class to annotate.IOException - if an error occurs while writing to the target stream.ObjectInputStream.resolveProxyClass(String[])public void close()
throws IOException
close in interface Closeableclose in interface ObjectOutputclose in interface AutoCloseableclose in class OutputStreamIOException - if an error occurs while closing this stream.public void defaultWriteObject()
throws IOException
IOException - if an error occurs while writing to the target stream.NotActiveException - if this method is not called from writeObject().ObjectInputStream.defaultReadObject()protected void drain()
throws IOException
flush but the flush is not propagated to the target stream.IOException - if an error occurs while writing to the target stream.protected boolean enableReplaceObject(boolean enable)
enable - true to enable object replacement; false to
disable it.replaceObject(java.lang.Object),
ObjectInputStream.enableResolveObject(boolean)public void flush()
throws IOException
flush
method of the target stream.flush in interface Flushableflush in interface ObjectOutputflush in class OutputStreamIOException - if an error occurs while writing to or flushing the output
stream.public ObjectOutputStream.PutField putFields() throws IOException
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.IOException - if an I/O error occurs.NotActiveException - if this method is not called from writeObject().ObjectInputStream.defaultReadObject()protected Object replaceObject(Object object) throws IOException
object with a new object. Object substitution has to be activated first
with calling enableReplaceObject(true). This implementation just
returns object.object - the original object for which a replacement may be defined.object.IOException - if any I/O error occurs while creating the replacement
object.enableReplaceObject(boolean),
ObjectInputStream.enableResolveObject(boolean),
ObjectInputStream.resolveObject(java.lang.Object)public void reset()
throws IOException
IOException - if reset() is called during the serialization of an
object.public void useProtocolVersion(int version)
throws IOException
version - the protocol version to be used. Use a PROTOCOL_VERSION_x constant from java.io.ObjectStreamConstants.IllegalArgumentException - if an invalid version is specified.IOException - if an I/O error occurs.ObjectStreamConstants.PROTOCOL_VERSION_1,
ObjectStreamConstants.PROTOCOL_VERSION_2public void write(byte[] buffer,
int offset,
int length)
throws IOException
count bytes from the byte array buffer starting at
offset index to the target stream. Blocks until all bytes are
written.write in interface DataOutputwrite in interface ObjectOutputwrite in class OutputStreambuffer - the buffer to write.offset - the index of the first byte in buffer to write.length - the number of bytes from buffer to write to the output
stream.IOException - if an error occurs while writing to the target stream.public void write(int value)
throws IOException
value is written to the stream. Blocks until
the byte is actually written.write in interface DataOutputwrite in interface ObjectOutputwrite in class OutputStreamvalue - the byte to write.IOException - if an error occurs while writing to the target stream.DataInput.readByte()public void writeBoolean(boolean value)
throws IOException
writeBoolean in interface DataOutputvalue - the boolean value to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readBoolean()public void writeByte(int value)
throws IOException
writeByte in interface DataOutputvalue - the byte to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readByte(),
DataInput.readUnsignedByte()public void writeBytes(String value) throws IOException
value as a sequence of bytes to the target
stream. Only the least significant byte of each character in the string
is written.writeBytes in interface DataOutputvalue - the string to write to the target stream.IOException - if an error occurs while writing to the target stream.public void writeChar(int value)
throws IOException
writeChar in interface DataOutputvalue - the character to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readChar()public void writeChars(String value) throws IOException
value as a sequence of characters to the target
stream.writeChars in interface DataOutputvalue - the string to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readChar()public void writeDouble(double value)
throws IOException
writeDouble in interface DataOutputvalue - the double to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readDouble()public void writeFields()
throws IOException
PutField object, which can be accessed by calling putFields().IOException - if an error occurs while writing to the target stream.NotActiveException - if there are no fields to write to the target stream.putFields()public void writeFloat(float value)
throws IOException
writeFloat in interface DataOutputvalue - the float to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readFloat()public void writeInt(int value)
throws IOException
writeInt in interface DataOutputvalue - the integer to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readInt()public void writeLong(long value)
throws IOException
writeLong in interface DataOutputvalue - the long to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readLong()protected void writeClassDescriptor(ObjectStreamClass classDesc) throws IOException
classDesc - the class descriptor to write to the target stream.IOException - if an error occurs while writing to the target stream.public final void writeObject(Object object) throws IOException
writeObject in interface ObjectOutputobject - the object to write to the target stream.IOException - if an error occurs while writing to the target stream.ObjectInputStream.readObject()public void writeUnshared(Object object) throws IOException
writeObject, except that it always writes a new object to the
stream versus the use of back-referencing for identical objects by
writeObject.object - the object to write to the target stream.IOException - if an error occurs while writing to the target stream.ObjectInputStream.readUnshared()protected void writeObjectOverride(Object object) throws IOException
object to the
target stream.object - the object to write to the target stream.IOException - if an error occurs while writing to the target stream.public void writeShort(int value)
throws IOException
writeShort in interface DataOutputvalue - the short to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readShort(),
DataInput.readUnsignedShort()protected void writeStreamHeader()
throws IOException
ObjectOutputStream header to the target stream.IOException - if an error occurs while writing to the target stream.public void writeUTF(String value) throws IOException
modified UTF-8 to the
target stream.writeUTF in interface DataOutputvalue - the string to write to the target stream.IOException - if an error occurs while writing to the target stream.DataInput.readUTF()