📄 objectoutputstream.java
字号:
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_2</code> by default, as is done * by the JDK 1.2. * * A non-portable method, <code>setDefaultProtocolVersion (int * version)</code> is provided to change the default protocol * version. * * For an explanation of the differences between 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 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 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 DataOutputStream#write(byte[]) */ public void write(byte[] b) throws IOException { write(b, 0, b.length); } /** * @see 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 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 abstract static class PutField { public abstract void put (String name, boolean value); public abstract void put (String name, byte value); public abstract void put (String name, char value); public abstract void put (String name, double value); public abstract void put (String name, float value); public abstract void put (String name, int value); public abstract void put (String name, long value); public abstract void put (String name, short value); public abstract void put (String name, Object value); /** * @deprecated */ public abstract void write (ObjectOutput out) throws IOException; } public PutField putFields() throws IOException { if (currentPutField != null) return currentPutField; currentPutField = new PutField() { private byte[] prim_field_data = new byte[currentObjectStreamClass.primFieldSize]; private Object[] objs = new Object[currentObjectStreamClass.objectFieldCount]; private ObjectStreamField getField (String name) { ObjectStreamField field = currentObjectStreamClass.getField(name); if (field == null) throw new IllegalArgumentException("no such serializable field " + name); return field; } public void put(String name, boolean value) { ObjectStreamField field = getField(name); checkType(field, 'Z'); prim_field_data[field.getOffset ()] = (byte)(value ? 1 : 0); } public void put(String name, byte value) { ObjectStreamField field = getField(name); checkType(field, 'B'); prim_field_data[field.getOffset()] = value; } public void put(String name, char value) { ObjectStreamField field = 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) { ObjectStreamField field = 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) { ObjectStreamField field = 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) { ObjectStreamField field = getField(name); checkType(field, 'I'); int off = field.getOffset(); prim_field_data[off++] = (byte)(value >>> 24); prim_field_data[off++] = (byte)(value >>> 16); prim_field_data[off++] = (byte)(value >>> 8); prim_field_data[off] = (byte)value; } public void put(String name, long value) { ObjectStreamField field = getField(name); checkType(field, 'J'); int off = field.getOffset(); prim_field_data[off++] = (byte)(value >>> 52); prim_field_data[off++] = (byte)(value >>> 48); prim_field_data[off++] = (byte)(value >>> 40); prim_field_data[off++] = (byte)(value >>> 32); prim_field_data[off++] = (byte)(value >>> 24); prim_field_data[off++] = (byte)(value >>> 16); prim_field_data[off++] = (byte)(value >>> 8); prim_field_data[off] = (byte)value; } public void put(String name, short value) { ObjectStreamField field = getField(name); checkType(field, 'S'); int off = field.getOffset(); prim_field_data[off++] = (byte)(value >>> 8); prim_field_data[off] = (byte)value; } public void put(String name, Object value) { ObjectStreamField field = getField(name); if (value != null && ! field.getType().isAssignableFrom(value.getClass ())) throw new IllegalArgumentException("Class " + value.getClass() + " cannot be cast to " + field.getType()); objs[field.getOffset()] = value; } public void write(ObjectOutput out) throws IOException { // Apparently Block data is not used with PutField as per // empirical evidence against JDK 1.2. Also see Mauve test // java.io.ObjectInputOutput.Test.GetPutField. boolean oldmode = setBlockDataMode(false); out.write(prim_field_data); for (int i = 0; i < objs.length; ++ i) out.writeObject(objs[i]); setBlockDataMode(oldmode); } private void checkType(ObjectStreamField field, char type)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -