objectoutputstream.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,976 行 · 第 1/5 页

JAVA
1,976
字号
    /**     * Retrieve the object used to buffer persistent fields to be written to     * the stream.  The fields will be written to the stream when writeFields     * method is called.     *      * @return	an instance of the class Putfield that holds the serializable     * 		fields     * @throws	IOException if I/O errors occur     * @since 1.2     */    public ObjectOutputStream.PutField putFields() throws IOException {	if (curPut == null) {	    if (curObj == null || curDesc == null) {		throw new NotActiveException("not in call to writeObject");	    }	    curPut = new PutFieldImpl(curDesc);	}	return curPut;    }    /**     * Write the buffered fields to the stream.     *     * @throws IOException if I/O errors occur while writing to the underlying     * stream     * @throws NotActiveException Called when a classes writeObject     * method was not called to write the state of the object.     * @since 1.2     */    public void writeFields() throws IOException {	if (curPut == null) {	    throw new NotActiveException("no current PutField object");	}	bout.setBlockDataMode(false);	curPut.writeFields();	bout.setBlockDataMode(true);    }        /**     * Reset will disregard the state of any objects already written     * to the stream.  The state is reset to be the same as a new     * ObjectOutputStream.  The current point in the stream is marked     * as reset so the corresponding ObjectInputStream will be reset     * at the same point.  Objects previously written to the stream     * will not be refered to as already being in the stream.  They     * will be written to the stream again.     *      * @throws IOException if reset() is invoked while serializing an object.     */    public void reset() throws IOException {	if (depth != 0) {	    throw new IOException("stream active");	}	bout.setBlockDataMode(false);	bout.writeByte(TC_RESET);	clear();	bout.setBlockDataMode(true);    }    /**     * Subclasses may implement this method to allow class data to be stored in     * the stream. By default this method does nothing.  The corresponding     * method in ObjectInputStream is resolveClass.  This method is called     * exactly once for each unique class in the stream.  The class name and     * signature will have already been written to the stream.  This method may     * make free use of the ObjectOutputStream to save any representation of     * the class it deems suitable (for example, the bytes of the class file).     * The resolveClass method in the corresponding subclass of     * ObjectInputStream must read and use any data or objects written by     * annotateClass.     *     * @param	cl the class to annotate custom data for     * @throws	IOException Any exception thrown by the underlying     * 		OutputStream.     */    protected void annotateClass(Class cl) throws IOException {    }    /**     * Subclasses may implement this method to store custom data in the     * stream along with descriptors for dynamic proxy classes.     *     * <p>This method is called exactly once for each unique proxy class     * descriptor in the stream.  The default implementation of this     * method in <code>ObjectOutputStream</code> does nothing.     *     * <p>The corresponding method in <code>ObjectInputStream</code> is     * <code>resolveProxyClass</code>.  For a given subclass of     * <code>ObjectOutputStream</code> that overrides this method, the     * <code>resolveProxyClass</code> method in the corresponding     * subclass of <code>ObjectInputStream</code> must read any data or     * objects writtem by <code>annotateProxyClass</code>.     *     * @param	cl the proxy class to annotate custom data for     * @throws	IOException any exception thrown by the underlying     *			    <code>OutputStream</code>     * @see ObjectInputStream#resolveProxyClass(String[])     * @since	1.3     */    protected void annotateProxyClass(Class cl) throws IOException {    }    /**      * This method will allow trusted subclasses of ObjectOutputStream to     * substitute one object for another during serialization. Replacing     * objects is disabled until enableReplaceObject is called. The     * enableReplaceObject method checks that the stream requesting to do     * replacment can be trusted.  The first occurrence of each object written     * into the serialization stream is passed to replaceObject.  Subsequent     * references to the object are replaced by the object returned by the     * original call to replaceObject.  To ensure that the private state of     * objects is not unintentionally exposed, only trusted streams may use     * replaceObject.     *      * <p>The ObjectOutputStream.writeObject method takes a parameter of type     * Object (as opposed to type Serializable) to allow for cases where     * non-serializable objects are replaced by serializable ones.     *      * <p>When a subclass is replacing objects it must insure that either a     * complementary substitution must be made during deserialization or that     * the substituted object is compatible with every field where the     * reference will be stored.  Objects whose type is not a subclass of the     * type of the field or array element abort the serialization by raising an     * exception and the object is not be stored.     *     * <p>This method is called only once when each object is first     * encountered.  All subsequent references to the object will be redirected     * to the new object. This method should return the object to be     * substituted or the original object.     *     * <p>Null can be returned as the object to be substituted, but may cause     * NullReferenceException in classes that contain references to the     * original object since they may be expecting an object instead of     * null.     *     * @param	obj the object to be replaced     * @return	the alternate object that replaced the specified one     * @throws	IOException Any exception thrown by the underlying     * 		OutputStream.     */    protected Object replaceObject(Object obj) throws IOException {	return obj;    }    /**     * Enable the stream to do replacement of objects in the stream.     *     * <p>When enabled, the replaceObject method is called for every object     * being serialized.     *     * <p>If <i>enable</i> is true, and there is a security manager installed,     * this method first calls the     * security manager's <code>checkPermission</code> method with a     * <code>SerializablePermission("enableSubstitution")</code>     * permission to ensure it's ok to      * enable the stream to do replacement of objects in the stream.     *     * @param enable boolean parameter to enable replacement of objects     * @return the previous setting before this method was invoked     * @throws SecurityException     *    if a security manager exists and its      *    <code>checkPermission</code> method denies     *    enabling the stream to do replacement of objects in the stream.     *     * @see SecurityManager#checkPermission     * @see java.io.SerializablePermission     */    protected boolean enableReplaceObject(boolean enable)	throws SecurityException    {	if (enable == enableReplace) {	    return enable;	}	if (enable) {	    SecurityManager sm = System.getSecurityManager();	    if (sm != null) {		sm.checkPermission(ObjectStreamConstants.SUBSTITUTION_PERMISSION);	    }	}	enableReplace = enable;	return !enableReplace;    }    /**     * The writeStreamHeader method is provided so subclasses can     * append or prepend their own header to the stream.     * It writes the magic number and version to the stream.     *     * @throws IOException if I/O errors occur while writing to the underlying     * stream     */    protected void writeStreamHeader() throws IOException {	bout.writeShort(STREAM_MAGIC);	bout.writeShort(STREAM_VERSION);    }    /**     * Write the specified class descriptor to the ObjectOutputStream.  Class     * descriptors are used to identify the classes of objects written to the     * stream.  Subclasses of ObjectOutputStream may override this method to     * customize the way in which class descriptors are written to the     * serialization stream.  The corresponding method in ObjectInputStream,     * <code>readClassDescriptor</code>, should then be overridden to     * reconstitute the class descriptor from its custom stream representation.     * By default, this method writes class descriptors according to the format     * defined in the Object Serialization specification.     *      * <p>Note that this method will only be called if the ObjectOutputStream     * is not using the old serialization stream format (set by calling     * ObjectOutputStream's <code>useProtocolVersion</code> method).  If this     * serialization stream is using the old format     * (<code>PROTOCOL_VERSION_1</code>), the class descriptor will be written     * internally in a manner that cannot be overridden or customized.     *     * @param	desc class descriptor to write to the stream     * @throws	IOException If an I/O error has occurred.     * @see java.io.ObjectInputStream#readClassDescriptor()     * @see #useProtocolVersion(int)     * @see java.io.ObjectStreamConstants#PROTOCOL_VERSION_1     * @since 1.3     */    protected void writeClassDescriptor(ObjectStreamClass desc)	throws IOException    {	desc.writeNonProxy(this);    }    /**     * Writes a byte. This method will block until the byte is actually     * written.     *     * @param	val the byte to be written to the stream     * @throws	IOException If an I/O error has occurred.     */    public void write(int val) throws IOException {	bout.write(val);    }    /**     * Writes an array of bytes. This method will block until the bytes     * are actually written.     *     * @param buf	the data to be written     * @exception IOException If an I/O error has occurred.     */    public void write(byte[] buf) throws IOException {	bout.write(buf, 0, buf.length, false);    }    /**     * Writes a sub array of bytes.     *     * @param buf	the data to be written     * @param off	the start offset in the data     * @param len	the number of bytes that are written     * @param copyOnWrite do not expose b to overrides of ObjectStream.write,     *                    copy the contents of b to a buffer before writing.     * @exception IOException If an I/O error has occurred.     */    public void write(byte[] buf, int off, int len) throws IOException {	int endoff = off + len;	if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {	    throw new IndexOutOfBoundsException();	}	bout.write(buf, off, len, false);    }    /**     * Flushes the stream. This will write any buffered output bytes and flush     * through to the underlying stream.     *     * @throws	IOException If an I/O error has occurred.     */    public void flush() throws IOException {	bout.flush();    }    /**     * Drain any buffered data in ObjectOutputStream.  Similar to flush but     * does not propagate the flush to the underlying stream.     *     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    protected void drain() throws IOException {	bout.drain();    }    /**     * Closes the stream. This method must be called to release any resources     * associated with the stream.     *     * @throws	IOException If an I/O error has occurred.     */    public void close() throws IOException {	flush();	clear();	bout.close();    }    /**     * Writes a boolean.     *     * @param val the boolean to be written     * @throws IOException if I/O errors occur while writing to the underlying     * stream     */    public void writeBoolean(boolean val) throws IOException {	bout.writeBoolean(val);    }    /**     * Writes an 8 bit byte.     *     * @param val the byte value to be written     * @throws IOException if I/O errors occur while writing to the underlying     * stream     */    public void writeByte(int val) throws IOException  {	bout.writeByte(val);    }    /**     * Writes a 16 bit short.     *     * @param	val the short value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeShort(int val)  throws IOException {	bout.writeShort(val);    }    /**     * Writes a 16 bit char.     *     * @param	val the char value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeChar(int val)  throws IOException {	bout.writeChar(val);    }    /**     * Writes a 32 bit int.     *     * @param	val the integer value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeInt(int val)  throws IOException {	bout.writeInt(val);    }    /**     * Writes a 64 bit long.     *     * @param	val the long value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeLong(long val)  throws IOException {	bout.writeLong(val);    }    /**     * Writes a 32 bit float.     *     * @param	val the float value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeFloat(float val) throws IOException {	bout.writeFloat(val);    }    /**     * Writes a 64 bit double.     *     * @param	val the double value to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeDouble(double val) throws IOException {	bout.writeDouble(val);    }    /**     * Writes a String as a sequence of bytes.     *     * @param	str the String of bytes to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeBytes(String str) throws IOException {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?