GeezerVison is showing docs.oracle.com — ObjectOutputStream (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.io
Class ObjectOutputStream
-
-
Nested Class Summary
Nested Classes
| Modifier and Type |
Class |
Description |
static class |
ObjectOutputStream.PutField |
Provide programmatic access to the persistent fields to be written
to ObjectOutput.
|
-
Field Summary
-
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 |
protected |
ObjectOutputStream() |
Provide a way for subclasses that are completely reimplementing
ObjectOutputStream to not have to allocate private data just used by
this implementation of ObjectOutputStream.
|
|
ObjectOutputStream(OutputStream out) |
Creates an ObjectOutputStream that writes to the specified OutputStream.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
protected void |
annotateClass(Class<?> cl) |
Subclasses may implement this method to allow class data to be stored in
the stream.
|
protected void |
annotateProxyClass(Class<?> cl) |
Subclasses may implement this method to store custom data in the stream
along with descriptors for dynamic proxy classes.
|
void |
close() |
Closes the stream.
|
void |
defaultWriteObject() |
Write the non-static and non-transient fields of the current class to
this stream.
|
protected void |
drain() |
Drain any buffered data in ObjectOutputStream.
|
protected boolean |
enableReplaceObject(boolean enable) |
Enable the stream to do replacement of objects in the stream.
|
void |
flush() |
Flushes the stream.
|
ObjectOutputStream.PutField |
putFields() |
Retrieve the object used to buffer persistent fields to be written to
the stream.
|
protected Object |
replaceObject(Object obj) |
This method will allow trusted subclasses of ObjectOutputStream to
substitute one object for another during serialization.
|
void |
reset() |
Reset will disregard the state of any objects already written to the
stream.
|
void |
useProtocolVersion(int version) |
Specify stream protocol version to use when writing the stream.
|
void |
write(byte[] buf) |
Writes an array of bytes.
|
void |
write(byte[] buf,
int off,
int len) |
Writes a sub array of bytes.
|
void |
write(int val) |
Writes a byte.
|
void |
writeBoolean(boolean val) |
Writes a boolean.
|
void |
writeByte(int val) |
Writes an 8 bit byte.
|
void |
writeBytes(String str) |
Writes a String as a sequence of bytes.
|
void |
writeChar(int val) |
Writes a 16 bit char.
|
void |
writeChars(String str) |
Writes a String as a sequence of chars.
|
protected void |
writeClassDescriptor(ObjectStreamClass desc) |
Write the specified class descriptor to the ObjectOutputStream.
|
void |
writeDouble(double val) |
Writes a 64 bit double.
|
void |
writeFields() |
Write the buffered fields to the stream.
|
void |
writeFloat(float val) |
Writes a 32 bit float.
|
void |
writeInt(int val) |
Writes a 32 bit int.
|
void |
writeLong(long val) |
Writes a 64 bit long.
|
void |
writeObject(Object obj) |
Write the specified object to the ObjectOutputStream.
|
protected void |
writeObjectOverride(Object obj) |
Method used by subclasses to override the default writeObject method.
|
void |
writeShort(int val) |
Writes a 16 bit short.
|
protected void |
writeStreamHeader() |
The writeStreamHeader method is provided so subclasses can append or
prepend their own header to the stream.
|
void |
writeUnshared(Object obj) |
Writes an "unshared" object to the ObjectOutputStream.
|
void |
writeUTF(String str) |
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
writeObjectOverride
protected void writeObjectOverride(Object obj)
throws IOException
Method used by subclasses to override the default writeObject method.
This method is called by trusted subclasses of ObjectInputStream that
constructed ObjectInputStream using the protected no-arg constructor.
The subclass is expected to provide an override method with the modifier
"final".
- Parameters:
obj - object to be written to the underlying stream
- Throws:
IOException - if there are I/O errors while writing to the
underlying stream
- Since:
- 1.2
- See Also:
ObjectOutputStream(),
writeObject(Object)
-
defaultWriteObject
public void defaultWriteObject()
throws IOException
Write the non-static and non-transient fields of the current class to
this stream. This may only be called from the writeObject method of the
class being serialized. It will throw the NotActiveException if it is
called otherwise.
- Throws:
IOException - if I/O errors occur while writing to the underlying
OutputStream
-
putFields
public ObjectOutputStream.PutField putFields()
throws IOException
Retrieve the object used to buffer persistent fields to be written to
the stream. The fields will be written to the stream when writeFields
method is called.
- Returns:
- an instance of the class Putfield that holds the serializable
fields
- Throws:
IOException - if I/O errors occur
- Since:
- 1.2
-
reset
public void reset()
throws IOException
Reset will disregard the state of any objects already written to the
stream. The state is reset to be the same as a new ObjectOutputStream.
The current point in the stream is marked as reset so the corresponding
ObjectInputStream will be reset at the same point. Objects previously
written to the stream will not be referred to as already being in the
stream. They will be written to the stream again.
- Throws:
IOException - if reset() is invoked while serializing an object.
-
annotateClass
protected void annotateClass(Class<?> cl)
throws IOException
Subclasses may implement this method to allow class data to be stored in
the stream. By default this method does nothing. The corresponding
method in ObjectInputStream is resolveClass. This method is called
exactly once for each unique class in the stream. The class name and
signature will have already been written to the stream. This method may
make free use of the ObjectOutputStream to save any representation of
the class it deems suitable (for example, the bytes of the class file).
The resolveClass method in the corresponding subclass of
ObjectInputStream must read and use any data or objects written by
annotateClass.
- Parameters:
cl - the class to annotate custom data for
- Throws:
IOException - Any exception thrown by the underlying
OutputStream.
-
writeStreamHeader
protected void writeStreamHeader()
throws IOException
The writeStreamHeader method is provided so subclasses can append or
prepend their own header to the stream. It writes the magic number and
version to the stream.
- Throws:
IOException - if I/O errors occur while writing to the underlying
stream
-
write
public void write(byte[] buf,
int off,
int len)
throws IOException
Writes a sub array of bytes.
- Specified by:
write in interface DataOutput
- Specified by:
write in interface ObjectOutput
- Overrides:
write in class OutputStream
- Parameters:
buf - the data to be written
off - the start offset in the data
len - the number of bytes that are written
- Throws:
IOException - If an I/O error has occurred.
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|