Package java.io
Class ObjectOutputStream
java.lang.Object
java.io.OutputStream
java.io.ObjectOutputStream
- All Implemented Interfaces:
Closeable,DataOutput,Flushable,ObjectOutput,ObjectStreamConstants,AutoCloseable
public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants
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:
ObjectInputStream,ObjectOutput,Serializable,Externalizable
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObjectOutputStream.PutFieldPutField is an inner class to provide access to the persistent fields that are written to the target stream. -
Field Summary
Fields inherited from interface java.io.ObjectStreamConstants
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 -
Constructor Summary
Constructors Modifier Constructor Description protectedObjectOutputStream()Constructs a newObjectOutputStream.ObjectOutputStream(OutputStream output)Constructs a new ObjectOutputStream that writes to the OutputStreamoutput. -
Method Summary
Modifier and Type Method Description protected voidannotateClass(Class<?> aClass)Writes optional information for classaClassto the output stream.protected voidannotateProxyClass(Class<?> aClass)Writes optional information for a proxy class to the target stream.voidclose()Closes this stream.voiddefaultWriteObject()Default method to write objects to this stream.protected voiddrain()Writes buffered data to the target stream.protected booleanenableReplaceObject(boolean enable)Enables object replacement for this stream.voidflush()Writes buffered data to the target stream and calls theflushmethod of the target stream.ObjectOutputStream.PutFieldputFields()Gets this stream'sPutFieldobject.protected ObjectreplaceObject(Object object)Allows trusted subclasses to substitute the specified originalobjectwith a new object.voidreset()Resets the state of this stream.voiduseProtocolVersion(int version)Sets the specified protocol version to be used by this stream.voidwrite(byte[] buffer, int offset, int length)Writescountbytes from the byte arraybufferstarting at offsetindexto the target stream.voidwrite(int value)Writes a single byte to the target stream.voidwriteBoolean(boolean value)Writes a boolean to the target stream.voidwriteByte(int value)Writes a byte (8 bit) to the target stream.voidwriteBytes(String value)Writes the stringvalueas a sequence of bytes to the target stream.voidwriteChar(int value)Writes a character (16 bit) to the target stream.voidwriteChars(String value)Writes the stringvalueas a sequence of characters to the target stream.protected voidwriteClassDescriptor(ObjectStreamClass classDesc)Writes a class descriptor to the target stream.voidwriteDouble(double value)Writes a double (64 bit) to the target stream.voidwriteFields()Writes the fields of the object currently being written to the target stream.voidwriteFloat(float value)Writes a float (32 bit) to the target stream.voidwriteInt(int value)Writes an integer (32 bit) to the target stream.voidwriteLong(long value)Writes a long (64 bit) to the target stream.voidwriteObject(Object object)Writes an object to the target stream.protected voidwriteObjectOverride(Object object)Method to be overridden by subclasses to writeobjectto the target stream.voidwriteShort(int value)Writes a short (16 bit) to the target stream.protected voidwriteStreamHeader()Writes theObjectOutputStreamheader to the target stream.voidwriteUnshared(Object object)Writes an unshared object to the target stream.voidwriteUTF(String value)Writes a string encoded withmodified UTF-8to the target stream.Methods inherited from class java.io.OutputStream
writeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.io.ObjectOutput
write
-
Constructor Details
-
ObjectOutputStream
Constructs a newObjectOutputStream. 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.
-
ObjectOutputStream
Constructs a new ObjectOutputStream that writes to the OutputStreamoutput.- Parameters:
output- the non-null OutputStream to filter writes on.- Throws:
IOException- if an error occurs while writing the object stream header
-
-
Method Details
-
annotateClass
Writes optional information for classaClassto 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- the class to annotate.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
ObjectInputStream.resolveClass(ObjectStreamClass)
-
annotateProxyClass
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- the proxy class to annotate.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
ObjectInputStream.resolveProxyClass(String[])
-
close
Closes this stream. Any buffered data is flushed. This implementation closes the target stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceObjectOutput- Overrides:
closein classOutputStream- Throws:
IOException- if an error occurs while closing this stream.
-
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 fromwriteObject().- See Also:
ObjectInputStream.defaultReadObject()
-
drain
Writes buffered data to the target stream. This is similar toflushbut the flush is not propagated to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
enableReplaceObject
protected 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-trueto enable object replacement;falseto disable it.- Returns:
- the previous setting.
- See Also:
replaceObject(java.lang.Object),ObjectInputStream.enableResolveObject(boolean)
-
flush
Writes buffered data to the target stream and calls theflushmethod of the target stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin interfaceObjectOutput- Overrides:
flushin classOutputStream- Throws:
IOException- if an error occurs while writing to or flushing the output stream.
-
putFields
Gets this stream'sPutFieldobject. 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:
- 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 fromwriteObject().- See Also:
ObjectInputStream.defaultReadObject()
-
replaceObject
Allows trusted subclasses to substitute the specified originalobjectwith a new object. Object substitution has to be activated first with callingenableReplaceObject(true). This implementation just returnsobject.- Parameters:
object- the original object for which a replacement may be defined.- Returns:
- the replacement object for
object. - Throws:
IOException- if any I/O error occurs while creating the replacement object.- See Also:
enableReplaceObject(boolean),ObjectInputStream.enableResolveObject(boolean),ObjectInputStream.resolveObject(java.lang.Object)
-
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- ifreset()is called during the serialization of an object.
-
useProtocolVersion
Sets the specified protocol version to be used by this stream.- Parameters:
version- the protocol version to be used. Use aPROTOCOL_VERSION_xconstant fromjava.io.ObjectStreamConstants.- Throws:
IllegalArgumentException- if an invalidversionis specified.IOException- if an I/O error occurs.- See Also:
ObjectStreamConstants.PROTOCOL_VERSION_1,ObjectStreamConstants.PROTOCOL_VERSION_2
-
write
Writescountbytes from the byte arraybufferstarting at offsetindexto the target stream. Blocks until all bytes are written.- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceObjectOutput- Overrides:
writein classOutputStream- Parameters:
buffer- the buffer to write.offset- the index of the first byte inbufferto write.length- the number of bytes frombufferto write to the output stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
write
Writes a single byte to the target stream. Only the least significant byte of the integervalueis written to the stream. Blocks until the byte is actually written.- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceObjectOutput- Specified by:
writein classOutputStream- Parameters:
value- the byte to write.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readByte()
-
writeBoolean
Writes a boolean to the target stream.- Specified by:
writeBooleanin interfaceDataOutput- Parameters:
value- the boolean value to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readBoolean()
-
writeByte
Writes a byte (8 bit) to the target stream.- Specified by:
writeBytein interfaceDataOutput- Parameters:
value- the byte to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readByte(),DataInput.readUnsignedByte()
-
writeBytes
Writes the stringvalueas a sequence of bytes to the target stream. Only the least significant byte of each character in the string is written.- Specified by:
writeBytesin interfaceDataOutput- Parameters:
value- the string to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
writeChar
Writes a character (16 bit) to the target stream.- Specified by:
writeCharin interfaceDataOutput- Parameters:
value- the character to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readChar()
-
writeChars
Writes the stringvalueas a sequence of characters to the target stream.- Specified by:
writeCharsin interfaceDataOutput- Parameters:
value- the string to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readChar()
-
writeDouble
Writes a double (64 bit) to the target stream.- Specified by:
writeDoublein interfaceDataOutput- Parameters:
value- the double to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readDouble()
-
writeFields
Writes the fields of the object currently being written to the target stream. The field values are buffered in the currently activePutFieldobject, which can be accessed by callingputFields().- 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:
putFields()
-
writeFloat
Writes a float (32 bit) to the target stream.- Specified by:
writeFloatin interfaceDataOutput- Parameters:
value- the float to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readFloat()
-
writeInt
Writes an integer (32 bit) to the target stream.- Specified by:
writeIntin interfaceDataOutput- Parameters:
value- the integer to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readInt()
-
writeLong
Writes a long (64 bit) to the target stream.- Specified by:
writeLongin interfaceDataOutput- Parameters:
value- the long to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readLong()
-
writeClassDescriptor
Writes a class descriptor to the target stream.- Parameters:
classDesc- the class descriptor to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
writeObject
Writes an object to the target stream.- Specified by:
writeObjectin interfaceObjectOutput- Parameters:
object- the object to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
ObjectInputStream.readObject()
-
writeObjectOverride
Method to be overridden by subclasses to writeobjectto the target stream.- Parameters:
object- the object to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
writeShort
Writes a short (16 bit) to the target stream.- Specified by:
writeShortin interfaceDataOutput- Parameters:
value- the short to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readShort(),DataInput.readUnsignedShort()
-
writeStreamHeader
Writes theObjectOutputStreamheader to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.
-
writeUTF
Writes a string encoded withmodified UTF-8to the target stream.- Specified by:
writeUTFin interfaceDataOutput- Parameters:
value- the string to write to the target stream.- Throws:
IOException- if an error occurs while writing to the target stream.- See Also:
DataInput.readUTF()
-