objectoutputstream.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,258 行 · 第 1/3 页
JAVA
1,258 行
forgotten. A notification of this reset is also written to the
underlying stream.
@exception IOException Exception from underlying
<code>OutputStream</code> or reset called while serialization is
in progress.
*/
public void reset() throws IOException {
reset(false);
}
private void reset(boolean internal) throws IOException {
if (!internal) {
if (isSerializing)
throw new IOException("Reset called while serialization in progress");
realOutput.writeByte(TC_RESET);
}
clearHandles();
}
/**
Informs this <code>ObjectOutputStream</code> to write data
according to the specified protocol. There are currently two
different protocols, specified by <code>PROTOCOL_VERSION_1</code>
and <code>PROTOCOL_VERSION_2</code>. This implementation writes
data using <code>PROTOCOL_VERSION_1</code> by default, as is done
by the JDK 1.1.
A non-portable method, <code>setDefaultProtocolVersion (int
version)</code> is provided to change the default protocol
version.
For an explination of the differences beween the two protocols
see XXX: the Java ObjectSerialization Specification.
@exception IOException if <code>version</code> is not a valid
protocol
@see setDefaultProtocolVersion (int)
*/
public void useProtocolVersion(int version) throws IOException {
if (version != PROTOCOL_VERSION_1 && version != PROTOCOL_VERSION_2)
throw new IOException("Invalid protocol version requested.");
protocolVersion = version;
}
/**
<em>GNU $classpath specific</em>
Changes the default stream protocol used by all
<code>ObjectOutputStream</code>s. There are currently two
different protocols, specified by <code>PROTOCOL_VERSION_1</code>
and <code>PROTOCOL_VERSION_2</code>. The default default is
<code>PROTOCOL_VERSION_1</code>.
@exception IOException if <code>version</code> is not a valid
protocol
@see useProtocolVersion (int)
*/
public static void setDefaultProtocolVersion(int version)
throws IOException {
if (version != PROTOCOL_VERSION_1 && version != PROTOCOL_VERSION_2)
throw new IOException("Invalid protocol version requested.");
defaultProtocolVersion = version;
}
/**
An empty hook that allows subclasses to write extra information
about classes to the stream. This method is called the first
time each class is seen, and after all of the standard
information about the class has been written.
@exception IOException Exception from underlying
<code>OutputStream</code>.
@see java.io.ObjectInputStream#resolveClass (java.io.ObjectStreamClass)
*/
protected void annotateClass(Class cl) throws IOException {
}
protected void annotateProxyClass(Class cl) throws IOException {
}
/**
Allows subclasses to replace objects that are written to the
stream with other objects to be written in their place. This
method is called the first time each object is encountered
(modulo reseting of the stream).
This method must be enabled before it will be called in the
serialization process.
@exception IOException Exception from underlying
<code>OutputStream</code>.
@see enableReplaceObject (boolean)
*/
protected Object replaceObject(Object obj) throws IOException {
return obj;
}
/**
If <code>enable</code> is <code>true</code> and this object is
trusted, then <code>replaceObject (Object)</code> will be called
in subsequent calls to <code>writeObject (Object)</code>.
Otherwise, <code>replaceObject (Object)</code> will not be called.
@exception SecurityException This class is not trusted.
*/
protected boolean enableReplaceObject(boolean enable)
throws SecurityException {
if (enable) {
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(
new SerializablePermission("enableSubstitution"));
}
boolean old_val = replacementEnabled;
replacementEnabled = enable;
return old_val;
}
/**
Writes stream magic and stream version information to the
underlying stream.
@exception IOException Exception from underlying
<code>OutputStream</code>.
*/
protected void writeStreamHeader() throws IOException {
realOutput.writeShort(STREAM_MAGIC);
realOutput.writeShort(STREAM_VERSION);
}
/**
Protected constructor that allows subclasses to override
serialization. This constructor should be called by subclasses
that wish to override <code>writeObject (Object)</code>. This
method does a security check <i>NOTE: currently not
implemented</i>, then sets a flag that informs
<code>writeObject (Object)</code> to call the subclasses
<code>writeObjectOverride (Object)</code> method.
@see writeObjectOverride (Object)
*/
protected ObjectOutputStream() throws IOException, SecurityException {
SecurityManager sec_man = System.getSecurityManager();
if (sec_man != null)
sec_man.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
useSubclassMethod = true;
}
/**
This method allows subclasses to override the default
serialization mechanism provided by
<code>ObjectOutputStream</code>. To make this method be used for
writing objects, subclasses must invoke the 0-argument
constructor on this class from there constructor.
@see ObjectOutputStream ()
@exception NotActiveException Subclass has arranged for this
method to be called, but did not implement this method.
*/
protected void writeObjectOverride(Object obj)
throws NotActiveException, IOException {
throw new NotActiveException("Subclass of ObjectOutputStream must implement writeObjectOverride");
}
/**
@see java.io.DataOutputStream#write (int)
*/
public void write(int data) throws IOException {
if (writeDataAsBlocks) {
if (blockDataCount == BUFFER_SIZE)
drain();
blockData[blockDataCount++] = (byte) data;
} else
realOutput.write(data);
}
/**
@see java.io.DataOutputStream#write (byte[])
*/
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
/**
@see java.io.DataOutputStream#write (byte[],int,int)
*/
public void write(byte[] b, int off, int len) throws IOException {
if (writeDataAsBlocks) {
if (len < 0)
throw new IndexOutOfBoundsException();
if (blockDataCount + len < BUFFER_SIZE) {
System.arraycopy(b, off, blockData, blockDataCount, len);
blockDataCount += len;
} else {
drain();
writeBlockDataHeader(len);
realOutput.write(b, off, len);
}
} else
realOutput.write(b, off, len);
}
/**
@see java.io.DataOutputStream#flush ()
*/
public void flush() throws IOException {
drain();
realOutput.flush();
}
/**
Causes the block-data buffer to be written to the underlying
stream, but does not flush underlying stream.
@exception IOException Exception from underlying
<code>OutputStream</code>.
*/
protected void drain() throws IOException {
if (blockDataCount == 0)
return;
if (writeDataAsBlocks) // ??
writeBlockDataHeader(blockDataCount);
realOutput.write(blockData, 0, blockDataCount);
blockDataCount = 0;
}
/**
@see java.io.DataOutputStream#close ()
*/
public void close() throws IOException {
flush();
realOutput.close();
}
/**
@see java.io.DataOutputStream#writeBoolean (boolean)
*/
public void writeBoolean(boolean data) throws IOException {
blockDataOutput.writeBoolean(data);
}
/**
@see java.io.DataOutputStream#writeByte (int)
*/
public void writeByte(int data) throws IOException {
blockDataOutput.writeByte(data);
}
/**
@see java.io.DataOutputStream#writeShort (int)
*/
public void writeShort(int data) throws IOException {
blockDataOutput.writeShort(data);
}
/**
@see java.io.DataOutputStream#writeChar (int)
*/
public void writeChar(int data) throws IOException {
blockDataOutput.writeChar(data);
}
/**
@see java.io.DataOutputStream#writeInt (int)
*/
public void writeInt(int data) throws IOException {
blockDataOutput.writeInt(data);
}
/**
@see java.io.DataOutputStream#writeLong (long)
*/
public void writeLong(long data) throws IOException {
blockDataOutput.writeLong(data);
}
/**
@see java.io.DataOutputStream#writeFloat (float)
*/
public void writeFloat(float data) throws IOException {
blockDataOutput.writeFloat(data);
}
/**
@see java.io.DataOutputStream#writeDouble (double)
*/
public void writeDouble(double data) throws IOException {
blockDataOutput.writeDouble(data);
}
/**
@see java.io.DataOutputStream#writeBytes (java.lang.String)
*/
public void writeBytes(String data) throws IOException {
blockDataOutput.writeBytes(data);
}
/**
@see java.io.DataOutputStream#writeChars (java.lang.String)
*/
public void writeChars(String data) throws IOException {
dataOutput.writeChars(data);
}
/**
@see java.io.DataOutputStream#writeUTF (java.lang.String)
*/
public void writeUTF(String data) throws IOException {
dataOutput.writeUTF(data);
}
/**
This class allows a class to specify exactly which fields should
be written, and what values should be written for these fields.
XXX: finish up comments
*/
public static abstract class PutField {
public abstract void put(String name, boolean value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, byte value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, char value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, double value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, float value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, int value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, long value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, short value)
throws IOException, IllegalArgumentException;
public abstract void put(String name, Object value)
throws IOException, IllegalArgumentException;
public abstract void write(ObjectOutput out) throws IOException;
}
public PutField putFields() throws IOException {
markFieldsWritten();
currentPutField = new PutField() {
private byte[] prim_field_data =
new byte[currentObjectStreamClass.primFieldSize];
private Object[] objs =
new Object[currentObjectStreamClass.objectFieldCount];
public void put(String name, boolean value)
throws IOException, IllegalArgumentException {
ObjectStreamField field =
currentObjectStreamClass.getField(name);
checkType(field, 'Z');
prim_field_data[field.getOffset()] = (byte) (value ? 1 : 0);
}
public void put(String name, byte value)
throws IOException, IllegalArgumentException {
ObjectStreamField field =
currentObjectStreamClass.getField(name);
checkType(field, 'B');
prim_field_data[field.getOffset()] = value;
}
public void put(String name, char value)
throws IOException, IllegalArgumentException {
ObjectStreamField field =
currentObjectStreamClass.getField(name);
checkType(field, 'C');
int off = field.getOffset();
prim_field_data[off++] = (byte) (value >>> 8);
prim_field_data[off] = (byte) value;
}
public void put(String name, double value)
throws IOException, IllegalArgumentException {
ObjectStreamField field =
currentObjectStreamClass.getField(name);
checkType(field, 'D');
int off = field.getOffset();
long l_value = Double.doubleToLongBits(value);
prim_field_data[off++] = (byte) (l_value >>> 52);
prim_field_data[off++] = (byte) (l_value >>> 48);
prim_field_data[off++] = (byte) (l_value >>> 40);
prim_field_data[off++] = (byte) (l_value >>> 32);
prim_field_data[off++] = (byte) (l_value >>> 24);
prim_field_data[off++] = (byte) (l_value >>> 16);
prim_field_data[off++] = (byte) (l_value >>> 8);
prim_field_data[off] = (byte) l_value;
}
public void put(String name, float value)
throws IOException, IllegalArgumentException {
ObjectStreamField field =
currentObjectStreamClass.getField(name);
checkType(field, 'F');
int off = field.getOffset();
int i_value = Float.floatToIntBits(value);
prim_field_data[off++] = (byte) (i_value >>> 24);
prim_field_data[off++] = (byte) (i_value >>> 16);
prim_field_data[off++] = (byte) (i_value >>> 8);
prim_field_data[off] = (byte) i_value;
}
public void put(String name, int value)
throws IOException, IllegalArgumentException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?