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.
| Method from java.io.ObjectOutputStream Detail: |
protected void annotateClass(Class<?> aClass) throws IOException {
// By default no extra info is saved. Subclasses can override
}
Writes optional information for class {@code 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. |
protected void annotateProxyClass(Class<?> aClass) throws IOException {
// By default no extra info is saved. Subclasses can override
}
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. |
public void close() throws IOException {
// First flush what is needed (primitive data, etc)
flush();
output.close();
}
Closes this stream. Any buffered data is flushed. This implementation
closes the target stream. |
public void defaultWriteObject() throws IOException {
// We can't be called from just anywhere. There are rules.
if (currentObject == null) {
throw new NotActiveException();
}
writeFieldValues(currentObject, currentClass);
}
Default method to write objects to this stream. Serializable fields
defined in the object's class and superclasses are written to the output
stream. |
protected void drain() throws IOException {
if (primitiveTypes == null || primitiveTypesBuffer == null) {
return;
}
// If we got here we have a Stream previously created
int offset = 0;
byte[] written = primitiveTypesBuffer.toByteArray();
// Normalize the primitive data
while (offset < written.length) {
int toWrite = written.length - offset > 1024 ? 1024
: written.length - offset;
if (toWrite < 256) {
output.writeByte(TC_BLOCKDATA);
output.writeByte((byte) toWrite);
} else {
output.writeByte(TC_BLOCKDATALONG);
output.writeInt(toWrite);
}
// write primitive types we had and the marker of end-of-buffer
output.write(written, offset, toWrite);
offset += toWrite;
}
// and now we're clean to a state where we can write an object
primitiveTypes = null;
primitiveTypesBuffer = null;
}
Writes buffered data to the target stream. This is similar to {@code
flush} but the flush is not propagated to the target stream. |
protected boolean enableReplaceObject(boolean enable) throws SecurityException {
if (enable) {
// The Stream has to be trusted for this feature to be enabled.
// trusted means the stream's classloader has to be null
SecurityManager currentManager = System.getSecurityManager();
if (currentManager != null) {
currentManager.checkPermission(SUBSTITUTION_PERMISSION);
}
}
boolean originalValue = enableReplace;
enableReplace = enable;
return originalValue;
}
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. |
public void flush() throws IOException {
drain();
output.flush();
}
Writes buffered data to the target stream and calls the {@code flush}
method of the target stream. |
public PutField putFields() throws IOException {
// We can't be called from just anywhere. There are rules.
if (currentObject == null) {
throw new NotActiveException();
}
if (currentPutField == null) {
computePutField();
}
return currentPutField;
}
Gets this stream's {@code 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. |
protected Object replaceObject(Object object) throws IOException {
// By default no object replacement. Subclasses can override
return object;
}
Allows trusted subclasses to substitute the specified original {@code
object} with a new object. Object substitution has to be activated first
with calling {@code enableReplaceObject(true)}. This implementation just
returns {@code object}. |
public void reset() throws IOException {
// First we flush what we have
drain();
/*
* And dump a reset marker, so that the ObjectInputStream can reset
* itself at the same point
*/
output.writeByte(TC_RESET);
// Now we reset ourselves
resetState();
}
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. |
public void useProtocolVersion(int version) throws IOException {
if (!objectsWritten.isEmpty()) {
// luni.C8=Cannot set protocol version when stream in use
throw new IllegalStateException(Messages.getString("luni.C8")); //$NON-NLS-1$
}
if (version != ObjectStreamConstants.PROTOCOL_VERSION_1
&& version != ObjectStreamConstants.PROTOCOL_VERSION_2) {
// luni.9C=Unknown protocol\: {0}
throw new IllegalArgumentException(Messages.getString("luni.9C", version)); //$NON-NLS-1$
}
protocolVersion = version;
}
Sets the specified protocol version to be used by this stream. |
public void write(byte[] buffer) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.write(buffer);
}
Writes the entire contents of the byte array {@code buffer} to the output
stream. Blocks until all bytes are written. |
public void write(int value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.write(value);
}
Writes a single byte to the target stream. Only the least significant
byte of the integer {@code value} is written to the stream. Blocks until
the byte is actually written. |
public void write(byte[] buffer,
int offset,
int length) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.write(buffer, offset, length);
}
Writes {@code count} bytes from the byte array {@code buffer} starting at
offset {@code index} to the target stream. Blocks until all bytes are
written. |
public void writeBoolean(boolean value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeBoolean(value);
}
Writes a boolean to the target stream. |
public void writeByte(int value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeByte(value);
}
Writes a byte (8 bit) to the target stream. |
public void writeBytes(String value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeBytes(value);
}
Writes the string {@code value} as a sequence of bytes to the target
stream. Only the least significant byte of each character in the string
is written. |
public void writeChar(int value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeChar(value);
}
Writes a character (16 bit) to the target stream. |
public void writeChars(String value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeChars(value);
}
Writes the string {@code value} as a sequence of characters to the target
stream. |
protected void writeClassDescriptor(ObjectStreamClass classDesc) throws IOException {
writeNewClassDesc(classDesc);
}
Writes a class descriptor to the target stream. |
public void writeDouble(double value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeDouble(value);
}
Writes a double (64 bit) to the target stream. |
public void writeFields() throws IOException {
// Has to have fields to write
if (currentPutField == null) {
throw new NotActiveException();
}
writeFieldValues(currentPutField);
}
Writes the fields of the object currently being written to the target
stream. The field values are buffered in the currently active {@code
PutField} object, which can be accessed by calling {@code putFields()}. |
public void writeFloat(float value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeFloat(value);
}
Writes a float (32 bit) to the target stream. |
public void writeInt(int value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeInt(value);
}
Writes an integer (32 bit) to the target stream. |
public void writeLong(long value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeLong(value);
}
Writes a long (64 bit) to the target stream. |
public final void writeObject(Object object) throws IOException {
writeObject(object, false);
}
Writes an object to the target stream. |
protected void writeObjectOverride(Object object) throws IOException {
if (!subclassOverridingImplementation) {
// Subclasses must override.
throw new IOException();
}
}
Method to be overridden by subclasses to write {@code object} to the
target stream. |
public void writeShort(int value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeShort(value);
}
Writes a short (16 bit) to the target stream. |
protected void writeStreamHeader() throws IOException {
output.writeShort(STREAM_MAGIC);
output.writeShort(STREAM_VERSION);
}
|
public void writeUTF(String value) throws IOException {
checkWritePrimitiveTypes();
primitiveTypes.writeUTF(value);
}
|
public void writeUnshared(Object object) throws IOException {
writeObject(object, true);
}
Writes an unshared object to the target stream. This method is identical
to {@code writeObject}, except that it always writes a new object to the
stream versus the use of back-referencing for identical objects by
{@code writeObject}. |