Adding ObjectInputStream and ObjectOutputStream (but without implementation). Adding PropertyChange related classes.
2 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.io.ObjectStreamClass.WeakClassKey;
29 import java.lang.ref.ReferenceQueue;
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.concurrent.ConcurrentMap;
37 import static java.io.ObjectStreamClass.processQueue;
38 import java.io.SerialCallbackContext;
41 * An ObjectOutputStream writes primitive data types and graphs of Java objects
42 * to an OutputStream. The objects can be read (reconstituted) using an
43 * ObjectInputStream. Persistent storage of objects can be accomplished by
44 * using a file for the stream. If the stream is a network socket stream, the
45 * objects can be reconstituted on another host or in another process.
47 * <p>Only objects that support the java.io.Serializable interface can be
48 * written to streams. The class of each serializable object is encoded
49 * including the class name and signature of the class, the values of the
50 * object's fields and arrays, and the closure of any other objects referenced
51 * from the initial objects.
53 * <p>The method writeObject is used to write an object to the stream. Any
54 * object, including Strings and arrays, is written with writeObject. Multiple
55 * objects or primitives can be written to the stream. The objects must be
56 * read back from the corresponding ObjectInputstream with the same types and
57 * in the same order as they were written.
59 * <p>Primitive data types can also be written to the stream using the
60 * appropriate methods from DataOutput. Strings can also be written using the
63 * <p>The default serialization mechanism for an object writes the class of the
64 * object, the class signature, and the values of all non-transient and
65 * non-static fields. References to other objects (except in transient or
66 * static fields) cause those objects to be written also. Multiple references
67 * to a single object are encoded using a reference sharing mechanism so that
68 * graphs of objects can be restored to the same shape as when the original was
71 * <p>For example to write an object that can be read by the example in
75 * FileOutputStream fos = new FileOutputStream("t.tmp");
76 * ObjectOutputStream oos = new ObjectOutputStream(fos);
78 * oos.writeInt(12345);
79 * oos.writeObject("Today");
80 * oos.writeObject(new Date());
85 * <p>Classes that require special handling during the serialization and
86 * deserialization process must implement special methods with these exact
90 * private void readObject(java.io.ObjectInputStream stream)
91 * throws IOException, ClassNotFoundException;
92 * private void writeObject(java.io.ObjectOutputStream stream)
94 * private void readObjectNoData()
95 * throws ObjectStreamException;
98 * <p>The writeObject method is responsible for writing the state of the object
99 * for its particular class so that the corresponding readObject method can
100 * restore it. The method does not need to concern itself with the state
101 * belonging to the object's superclasses or subclasses. State is saved by
102 * writing the individual fields to the ObjectOutputStream using the
103 * writeObject method or by using the methods for primitive data types
104 * supported by DataOutput.
106 * <p>Serialization does not write out the fields of any object that does not
107 * implement the java.io.Serializable interface. Subclasses of Objects that
108 * are not serializable can be serializable. In this case the non-serializable
109 * class must have a no-arg constructor to allow its fields to be initialized.
110 * In this case it is the responsibility of the subclass to save and restore
111 * the state of the non-serializable class. It is frequently the case that the
112 * fields of that class are accessible (public, package, or protected) or that
113 * there are get and set methods that can be used to restore the state.
115 * <p>Serialization of an object can be prevented by implementing writeObject
116 * and readObject methods that throw the NotSerializableException. The
117 * exception will be caught by the ObjectOutputStream and abort the
118 * serialization process.
120 * <p>Implementing the Externalizable interface allows the object to assume
121 * complete control over the contents and format of the object's serialized
122 * form. The methods of the Externalizable interface, writeExternal and
123 * readExternal, are called to save and restore the objects state. When
124 * implemented by a class they can write and read their own state using all of
125 * the methods of ObjectOutput and ObjectInput. It is the responsibility of
126 * the objects to handle any versioning that occurs.
128 * <p>Enum constants are serialized differently than ordinary serializable or
129 * externalizable objects. The serialized form of an enum constant consists
130 * solely of its name; field values of the constant are not transmitted. To
131 * serialize an enum constant, ObjectOutputStream writes the string returned by
132 * the constant's name method. Like other serializable or externalizable
133 * objects, enum constants can function as the targets of back references
134 * appearing subsequently in the serialization stream. The process by which
135 * enum constants are serialized cannot be customized; any class-specific
136 * writeObject and writeReplace methods defined by enum types are ignored
137 * during serialization. Similarly, any serialPersistentFields or
138 * serialVersionUID field declarations are also ignored--all enum types have a
139 * fixed serialVersionUID of 0L.
141 * <p>Primitive data, excluding serializable fields and externalizable data, is
142 * written to the ObjectOutputStream in block-data records. A block data record
143 * is composed of a header and data. The block data header consists of a marker
144 * and the number of bytes to follow the header. Consecutive primitive data
145 * writes are merged into one block-data record. The blocking factor used for
146 * a block-data record will be 1024 bytes. Each block-data record will be
147 * filled up to 1024 bytes, or be written whenever there is a termination of
148 * block-data mode. Calls to the ObjectOutputStream methods writeObject,
149 * defaultWriteObject and writeFields initially terminate any existing
152 * @author Mike Warres
153 * @author Roger Riggs
154 * @see java.io.DataOutput
155 * @see java.io.ObjectInputStream
156 * @see java.io.Serializable
157 * @see java.io.Externalizable
158 * @see <a href="../../../platform/serialization/spec/output.html">Object Serialization Specification, Section 2, Object Output Classes</a>
161 public class ObjectOutputStream
162 extends OutputStream implements ObjectOutput, ObjectStreamConstants
165 private static class Caches {
166 /** cache of subclass security audit results */
167 static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
168 new ConcurrentHashMap<>();
170 /** queue for WeakReferences to audited subclasses */
171 static final ReferenceQueue<Class<?>> subclassAuditsQueue =
172 new ReferenceQueue<>();
175 /** filter stream for handling block data conversion */
176 private final BlockDataOutputStream bout;
177 /** obj -> wire handle map */
178 private final HandleTable handles;
179 /** obj -> replacement obj map */
180 private final ReplaceTable subs;
181 /** stream protocol version */
182 private int protocol = PROTOCOL_VERSION_2;
183 /** recursion depth */
186 /** buffer for writing primitive field values */
187 private byte[] primVals;
189 /** if true, invoke writeObjectOverride() instead of writeObject() */
190 private final boolean enableOverride;
191 /** if true, invoke replaceObject() */
192 private boolean enableReplace;
194 // values below valid only during upcalls to writeObject()/writeExternal()
196 * Context during upcalls to class-defined writeObject methods; holds
197 * object currently being serialized and descriptor for current class.
198 * Null when not during writeObject upcall.
200 private SerialCallbackContext curContext;
201 /** current PutField object */
202 private PutFieldImpl curPut;
204 /** custom storage for debug trace info */
205 private final DebugTraceInfoStack debugInfoStack;
208 * value of "sun.io.serialization.extendedDebugInfo" property,
209 * as true or false for extended information about exception's place
211 private static final boolean extendedDebugInfo =
212 java.security.AccessController.doPrivileged(
213 new sun.security.action.GetBooleanAction(
214 "sun.io.serialization.extendedDebugInfo")).booleanValue();
217 * Creates an ObjectOutputStream that writes to the specified OutputStream.
218 * This constructor writes the serialization stream header to the
219 * underlying stream; callers may wish to flush the stream immediately to
220 * ensure that constructors for receiving ObjectInputStreams will not block
221 * when reading the header.
223 * <p>If a security manager is installed, this constructor will check for
224 * the "enableSubclassImplementation" SerializablePermission when invoked
225 * directly or indirectly by the constructor of a subclass which overrides
226 * the ObjectOutputStream.putFields or ObjectOutputStream.writeUnshared
229 * @param out output stream to write to
230 * @throws IOException if an I/O error occurs while writing stream header
231 * @throws SecurityException if untrusted subclass illegally overrides
232 * security-sensitive methods
233 * @throws NullPointerException if <code>out</code> is <code>null</code>
235 * @see ObjectOutputStream#ObjectOutputStream()
236 * @see ObjectOutputStream#putFields()
237 * @see ObjectInputStream#ObjectInputStream(InputStream)
239 public ObjectOutputStream(OutputStream out) throws IOException {
241 bout = new BlockDataOutputStream(out);
242 handles = new HandleTable(10, (float) 3.00);
243 subs = new ReplaceTable(10, (float) 3.00);
244 enableOverride = false;
246 bout.setBlockDataMode(true);
247 if (extendedDebugInfo) {
248 debugInfoStack = new DebugTraceInfoStack();
250 debugInfoStack = null;
255 * Provide a way for subclasses that are completely reimplementing
256 * ObjectOutputStream to not have to allocate private data just used by
257 * this implementation of ObjectOutputStream.
259 * <p>If there is a security manager installed, this method first calls the
260 * security manager's <code>checkPermission</code> method with a
261 * <code>SerializablePermission("enableSubclassImplementation")</code>
262 * permission to ensure it's ok to enable subclassing.
264 * @throws SecurityException if a security manager exists and its
265 * <code>checkPermission</code> method denies enabling
267 * @see SecurityManager#checkPermission
268 * @see java.io.SerializablePermission
270 protected ObjectOutputStream() throws IOException, SecurityException {
271 SecurityManager sm = System.getSecurityManager();
273 sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
278 enableOverride = true;
279 debugInfoStack = null;
283 * Specify stream protocol version to use when writing the stream.
285 * <p>This routine provides a hook to enable the current version of
286 * Serialization to write in a format that is backwards compatible to a
287 * previous version of the stream format.
289 * <p>Every effort will be made to avoid introducing additional
290 * backwards incompatibilities; however, sometimes there is no
293 * @param version use ProtocolVersion from java.io.ObjectStreamConstants.
294 * @throws IllegalStateException if called after any objects
295 * have been serialized.
296 * @throws IllegalArgumentException if invalid version is passed in.
297 * @throws IOException if I/O errors occur
298 * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_1
299 * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_2
302 public void useProtocolVersion(int version) throws IOException {
303 if (handles.size() != 0) {
304 // REMIND: implement better check for pristine stream?
305 throw new IllegalStateException("stream non-empty");
308 case PROTOCOL_VERSION_1:
309 case PROTOCOL_VERSION_2:
314 throw new IllegalArgumentException(
315 "unknown version: " + version);
320 * Write the specified object to the ObjectOutputStream. The class of the
321 * object, the signature of the class, and the values of the non-transient
322 * and non-static fields of the class and all of its supertypes are
323 * written. Default serialization for a class can be overridden using the
324 * writeObject and the readObject methods. Objects referenced by this
325 * object are written transitively so that a complete equivalent graph of
326 * objects can be reconstructed by an ObjectInputStream.
328 * <p>Exceptions are thrown for problems with the OutputStream and for
329 * classes that should not be serialized. All exceptions are fatal to the
330 * OutputStream, which is left in an indeterminate state, and it is up to
331 * the caller to ignore or recover the stream state.
333 * @throws InvalidClassException Something is wrong with a class used by
335 * @throws NotSerializableException Some object to be serialized does not
336 * implement the java.io.Serializable interface.
337 * @throws IOException Any exception thrown by the underlying
340 public final void writeObject(Object obj) throws IOException {
341 if (enableOverride) {
342 writeObjectOverride(obj);
346 writeObject0(obj, false);
347 } catch (IOException ex) {
349 writeFatalException(ex);
356 * Method used by subclasses to override the default writeObject method.
357 * This method is called by trusted subclasses of ObjectInputStream that
358 * constructed ObjectInputStream using the protected no-arg constructor.
359 * The subclass is expected to provide an override method with the modifier
362 * @param obj object to be written to the underlying stream
363 * @throws IOException if there are I/O errors while writing to the
365 * @see #ObjectOutputStream()
366 * @see #writeObject(Object)
369 protected void writeObjectOverride(Object obj) throws IOException {
373 * Writes an "unshared" object to the ObjectOutputStream. This method is
374 * identical to writeObject, except that it always writes the given object
375 * as a new, unique object in the stream (as opposed to a back-reference
376 * pointing to a previously serialized instance). Specifically:
378 * <li>An object written via writeUnshared is always serialized in the
379 * same manner as a newly appearing object (an object that has not
380 * been written to the stream yet), regardless of whether or not the
381 * object has been written previously.
383 * <li>If writeObject is used to write an object that has been previously
384 * written with writeUnshared, the previous writeUnshared operation
385 * is treated as if it were a write of a separate object. In other
386 * words, ObjectOutputStream will never generate back-references to
387 * object data written by calls to writeUnshared.
389 * While writing an object via writeUnshared does not in itself guarantee a
390 * unique reference to the object when it is deserialized, it allows a
391 * single object to be defined multiple times in a stream, so that multiple
392 * calls to readUnshared by the receiver will not conflict. Note that the
393 * rules described above only apply to the base-level object written with
394 * writeUnshared, and not to any transitively referenced sub-objects in the
395 * object graph to be serialized.
397 * <p>ObjectOutputStream subclasses which override this method can only be
398 * constructed in security contexts possessing the
399 * "enableSubclassImplementation" SerializablePermission; any attempt to
400 * instantiate such a subclass without this permission will cause a
401 * SecurityException to be thrown.
403 * @param obj object to write to stream
404 * @throws NotSerializableException if an object in the graph to be
405 * serialized does not implement the Serializable interface
406 * @throws InvalidClassException if a problem exists with the class of an
407 * object to be serialized
408 * @throws IOException if an I/O error occurs during serialization
411 public void writeUnshared(Object obj) throws IOException {
413 writeObject0(obj, true);
414 } catch (IOException ex) {
416 writeFatalException(ex);
423 * Write the non-static and non-transient fields of the current class to
424 * this stream. This may only be called from the writeObject method of the
425 * class being serialized. It will throw the NotActiveException if it is
428 * @throws IOException if I/O errors occur while writing to the underlying
429 * <code>OutputStream</code>
431 public void defaultWriteObject() throws IOException {
432 if ( curContext == null ) {
433 throw new NotActiveException("not in call to writeObject");
435 Object curObj = curContext.getObj();
436 ObjectStreamClass curDesc = curContext.getDesc();
437 bout.setBlockDataMode(false);
438 defaultWriteFields(curObj, curDesc);
439 bout.setBlockDataMode(true);
443 * Retrieve the object used to buffer persistent fields to be written to
444 * the stream. The fields will be written to the stream when writeFields
447 * @return an instance of the class Putfield that holds the serializable
449 * @throws IOException if I/O errors occur
452 public ObjectOutputStream.PutField putFields() throws IOException {
453 if (curPut == null) {
454 if (curContext == null) {
455 throw new NotActiveException("not in call to writeObject");
457 Object curObj = curContext.getObj();
458 ObjectStreamClass curDesc = curContext.getDesc();
459 curPut = new PutFieldImpl(curDesc);
465 * Write the buffered fields to the stream.
467 * @throws IOException if I/O errors occur while writing to the underlying
469 * @throws NotActiveException Called when a classes writeObject method was
470 * not called to write the state of the object.
473 public void writeFields() throws IOException {
474 if (curPut == null) {
475 throw new NotActiveException("no current PutField object");
477 bout.setBlockDataMode(false);
478 curPut.writeFields();
479 bout.setBlockDataMode(true);
483 * Reset will disregard the state of any objects already written to the
484 * stream. The state is reset to be the same as a new ObjectOutputStream.
485 * The current point in the stream is marked as reset so the corresponding
486 * ObjectInputStream will be reset at the same point. Objects previously
487 * written to the stream will not be refered to as already being in the
488 * stream. They will be written to the stream again.
490 * @throws IOException if reset() is invoked while serializing an object.
492 public void reset() throws IOException {
494 throw new IOException("stream active");
496 bout.setBlockDataMode(false);
497 bout.writeByte(TC_RESET);
499 bout.setBlockDataMode(true);
503 * Subclasses may implement this method to allow class data to be stored in
504 * the stream. By default this method does nothing. The corresponding
505 * method in ObjectInputStream is resolveClass. This method is called
506 * exactly once for each unique class in the stream. The class name and
507 * signature will have already been written to the stream. This method may
508 * make free use of the ObjectOutputStream to save any representation of
509 * the class it deems suitable (for example, the bytes of the class file).
510 * The resolveClass method in the corresponding subclass of
511 * ObjectInputStream must read and use any data or objects written by
514 * @param cl the class to annotate custom data for
515 * @throws IOException Any exception thrown by the underlying
518 protected void annotateClass(Class<?> cl) throws IOException {
522 * Subclasses may implement this method to store custom data in the stream
523 * along with descriptors for dynamic proxy classes.
525 * <p>This method is called exactly once for each unique proxy class
526 * descriptor in the stream. The default implementation of this method in
527 * <code>ObjectOutputStream</code> does nothing.
529 * <p>The corresponding method in <code>ObjectInputStream</code> is
530 * <code>resolveProxyClass</code>. For a given subclass of
531 * <code>ObjectOutputStream</code> that overrides this method, the
532 * <code>resolveProxyClass</code> method in the corresponding subclass of
533 * <code>ObjectInputStream</code> must read any data or objects written by
534 * <code>annotateProxyClass</code>.
536 * @param cl the proxy class to annotate custom data for
537 * @throws IOException any exception thrown by the underlying
538 * <code>OutputStream</code>
539 * @see ObjectInputStream#resolveProxyClass(String[])
542 protected void annotateProxyClass(Class<?> cl) throws IOException {
546 * This method will allow trusted subclasses of ObjectOutputStream to
547 * substitute one object for another during serialization. Replacing
548 * objects is disabled until enableReplaceObject is called. The
549 * enableReplaceObject method checks that the stream requesting to do
550 * replacement can be trusted. The first occurrence of each object written
551 * into the serialization stream is passed to replaceObject. Subsequent
552 * references to the object are replaced by the object returned by the
553 * original call to replaceObject. To ensure that the private state of
554 * objects is not unintentionally exposed, only trusted streams may use
557 * <p>The ObjectOutputStream.writeObject method takes a parameter of type
558 * Object (as opposed to type Serializable) to allow for cases where
559 * non-serializable objects are replaced by serializable ones.
561 * <p>When a subclass is replacing objects it must insure that either a
562 * complementary substitution must be made during deserialization or that
563 * the substituted object is compatible with every field where the
564 * reference will be stored. Objects whose type is not a subclass of the
565 * type of the field or array element abort the serialization by raising an
566 * exception and the object is not be stored.
568 * <p>This method is called only once when each object is first
569 * encountered. All subsequent references to the object will be redirected
570 * to the new object. This method should return the object to be
571 * substituted or the original object.
573 * <p>Null can be returned as the object to be substituted, but may cause
574 * NullReferenceException in classes that contain references to the
575 * original object since they may be expecting an object instead of
578 * @param obj the object to be replaced
579 * @return the alternate object that replaced the specified one
580 * @throws IOException Any exception thrown by the underlying
583 protected Object replaceObject(Object obj) throws IOException {
588 * Enable the stream to do replacement of objects in the stream. When
589 * enabled, the replaceObject method is called for every object being
592 * <p>If <code>enable</code> is true, and there is a security manager
593 * installed, this method first calls the security manager's
594 * <code>checkPermission</code> method with a
595 * <code>SerializablePermission("enableSubstitution")</code> permission to
596 * ensure it's ok to enable the stream to do replacement of objects in the
599 * @param enable boolean parameter to enable replacement of objects
600 * @return the previous setting before this method was invoked
601 * @throws SecurityException if a security manager exists and its
602 * <code>checkPermission</code> method denies enabling the stream
603 * to do replacement of objects in the stream.
604 * @see SecurityManager#checkPermission
605 * @see java.io.SerializablePermission
607 protected boolean enableReplaceObject(boolean enable)
608 throws SecurityException
610 if (enable == enableReplace) {
614 SecurityManager sm = System.getSecurityManager();
616 sm.checkPermission(SUBSTITUTION_PERMISSION);
619 enableReplace = enable;
620 return !enableReplace;
624 * The writeStreamHeader method is provided so subclasses can append or
625 * prepend their own header to the stream. It writes the magic number and
626 * version to the stream.
628 * @throws IOException if I/O errors occur while writing to the underlying
631 protected void writeStreamHeader() throws IOException {
632 bout.writeShort(STREAM_MAGIC);
633 bout.writeShort(STREAM_VERSION);
637 * Write the specified class descriptor to the ObjectOutputStream. Class
638 * descriptors are used to identify the classes of objects written to the
639 * stream. Subclasses of ObjectOutputStream may override this method to
640 * customize the way in which class descriptors are written to the
641 * serialization stream. The corresponding method in ObjectInputStream,
642 * <code>readClassDescriptor</code>, should then be overridden to
643 * reconstitute the class descriptor from its custom stream representation.
644 * By default, this method writes class descriptors according to the format
645 * defined in the Object Serialization specification.
647 * <p>Note that this method will only be called if the ObjectOutputStream
648 * is not using the old serialization stream format (set by calling
649 * ObjectOutputStream's <code>useProtocolVersion</code> method). If this
650 * serialization stream is using the old format
651 * (<code>PROTOCOL_VERSION_1</code>), the class descriptor will be written
652 * internally in a manner that cannot be overridden or customized.
654 * @param desc class descriptor to write to the stream
655 * @throws IOException If an I/O error has occurred.
656 * @see java.io.ObjectInputStream#readClassDescriptor()
657 * @see #useProtocolVersion(int)
658 * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_1
661 protected void writeClassDescriptor(ObjectStreamClass desc)
664 desc.writeNonProxy(this);
668 * Writes a byte. This method will block until the byte is actually
671 * @param val the byte to be written to the stream
672 * @throws IOException If an I/O error has occurred.
674 public void write(int val) throws IOException {
679 * Writes an array of bytes. This method will block until the bytes are
682 * @param buf the data to be written
683 * @throws IOException If an I/O error has occurred.
685 public void write(byte[] buf) throws IOException {
686 bout.write(buf, 0, buf.length, false);
690 * Writes a sub array of bytes.
692 * @param buf the data to be written
693 * @param off the start offset in the data
694 * @param len the number of bytes that are written
695 * @throws IOException If an I/O error has occurred.
697 public void write(byte[] buf, int off, int len) throws IOException {
699 throw new NullPointerException();
701 int endoff = off + len;
702 if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
703 throw new IndexOutOfBoundsException();
705 bout.write(buf, off, len, false);
709 * Flushes the stream. This will write any buffered output bytes and flush
710 * through to the underlying stream.
712 * @throws IOException If an I/O error has occurred.
714 public void flush() throws IOException {
719 * Drain any buffered data in ObjectOutputStream. Similar to flush but
720 * does not propagate the flush to the underlying stream.
722 * @throws IOException if I/O errors occur while writing to the underlying
725 protected void drain() throws IOException {
730 * Closes the stream. This method must be called to release any resources
731 * associated with the stream.
733 * @throws IOException If an I/O error has occurred.
735 public void close() throws IOException {
744 * @param val the boolean to be written
745 * @throws IOException if I/O errors occur while writing to the underlying
748 public void writeBoolean(boolean val) throws IOException {
749 bout.writeBoolean(val);
753 * Writes an 8 bit byte.
755 * @param val the byte value to be written
756 * @throws IOException if I/O errors occur while writing to the underlying
759 public void writeByte(int val) throws IOException {
764 * Writes a 16 bit short.
766 * @param val the short value to be written
767 * @throws IOException if I/O errors occur while writing to the underlying
770 public void writeShort(int val) throws IOException {
771 bout.writeShort(val);
775 * Writes a 16 bit char.
777 * @param val the char value to be written
778 * @throws IOException if I/O errors occur while writing to the underlying
781 public void writeChar(int val) throws IOException {
786 * Writes a 32 bit int.
788 * @param val the integer value to be written
789 * @throws IOException if I/O errors occur while writing to the underlying
792 public void writeInt(int val) throws IOException {
797 * Writes a 64 bit long.
799 * @param val the long value to be written
800 * @throws IOException if I/O errors occur while writing to the underlying
803 public void writeLong(long val) throws IOException {
808 * Writes a 32 bit float.
810 * @param val the float value to be written
811 * @throws IOException if I/O errors occur while writing to the underlying
814 public void writeFloat(float val) throws IOException {
815 bout.writeFloat(val);
819 * Writes a 64 bit double.
821 * @param val the double value to be written
822 * @throws IOException if I/O errors occur while writing to the underlying
825 public void writeDouble(double val) throws IOException {
826 bout.writeDouble(val);
830 * Writes a String as a sequence of bytes.
832 * @param str the String of bytes to be written
833 * @throws IOException if I/O errors occur while writing to the underlying
836 public void writeBytes(String str) throws IOException {
837 bout.writeBytes(str);
841 * Writes a String as a sequence of chars.
843 * @param str the String of chars to be written
844 * @throws IOException if I/O errors occur while writing to the underlying
847 public void writeChars(String str) throws IOException {
848 bout.writeChars(str);
852 * Primitive data write of this String in
853 * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
854 * format. Note that there is a
855 * significant difference between writing a String into the stream as
856 * primitive data or as an Object. A String instance written by writeObject
857 * is written into the stream as a String initially. Future writeObject()
858 * calls write references to the string into the stream.
860 * @param str the String to be written
861 * @throws IOException if I/O errors occur while writing to the underlying
864 public void writeUTF(String str) throws IOException {
869 * Provide programmatic access to the persistent fields to be written
874 public static abstract class PutField {
877 * Put the value of the named boolean field into the persistent field.
879 * @param name the name of the serializable field
880 * @param val the value to assign to the field
881 * @throws IllegalArgumentException if <code>name</code> does not
882 * match the name of a serializable field for the class whose fields
883 * are being written, or if the type of the named field is not
884 * <code>boolean</code>
886 public abstract void put(String name, boolean val);
889 * Put the value of the named byte field into the persistent field.
891 * @param name the name of the serializable field
892 * @param val the value to assign to the field
893 * @throws IllegalArgumentException if <code>name</code> does not
894 * match the name of a serializable field for the class whose fields
895 * are being written, or if the type of the named field is not
898 public abstract void put(String name, byte val);
901 * Put the value of the named char field into the persistent field.
903 * @param name the name of the serializable field
904 * @param val the value to assign to the field
905 * @throws IllegalArgumentException if <code>name</code> does not
906 * match the name of a serializable field for the class whose fields
907 * are being written, or if the type of the named field is not
910 public abstract void put(String name, char val);
913 * Put the value of the named short field into the persistent field.
915 * @param name the name of the serializable field
916 * @param val the value to assign to the field
917 * @throws IllegalArgumentException if <code>name</code> does not
918 * match the name of a serializable field for the class whose fields
919 * are being written, or if the type of the named field is not
922 public abstract void put(String name, short val);
925 * Put the value of the named int field into the persistent field.
927 * @param name the name of the serializable field
928 * @param val the value to assign to the field
929 * @throws IllegalArgumentException if <code>name</code> does not
930 * match the name of a serializable field for the class whose fields
931 * are being written, or if the type of the named field is not
934 public abstract void put(String name, int val);
937 * Put the value of the named long field into the persistent field.
939 * @param name the name of the serializable field
940 * @param val the value to assign to the field
941 * @throws IllegalArgumentException if <code>name</code> does not
942 * match the name of a serializable field for the class whose fields
943 * are being written, or if the type of the named field is not
946 public abstract void put(String name, long val);
949 * Put the value of the named float field into the persistent field.
951 * @param name the name of the serializable field
952 * @param val the value to assign to the field
953 * @throws IllegalArgumentException if <code>name</code> does not
954 * match the name of a serializable field for the class whose fields
955 * are being written, or if the type of the named field is not
958 public abstract void put(String name, float val);
961 * Put the value of the named double field into the persistent field.
963 * @param name the name of the serializable field
964 * @param val the value to assign to the field
965 * @throws IllegalArgumentException if <code>name</code> does not
966 * match the name of a serializable field for the class whose fields
967 * are being written, or if the type of the named field is not
968 * <code>double</code>
970 public abstract void put(String name, double val);
973 * Put the value of the named Object field into the persistent field.
975 * @param name the name of the serializable field
976 * @param val the value to assign to the field
977 * (which may be <code>null</code>)
978 * @throws IllegalArgumentException if <code>name</code> does not
979 * match the name of a serializable field for the class whose fields
980 * are being written, or if the type of the named field is not a
983 public abstract void put(String name, Object val);
986 * Write the data and fields to the specified ObjectOutput stream,
987 * which must be the same stream that produced this
988 * <code>PutField</code> object.
990 * @param out the stream to write the data and fields to
991 * @throws IOException if I/O errors occur while writing to the
993 * @throws IllegalArgumentException if the specified stream is not
994 * the same stream that produced this <code>PutField</code>
996 * @deprecated This method does not write the values contained by this
997 * <code>PutField</code> object in a proper format, and may
998 * result in corruption of the serialization stream. The
999 * correct way to write <code>PutField</code> data is by
1000 * calling the {@link java.io.ObjectOutputStream#writeFields()}
1004 public abstract void write(ObjectOutput out) throws IOException;
1009 * Returns protocol version in use.
1011 int getProtocolVersion() {
1016 * Writes string without allowing it to be replaced in stream. Used by
1017 * ObjectStreamClass to write class descriptor type strings.
1019 void writeTypeString(String str) throws IOException {
1023 } else if ((handle = handles.lookup(str)) != -1) {
1024 writeHandle(handle);
1026 writeString(str, false);
1031 * Verifies that this (possibly subclass) instance can be constructed
1032 * without violating security constraints: the subclass must not override
1033 * security-sensitive non-final methods, or else the
1034 * "enableSubclassImplementation" SerializablePermission is checked.
1036 private void verifySubclass() {
1037 Class cl = getClass();
1038 if (cl == ObjectOutputStream.class) {
1041 SecurityManager sm = System.getSecurityManager();
1045 processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits);
1046 WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
1047 Boolean result = Caches.subclassAudits.get(key);
1048 if (result == null) {
1049 result = Boolean.valueOf(auditSubclass(cl));
1050 Caches.subclassAudits.putIfAbsent(key, result);
1052 if (result.booleanValue()) {
1055 sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
1059 * Performs reflective checks on given subclass to verify that it doesn't
1060 * override security-sensitive non-final methods. Returns true if subclass
1061 * is "safe", false otherwise.
1063 private static boolean auditSubclass(final Class subcl) {
1064 Boolean result = AccessController.doPrivileged(
1065 new PrivilegedAction<Boolean>() {
1066 public Boolean run() {
1067 for (Class cl = subcl;
1068 cl != ObjectOutputStream.class;
1069 cl = cl.getSuperclass())
1072 cl.getDeclaredMethod(
1073 "writeUnshared", new Class[] { Object.class });
1074 return Boolean.FALSE;
1075 } catch (NoSuchMethodException ex) {
1078 cl.getDeclaredMethod("putFields", (Class[]) null);
1079 return Boolean.FALSE;
1080 } catch (NoSuchMethodException ex) {
1083 return Boolean.TRUE;
1087 return result.booleanValue();
1091 * Clears internal data structures.
1093 private void clear() {
1099 * Underlying writeObject/writeUnshared implementation.
1101 private void writeObject0(Object obj, boolean unshared)
1104 boolean oldMode = bout.setBlockDataMode(false);
1107 // handle previously written and non-replaceable objects
1109 if ((obj = subs.lookup(obj)) == null) {
1112 } else if (!unshared && (h = handles.lookup(obj)) != -1) {
1115 } else if (obj instanceof Class) {
1116 writeClass((Class) obj, unshared);
1118 } else if (obj instanceof ObjectStreamClass) {
1119 writeClassDesc((ObjectStreamClass) obj, unshared);
1123 // check for replacement object
1125 Class cl = obj.getClass();
1126 ObjectStreamClass desc;
1128 // REMIND: skip this check for strings/arrays?
1130 desc = ObjectStreamClass.lookup(cl, true);
1131 if (!desc.hasWriteReplaceMethod() ||
1132 (obj = desc.invokeWriteReplace(obj)) == null ||
1133 (repCl = obj.getClass()) == cl)
1139 if (enableReplace) {
1140 Object rep = replaceObject(obj);
1141 if (rep != obj && rep != null) {
1142 cl = rep.getClass();
1143 desc = ObjectStreamClass.lookup(cl, true);
1148 // if object replaced, run through original checks a second time
1150 subs.assign(orig, obj);
1154 } else if (!unshared && (h = handles.lookup(obj)) != -1) {
1157 } else if (obj instanceof Class) {
1158 writeClass((Class) obj, unshared);
1160 } else if (obj instanceof ObjectStreamClass) {
1161 writeClassDesc((ObjectStreamClass) obj, unshared);
1167 if (obj instanceof String) {
1168 writeString((String) obj, unshared);
1169 } else if (cl.isArray()) {
1170 writeArray(obj, desc, unshared);
1171 } else if (obj instanceof Enum) {
1172 writeEnum((Enum) obj, desc, unshared);
1173 } else if (obj instanceof Serializable) {
1174 writeOrdinaryObject(obj, desc, unshared);
1176 if (extendedDebugInfo) {
1177 throw new NotSerializableException(
1178 cl.getName() + "\n" + debugInfoStack.toString());
1180 throw new NotSerializableException(cl.getName());
1185 bout.setBlockDataMode(oldMode);
1190 * Writes null code to stream.
1192 private void writeNull() throws IOException {
1193 bout.writeByte(TC_NULL);
1197 * Writes given object handle to stream.
1199 private void writeHandle(int handle) throws IOException {
1200 bout.writeByte(TC_REFERENCE);
1201 bout.writeInt(baseWireHandle + handle);
1205 * Writes representation of given class to stream.
1207 private void writeClass(Class cl, boolean unshared) throws IOException {
1208 bout.writeByte(TC_CLASS);
1209 writeClassDesc(ObjectStreamClass.lookup(cl, true), false);
1210 handles.assign(unshared ? null : cl);
1214 * Writes representation of given class descriptor to stream.
1216 private void writeClassDesc(ObjectStreamClass desc, boolean unshared)
1222 } else if (!unshared && (handle = handles.lookup(desc)) != -1) {
1223 writeHandle(handle);
1224 } else if (desc.isProxy()) {
1225 writeProxyDesc(desc, unshared);
1227 writeNonProxyDesc(desc, unshared);
1232 * Writes class descriptor representing a dynamic proxy class to stream.
1234 private void writeProxyDesc(ObjectStreamClass desc, boolean unshared)
1237 bout.writeByte(TC_PROXYCLASSDESC);
1238 handles.assign(unshared ? null : desc);
1240 Class cl = desc.forClass();
1241 Class[] ifaces = cl.getInterfaces();
1242 bout.writeInt(ifaces.length);
1243 for (int i = 0; i < ifaces.length; i++) {
1244 bout.writeUTF(ifaces[i].getName());
1247 bout.setBlockDataMode(true);
1248 annotateProxyClass(cl);
1249 bout.setBlockDataMode(false);
1250 bout.writeByte(TC_ENDBLOCKDATA);
1252 writeClassDesc(desc.getSuperDesc(), false);
1256 * Writes class descriptor representing a standard (i.e., not a dynamic
1257 * proxy) class to stream.
1259 private void writeNonProxyDesc(ObjectStreamClass desc, boolean unshared)
1262 bout.writeByte(TC_CLASSDESC);
1263 handles.assign(unshared ? null : desc);
1265 if (protocol == PROTOCOL_VERSION_1) {
1266 // do not invoke class descriptor write hook with old protocol
1267 desc.writeNonProxy(this);
1269 writeClassDescriptor(desc);
1272 Class cl = desc.forClass();
1273 bout.setBlockDataMode(true);
1275 bout.setBlockDataMode(false);
1276 bout.writeByte(TC_ENDBLOCKDATA);
1278 writeClassDesc(desc.getSuperDesc(), false);
1282 * Writes given string to stream, using standard or long UTF format
1283 * depending on string length.
1285 private void writeString(String str, boolean unshared) throws IOException {
1286 handles.assign(unshared ? null : str);
1287 long utflen = bout.getUTFLength(str);
1288 if (utflen <= 0xFFFF) {
1289 bout.writeByte(TC_STRING);
1290 bout.writeUTF(str, utflen);
1292 bout.writeByte(TC_LONGSTRING);
1293 bout.writeLongUTF(str, utflen);
1298 * Writes given array object to stream.
1300 private void writeArray(Object array,
1301 ObjectStreamClass desc,
1305 bout.writeByte(TC_ARRAY);
1306 writeClassDesc(desc, false);
1307 handles.assign(unshared ? null : array);
1309 Class ccl = desc.forClass().getComponentType();
1310 if (ccl.isPrimitive()) {
1311 if (ccl == Integer.TYPE) {
1312 int[] ia = (int[]) array;
1313 bout.writeInt(ia.length);
1314 bout.writeInts(ia, 0, ia.length);
1315 } else if (ccl == Byte.TYPE) {
1316 byte[] ba = (byte[]) array;
1317 bout.writeInt(ba.length);
1318 bout.write(ba, 0, ba.length, true);
1319 } else if (ccl == Long.TYPE) {
1320 long[] ja = (long[]) array;
1321 bout.writeInt(ja.length);
1322 bout.writeLongs(ja, 0, ja.length);
1323 } else if (ccl == Float.TYPE) {
1324 float[] fa = (float[]) array;
1325 bout.writeInt(fa.length);
1326 bout.writeFloats(fa, 0, fa.length);
1327 } else if (ccl == Double.TYPE) {
1328 double[] da = (double[]) array;
1329 bout.writeInt(da.length);
1330 bout.writeDoubles(da, 0, da.length);
1331 } else if (ccl == Short.TYPE) {
1332 short[] sa = (short[]) array;
1333 bout.writeInt(sa.length);
1334 bout.writeShorts(sa, 0, sa.length);
1335 } else if (ccl == Character.TYPE) {
1336 char[] ca = (char[]) array;
1337 bout.writeInt(ca.length);
1338 bout.writeChars(ca, 0, ca.length);
1339 } else if (ccl == Boolean.TYPE) {
1340 boolean[] za = (boolean[]) array;
1341 bout.writeInt(za.length);
1342 bout.writeBooleans(za, 0, za.length);
1344 throw new InternalError();
1347 Object[] objs = (Object[]) array;
1348 int len = objs.length;
1350 if (extendedDebugInfo) {
1351 debugInfoStack.push(
1352 "array (class \"" + array.getClass().getName() +
1353 "\", size: " + len + ")");
1356 for (int i = 0; i < len; i++) {
1357 if (extendedDebugInfo) {
1358 debugInfoStack.push(
1359 "element of array (index: " + i + ")");
1362 writeObject0(objs[i], false);
1364 if (extendedDebugInfo) {
1365 debugInfoStack.pop();
1370 if (extendedDebugInfo) {
1371 debugInfoStack.pop();
1378 * Writes given enum constant to stream.
1380 private void writeEnum(Enum en,
1381 ObjectStreamClass desc,
1385 bout.writeByte(TC_ENUM);
1386 ObjectStreamClass sdesc = desc.getSuperDesc();
1387 writeClassDesc((sdesc.forClass() == Enum.class) ? desc : sdesc, false);
1388 handles.assign(unshared ? null : en);
1389 writeString(en.name(), false);
1393 * Writes representation of a "ordinary" (i.e., not a String, Class,
1394 * ObjectStreamClass, array, or enum constant) serializable object to the
1397 private void writeOrdinaryObject(Object obj,
1398 ObjectStreamClass desc,
1402 if (extendedDebugInfo) {
1403 debugInfoStack.push(
1404 (depth == 1 ? "root " : "") + "object (class \"" +
1405 obj.getClass().getName() + "\", " + obj.toString() + ")");
1408 desc.checkSerialize();
1410 bout.writeByte(TC_OBJECT);
1411 writeClassDesc(desc, false);
1412 handles.assign(unshared ? null : obj);
1413 if (desc.isExternalizable() && !desc.isProxy()) {
1414 writeExternalData((Externalizable) obj);
1416 writeSerialData(obj, desc);
1419 if (extendedDebugInfo) {
1420 debugInfoStack.pop();
1426 * Writes externalizable data of given object by invoking its
1427 * writeExternal() method.
1429 private void writeExternalData(Externalizable obj) throws IOException {
1430 PutFieldImpl oldPut = curPut;
1433 if (extendedDebugInfo) {
1434 debugInfoStack.push("writeExternal data");
1436 SerialCallbackContext oldContext = curContext;
1439 if (protocol == PROTOCOL_VERSION_1) {
1440 obj.writeExternal(this);
1442 bout.setBlockDataMode(true);
1443 obj.writeExternal(this);
1444 bout.setBlockDataMode(false);
1445 bout.writeByte(TC_ENDBLOCKDATA);
1448 curContext = oldContext;
1449 if (extendedDebugInfo) {
1450 debugInfoStack.pop();
1458 * Writes instance data for each serializable class of given object, from
1459 * superclass to subclass.
1461 private void writeSerialData(Object obj, ObjectStreamClass desc)
1464 ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
1465 for (int i = 0; i < slots.length; i++) {
1466 ObjectStreamClass slotDesc = slots[i].desc;
1467 if (slotDesc.hasWriteObjectMethod()) {
1468 PutFieldImpl oldPut = curPut;
1470 SerialCallbackContext oldContext = curContext;
1472 if (extendedDebugInfo) {
1473 debugInfoStack.push(
1474 "custom writeObject data (class \"" +
1475 slotDesc.getName() + "\")");
1478 curContext = new SerialCallbackContext(obj, slotDesc);
1479 bout.setBlockDataMode(true);
1480 slotDesc.invokeWriteObject(obj, this);
1481 bout.setBlockDataMode(false);
1482 bout.writeByte(TC_ENDBLOCKDATA);
1484 curContext.setUsed();
1485 curContext = oldContext;
1486 if (extendedDebugInfo) {
1487 debugInfoStack.pop();
1493 defaultWriteFields(obj, slotDesc);
1499 * Fetches and writes values of serializable fields of given object to
1500 * stream. The given class descriptor specifies which field values to
1501 * write, and in which order they should be written.
1503 private void defaultWriteFields(Object obj, ObjectStreamClass desc)
1506 // REMIND: perform conservative isInstance check here?
1507 desc.checkDefaultSerialize();
1509 int primDataSize = desc.getPrimDataSize();
1510 if (primVals == null || primVals.length < primDataSize) {
1511 primVals = new byte[primDataSize];
1513 desc.getPrimFieldValues(obj, primVals);
1514 bout.write(primVals, 0, primDataSize, false);
1516 ObjectStreamField[] fields = desc.getFields(false);
1517 Object[] objVals = new Object[desc.getNumObjFields()];
1518 int numPrimFields = fields.length - objVals.length;
1519 desc.getObjFieldValues(obj, objVals);
1520 for (int i = 0; i < objVals.length; i++) {
1521 if (extendedDebugInfo) {
1522 debugInfoStack.push(
1523 "field (class \"" + desc.getName() + "\", name: \"" +
1524 fields[numPrimFields + i].getName() + "\", type: \"" +
1525 fields[numPrimFields + i].getType() + "\")");
1528 writeObject0(objVals[i],
1529 fields[numPrimFields + i].isUnshared());
1531 if (extendedDebugInfo) {
1532 debugInfoStack.pop();
1539 * Attempts to write to stream fatal IOException that has caused
1540 * serialization to abort.
1542 private void writeFatalException(IOException ex) throws IOException {
1544 * Note: the serialization specification states that if a second
1545 * IOException occurs while attempting to serialize the original fatal
1546 * exception to the stream, then a StreamCorruptedException should be
1547 * thrown (section 2.1). However, due to a bug in previous
1548 * implementations of serialization, StreamCorruptedExceptions were
1549 * rarely (if ever) actually thrown--the "root" exceptions from
1550 * underlying streams were thrown instead. This historical behavior is
1551 * followed here for consistency.
1554 boolean oldMode = bout.setBlockDataMode(false);
1556 bout.writeByte(TC_EXCEPTION);
1557 writeObject0(ex, false);
1560 bout.setBlockDataMode(oldMode);
1565 * Converts specified span of float values into byte values.
1567 // REMIND: remove once hotspot inlines Float.floatToIntBits
1568 private static native void floatsToBytes(float[] src, int srcpos,
1569 byte[] dst, int dstpos,
1573 * Converts specified span of double values into byte values.
1575 // REMIND: remove once hotspot inlines Double.doubleToLongBits
1576 private static native void doublesToBytes(double[] src, int srcpos,
1577 byte[] dst, int dstpos,
1581 * Default PutField implementation.
1583 private class PutFieldImpl extends PutField {
1585 /** class descriptor describing serializable fields */
1586 private final ObjectStreamClass desc;
1587 /** primitive field values */
1588 private final byte[] primVals;
1589 /** object field values */
1590 private final Object[] objVals;
1593 * Creates PutFieldImpl object for writing fields defined in given
1596 PutFieldImpl(ObjectStreamClass desc) {
1598 primVals = new byte[desc.getPrimDataSize()];
1599 objVals = new Object[desc.getNumObjFields()];
1602 public void put(String name, boolean val) {
1603 Bits.putBoolean(primVals, getFieldOffset(name, Boolean.TYPE), val);
1606 public void put(String name, byte val) {
1607 primVals[getFieldOffset(name, Byte.TYPE)] = val;
1610 public void put(String name, char val) {
1611 Bits.putChar(primVals, getFieldOffset(name, Character.TYPE), val);
1614 public void put(String name, short val) {
1615 Bits.putShort(primVals, getFieldOffset(name, Short.TYPE), val);
1618 public void put(String name, int val) {
1619 Bits.putInt(primVals, getFieldOffset(name, Integer.TYPE), val);
1622 public void put(String name, float val) {
1623 Bits.putFloat(primVals, getFieldOffset(name, Float.TYPE), val);
1626 public void put(String name, long val) {
1627 Bits.putLong(primVals, getFieldOffset(name, Long.TYPE), val);
1630 public void put(String name, double val) {
1631 Bits.putDouble(primVals, getFieldOffset(name, Double.TYPE), val);
1634 public void put(String name, Object val) {
1635 objVals[getFieldOffset(name, Object.class)] = val;
1638 // deprecated in ObjectOutputStream.PutField
1639 public void write(ObjectOutput out) throws IOException {
1641 * Applications should *not* use this method to write PutField
1642 * data, as it will lead to stream corruption if the PutField
1643 * object writes any primitive data (since block data mode is not
1644 * unset/set properly, as is done in OOS.writeFields()). This
1645 * broken implementation is being retained solely for behavioral
1646 * compatibility, in order to support applications which use
1647 * OOS.PutField.write() for writing only non-primitive data.
1649 * Serialization of unshared objects is not implemented here since
1650 * it is not necessary for backwards compatibility; also, unshared
1651 * semantics may not be supported by the given ObjectOutput
1652 * instance. Applications which write unshared objects using the
1653 * PutField API must use OOS.writeFields().
1655 if (ObjectOutputStream.this != out) {
1656 throw new IllegalArgumentException("wrong stream");
1658 out.write(primVals, 0, primVals.length);
1660 ObjectStreamField[] fields = desc.getFields(false);
1661 int numPrimFields = fields.length - objVals.length;
1662 // REMIND: warn if numPrimFields > 0?
1663 for (int i = 0; i < objVals.length; i++) {
1664 if (fields[numPrimFields + i].isUnshared()) {
1665 throw new IOException("cannot write unshared object");
1667 out.writeObject(objVals[i]);
1672 * Writes buffered primitive data and object fields to stream.
1674 void writeFields() throws IOException {
1675 bout.write(primVals, 0, primVals.length, false);
1677 ObjectStreamField[] fields = desc.getFields(false);
1678 int numPrimFields = fields.length - objVals.length;
1679 for (int i = 0; i < objVals.length; i++) {
1680 if (extendedDebugInfo) {
1681 debugInfoStack.push(
1682 "field (class \"" + desc.getName() + "\", name: \"" +
1683 fields[numPrimFields + i].getName() + "\", type: \"" +
1684 fields[numPrimFields + i].getType() + "\")");
1687 writeObject0(objVals[i],
1688 fields[numPrimFields + i].isUnshared());
1690 if (extendedDebugInfo) {
1691 debugInfoStack.pop();
1698 * Returns offset of field with given name and type. A specified type
1699 * of null matches all types, Object.class matches all non-primitive
1700 * types, and any other non-null type matches assignable types only.
1701 * Throws IllegalArgumentException if no matching field found.
1703 private int getFieldOffset(String name, Class type) {
1704 ObjectStreamField field = desc.getField(name, type);
1705 if (field == null) {
1706 throw new IllegalArgumentException("no such field " + name +
1707 " with type " + type);
1709 return field.getOffset();
1714 * Buffered output stream with two modes: in default mode, outputs data in
1715 * same format as DataOutputStream; in "block data" mode, outputs data
1716 * bracketed by block data markers (see object serialization specification
1719 private static class BlockDataOutputStream
1720 extends OutputStream implements DataOutput
1722 /** maximum data block length */
1723 private static final int MAX_BLOCK_SIZE = 1024;
1724 /** maximum data block header length */
1725 private static final int MAX_HEADER_SIZE = 5;
1726 /** (tunable) length of char buffer (for writing strings) */
1727 private static final int CHAR_BUF_SIZE = 256;
1729 /** buffer for writing general/block data */
1730 private final byte[] buf = new byte[MAX_BLOCK_SIZE];
1731 /** buffer for writing block data headers */
1732 private final byte[] hbuf = new byte[MAX_HEADER_SIZE];
1733 /** char buffer for fast string writes */
1734 private final char[] cbuf = new char[CHAR_BUF_SIZE];
1736 /** block data mode */
1737 private boolean blkmode = false;
1738 /** current offset into buf */
1739 private int pos = 0;
1741 /** underlying output stream */
1742 private final OutputStream out;
1743 /** loopback stream (for data writes that span data blocks) */
1744 private final DataOutputStream dout;
1747 * Creates new BlockDataOutputStream on top of given underlying stream.
1748 * Block data mode is turned off by default.
1750 BlockDataOutputStream(OutputStream out) {
1752 dout = new DataOutputStream(this);
1756 * Sets block data mode to the given mode (true == on, false == off)
1757 * and returns the previous mode value. If the new mode is the same as
1758 * the old mode, no action is taken. If the new mode differs from the
1759 * old mode, any buffered data is flushed before switching to the new
1762 boolean setBlockDataMode(boolean mode) throws IOException {
1763 if (blkmode == mode) {
1772 * Returns true if the stream is currently in block data mode, false
1775 boolean getBlockDataMode() {
1779 /* ----------------- generic output stream methods ----------------- */
1781 * The following methods are equivalent to their counterparts in
1782 * OutputStream, except that they partition written data into data
1783 * blocks when in block data mode.
1786 public void write(int b) throws IOException {
1787 if (pos >= MAX_BLOCK_SIZE) {
1790 buf[pos++] = (byte) b;
1793 public void write(byte[] b) throws IOException {
1794 write(b, 0, b.length, false);
1797 public void write(byte[] b, int off, int len) throws IOException {
1798 write(b, off, len, false);
1801 public void flush() throws IOException {
1806 public void close() throws IOException {
1812 * Writes specified span of byte values from given array. If copy is
1813 * true, copies the values to an intermediate buffer before writing
1814 * them to underlying stream (to avoid exposing a reference to the
1815 * original byte array).
1817 void write(byte[] b, int off, int len, boolean copy)
1820 if (!(copy || blkmode)) { // write directly
1822 out.write(b, off, len);
1827 if (pos >= MAX_BLOCK_SIZE) {
1830 if (len >= MAX_BLOCK_SIZE && !copy && pos == 0) {
1831 // avoid unnecessary copy
1832 writeBlockHeader(MAX_BLOCK_SIZE);
1833 out.write(b, off, MAX_BLOCK_SIZE);
1834 off += MAX_BLOCK_SIZE;
1835 len -= MAX_BLOCK_SIZE;
1837 int wlen = Math.min(len, MAX_BLOCK_SIZE - pos);
1838 System.arraycopy(b, off, buf, pos, wlen);
1847 * Writes all buffered data from this stream to the underlying stream,
1848 * but does not flush underlying stream.
1850 void drain() throws IOException {
1855 writeBlockHeader(pos);
1857 out.write(buf, 0, pos);
1862 * Writes block data header. Data blocks shorter than 256 bytes are
1863 * prefixed with a 2-byte header; all others start with a 5-byte
1866 private void writeBlockHeader(int len) throws IOException {
1868 hbuf[0] = TC_BLOCKDATA;
1869 hbuf[1] = (byte) len;
1870 out.write(hbuf, 0, 2);
1872 hbuf[0] = TC_BLOCKDATALONG;
1873 Bits.putInt(hbuf, 1, len);
1874 out.write(hbuf, 0, 5);
1879 /* ----------------- primitive data output methods ----------------- */
1881 * The following methods are equivalent to their counterparts in
1882 * DataOutputStream, except that they partition written data into data
1883 * blocks when in block data mode.
1886 public void writeBoolean(boolean v) throws IOException {
1887 if (pos >= MAX_BLOCK_SIZE) {
1890 Bits.putBoolean(buf, pos++, v);
1893 public void writeByte(int v) throws IOException {
1894 if (pos >= MAX_BLOCK_SIZE) {
1897 buf[pos++] = (byte) v;
1900 public void writeChar(int v) throws IOException {
1901 if (pos + 2 <= MAX_BLOCK_SIZE) {
1902 Bits.putChar(buf, pos, (char) v);
1909 public void writeShort(int v) throws IOException {
1910 if (pos + 2 <= MAX_BLOCK_SIZE) {
1911 Bits.putShort(buf, pos, (short) v);
1918 public void writeInt(int v) throws IOException {
1919 if (pos + 4 <= MAX_BLOCK_SIZE) {
1920 Bits.putInt(buf, pos, v);
1927 public void writeFloat(float v) throws IOException {
1928 if (pos + 4 <= MAX_BLOCK_SIZE) {
1929 Bits.putFloat(buf, pos, v);
1936 public void writeLong(long v) throws IOException {
1937 if (pos + 8 <= MAX_BLOCK_SIZE) {
1938 Bits.putLong(buf, pos, v);
1945 public void writeDouble(double v) throws IOException {
1946 if (pos + 8 <= MAX_BLOCK_SIZE) {
1947 Bits.putDouble(buf, pos, v);
1950 dout.writeDouble(v);
1954 public void writeBytes(String s) throws IOException {
1955 int endoff = s.length();
1958 for (int off = 0; off < endoff; ) {
1959 if (cpos >= csize) {
1961 csize = Math.min(endoff - off, CHAR_BUF_SIZE);
1962 s.getChars(off, off + csize, cbuf, 0);
1964 if (pos >= MAX_BLOCK_SIZE) {
1967 int n = Math.min(csize - cpos, MAX_BLOCK_SIZE - pos);
1969 while (pos < stop) {
1970 buf[pos++] = (byte) cbuf[cpos++];
1976 public void writeChars(String s) throws IOException {
1977 int endoff = s.length();
1978 for (int off = 0; off < endoff; ) {
1979 int csize = Math.min(endoff - off, CHAR_BUF_SIZE);
1980 s.getChars(off, off + csize, cbuf, 0);
1981 writeChars(cbuf, 0, csize);
1986 public void writeUTF(String s) throws IOException {
1987 writeUTF(s, getUTFLength(s));
1991 /* -------------- primitive data array output methods -------------- */
1993 * The following methods write out spans of primitive data values.
1994 * Though equivalent to calling the corresponding primitive write
1995 * methods repeatedly, these methods are optimized for writing groups
1996 * of primitive data values more efficiently.
1999 void writeBooleans(boolean[] v, int off, int len) throws IOException {
2000 int endoff = off + len;
2001 while (off < endoff) {
2002 if (pos >= MAX_BLOCK_SIZE) {
2005 int stop = Math.min(endoff, off + (MAX_BLOCK_SIZE - pos));
2006 while (off < stop) {
2007 Bits.putBoolean(buf, pos++, v[off++]);
2012 void writeChars(char[] v, int off, int len) throws IOException {
2013 int limit = MAX_BLOCK_SIZE - 2;
2014 int endoff = off + len;
2015 while (off < endoff) {
2017 int avail = (MAX_BLOCK_SIZE - pos) >> 1;
2018 int stop = Math.min(endoff, off + avail);
2019 while (off < stop) {
2020 Bits.putChar(buf, pos, v[off++]);
2024 dout.writeChar(v[off++]);
2029 void writeShorts(short[] v, int off, int len) throws IOException {
2030 int limit = MAX_BLOCK_SIZE - 2;
2031 int endoff = off + len;
2032 while (off < endoff) {
2034 int avail = (MAX_BLOCK_SIZE - pos) >> 1;
2035 int stop = Math.min(endoff, off + avail);
2036 while (off < stop) {
2037 Bits.putShort(buf, pos, v[off++]);
2041 dout.writeShort(v[off++]);
2046 void writeInts(int[] v, int off, int len) throws IOException {
2047 int limit = MAX_BLOCK_SIZE - 4;
2048 int endoff = off + len;
2049 while (off < endoff) {
2051 int avail = (MAX_BLOCK_SIZE - pos) >> 2;
2052 int stop = Math.min(endoff, off + avail);
2053 while (off < stop) {
2054 Bits.putInt(buf, pos, v[off++]);
2058 dout.writeInt(v[off++]);
2063 void writeFloats(float[] v, int off, int len) throws IOException {
2064 int limit = MAX_BLOCK_SIZE - 4;
2065 int endoff = off + len;
2066 while (off < endoff) {
2068 int avail = (MAX_BLOCK_SIZE - pos) >> 2;
2069 int chunklen = Math.min(endoff - off, avail);
2070 floatsToBytes(v, off, buf, pos, chunklen);
2072 pos += chunklen << 2;
2074 dout.writeFloat(v[off++]);
2079 void writeLongs(long[] v, int off, int len) throws IOException {
2080 int limit = MAX_BLOCK_SIZE - 8;
2081 int endoff = off + len;
2082 while (off < endoff) {
2084 int avail = (MAX_BLOCK_SIZE - pos) >> 3;
2085 int stop = Math.min(endoff, off + avail);
2086 while (off < stop) {
2087 Bits.putLong(buf, pos, v[off++]);
2091 dout.writeLong(v[off++]);
2096 void writeDoubles(double[] v, int off, int len) throws IOException {
2097 int limit = MAX_BLOCK_SIZE - 8;
2098 int endoff = off + len;
2099 while (off < endoff) {
2101 int avail = (MAX_BLOCK_SIZE - pos) >> 3;
2102 int chunklen = Math.min(endoff - off, avail);
2103 doublesToBytes(v, off, buf, pos, chunklen);
2105 pos += chunklen << 3;
2107 dout.writeDouble(v[off++]);
2113 * Returns the length in bytes of the UTF encoding of the given string.
2115 long getUTFLength(String s) {
2116 int len = s.length();
2118 for (int off = 0; off < len; ) {
2119 int csize = Math.min(len - off, CHAR_BUF_SIZE);
2120 s.getChars(off, off + csize, cbuf, 0);
2121 for (int cpos = 0; cpos < csize; cpos++) {
2122 char c = cbuf[cpos];
2123 if (c >= 0x0001 && c <= 0x007F) {
2125 } else if (c > 0x07FF) {
2137 * Writes the given string in UTF format. This method is used in
2138 * situations where the UTF encoding length of the string is already
2139 * known; specifying it explicitly avoids a prescan of the string to
2140 * determine its UTF length.
2142 void writeUTF(String s, long utflen) throws IOException {
2143 if (utflen > 0xFFFFL) {
2144 throw new UTFDataFormatException();
2146 writeShort((int) utflen);
2147 if (utflen == (long) s.length()) {
2155 * Writes given string in "long" UTF format. "Long" UTF format is
2156 * identical to standard UTF, except that it uses an 8 byte header
2157 * (instead of the standard 2 bytes) to convey the UTF encoding length.
2159 void writeLongUTF(String s) throws IOException {
2160 writeLongUTF(s, getUTFLength(s));
2164 * Writes given string in "long" UTF format, where the UTF encoding
2165 * length of the string is already known.
2167 void writeLongUTF(String s, long utflen) throws IOException {
2169 if (utflen == (long) s.length()) {
2177 * Writes the "body" (i.e., the UTF representation minus the 2-byte or
2178 * 8-byte length header) of the UTF encoding for the given string.
2180 private void writeUTFBody(String s) throws IOException {
2181 int limit = MAX_BLOCK_SIZE - 3;
2182 int len = s.length();
2183 for (int off = 0; off < len; ) {
2184 int csize = Math.min(len - off, CHAR_BUF_SIZE);
2185 s.getChars(off, off + csize, cbuf, 0);
2186 for (int cpos = 0; cpos < csize; cpos++) {
2187 char c = cbuf[cpos];
2189 if (c <= 0x007F && c != 0) {
2190 buf[pos++] = (byte) c;
2191 } else if (c > 0x07FF) {
2192 buf[pos + 2] = (byte) (0x80 | ((c >> 0) & 0x3F));
2193 buf[pos + 1] = (byte) (0x80 | ((c >> 6) & 0x3F));
2194 buf[pos + 0] = (byte) (0xE0 | ((c >> 12) & 0x0F));
2197 buf[pos + 1] = (byte) (0x80 | ((c >> 0) & 0x3F));
2198 buf[pos + 0] = (byte) (0xC0 | ((c >> 6) & 0x1F));
2201 } else { // write one byte at a time to normalize block
2202 if (c <= 0x007F && c != 0) {
2204 } else if (c > 0x07FF) {
2205 write(0xE0 | ((c >> 12) & 0x0F));
2206 write(0x80 | ((c >> 6) & 0x3F));
2207 write(0x80 | ((c >> 0) & 0x3F));
2209 write(0xC0 | ((c >> 6) & 0x1F));
2210 write(0x80 | ((c >> 0) & 0x3F));
2220 * Lightweight identity hash table which maps objects to integer handles,
2221 * assigned in ascending order.
2223 private static class HandleTable {
2225 /* number of mappings in table/next available handle */
2227 /* size threshold determining when to expand hash spine */
2228 private int threshold;
2229 /* factor for computing size threshold */
2230 private final float loadFactor;
2231 /* maps hash value -> candidate handle value */
2232 private int[] spine;
2233 /* maps handle value -> next candidate handle value */
2235 /* maps handle value -> associated object */
2236 private Object[] objs;
2239 * Creates new HandleTable with given capacity and load factor.
2241 HandleTable(int initialCapacity, float loadFactor) {
2242 this.loadFactor = loadFactor;
2243 spine = new int[initialCapacity];
2244 next = new int[initialCapacity];
2245 objs = new Object[initialCapacity];
2246 threshold = (int) (initialCapacity * loadFactor);
2251 * Assigns next available handle to given object, and returns handle
2252 * value. Handles are assigned in ascending order starting at 0.
2254 int assign(Object obj) {
2255 if (size >= next.length) {
2258 if (size >= threshold) {
2266 * Looks up and returns handle associated with given object, or -1 if
2269 int lookup(Object obj) {
2273 int index = hash(obj) % spine.length;
2274 for (int i = spine[index]; i >= 0; i = next[i]) {
2275 if (objs[i] == obj) {
2283 * Resets table to its initial (empty) state.
2286 Arrays.fill(spine, -1);
2287 Arrays.fill(objs, 0, size, null);
2292 * Returns the number of mappings currently in table.
2299 * Inserts mapping object -> handle mapping into table. Assumes table
2300 * is large enough to accommodate new mapping.
2302 private void insert(Object obj, int handle) {
2303 int index = hash(obj) % spine.length;
2305 next[handle] = spine[index];
2306 spine[index] = handle;
2310 * Expands the hash "spine" -- equivalent to increasing the number of
2311 * buckets in a conventional hash table.
2313 private void growSpine() {
2314 spine = new int[(spine.length << 1) + 1];
2315 threshold = (int) (spine.length * loadFactor);
2316 Arrays.fill(spine, -1);
2317 for (int i = 0; i < size; i++) {
2323 * Increases hash table capacity by lengthening entry arrays.
2325 private void growEntries() {
2326 int newLength = (next.length << 1) + 1;
2327 int[] newNext = new int[newLength];
2328 System.arraycopy(next, 0, newNext, 0, size);
2331 Object[] newObjs = new Object[newLength];
2332 System.arraycopy(objs, 0, newObjs, 0, size);
2337 * Returns hash value for given object.
2339 private int hash(Object obj) {
2340 return System.identityHashCode(obj) & 0x7FFFFFFF;
2345 * Lightweight identity hash table which maps objects to replacement
2348 private static class ReplaceTable {
2350 /* maps object -> index */
2351 private final HandleTable htab;
2352 /* maps index -> replacement object */
2353 private Object[] reps;
2356 * Creates new ReplaceTable with given capacity and load factor.
2358 ReplaceTable(int initialCapacity, float loadFactor) {
2359 htab = new HandleTable(initialCapacity, loadFactor);
2360 reps = new Object[initialCapacity];
2364 * Enters mapping from object to replacement object.
2366 void assign(Object obj, Object rep) {
2367 int index = htab.assign(obj);
2368 while (index >= reps.length) {
2375 * Looks up and returns replacement for given object. If no
2376 * replacement is found, returns the lookup object itself.
2378 Object lookup(Object obj) {
2379 int index = htab.lookup(obj);
2380 return (index >= 0) ? reps[index] : obj;
2384 * Resets table to its initial (empty) state.
2387 Arrays.fill(reps, 0, htab.size(), null);
2392 * Returns the number of mappings currently in table.
2399 * Increases table capacity.
2401 private void grow() {
2402 Object[] newReps = new Object[(reps.length << 1) + 1];
2403 System.arraycopy(reps, 0, newReps, 0, reps.length);
2409 * Stack to keep debug information about the state of the
2410 * serialization process, for embedding in exception messages.
2412 private static class DebugTraceInfoStack {
2413 private final List<String> stack;
2415 DebugTraceInfoStack() {
2416 stack = new ArrayList<>();
2420 * Removes all of the elements from enclosed list.
2427 * Removes the object at the top of enclosed list.
2430 stack.remove(stack.size()-1);
2434 * Pushes a String onto the top of enclosed list.
2436 void push(String entry) {
2437 stack.add("\t- " + entry);
2441 * Returns a string representation of this object
2443 public String toString() {
2444 StringBuilder buffer = new StringBuilder();
2445 if (!stack.isEmpty()) {
2446 for(int i = stack.size(); i > 0; i-- ) {
2447 buffer.append(stack.get(i-1) + ((i != 1) ? "\n" : ""));
2450 return buffer.toString();