objectoutputstream.java

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

JAVA
1,976
字号
	bout.writeBytes(str);    }    /**     * Writes a String as a sequence of chars.     *     * @param	str the String of chars to be written     * @throws	IOException if I/O errors occur while writing to the underlying     * 		stream     */    public void writeChars(String str) throws IOException {	bout.writeChars(str);    }    /**     * Primitive data write of this String in UTF format.     *     * Note that there is a significant difference between     * writing a String into the stream as primitive data or     * as an Object. A String instance written by writeObject     * is written into the stream as a String initially. Future     * writeObject() calls write references to the string into     * the stream.     *     * @param str the String in UTF format     * @throws IOException if I/O errors occur while writing to the underlying     * stream     */    public void writeUTF(String str) throws IOException {	bout.writeUTF(str);    }        /*     * Gets the values of the primitive fields of object obj.  fieldIDs is an     * array of field IDs (the primFieldsID field of the appropriate     * ObjectStreamClass) identifying which fields to get.  typecodes is an     * array of characters designating the primitive type of each field (e.g.,     * 'C' for char, 'Z' for boolean, etc.)  data is the byte buffer in which     * the primitive field values are written, in the order of their field IDs.     *      * For efficiency, this method does not check all of its arguments for     * safety.  Specifically, it assumes that obj's type is compatible with the     * given field IDs, and that the data array is long enough to contain all of     * the byte values that will be written to it.     */    static native void getPrimitiveFieldValues(Object obj, 	    long[] fieldIDs, char[] typecodes, byte[] data);        /*     * Gets the value of an object field of object obj.  fieldID is the field ID     * identifying which field to set (obtained from the objFieldsID array field     * of the appropriate ObjectStreamClass).     *      * For efficiency, this method does not check to make sure that obj's type     * is compatible with the given field ID.     */    static native Object getObjectFieldValue(Object obj, long fieldID);    /*************************************/    /**     * Provide programatic access to the persistent fields to be written     * to ObjectOutput.     *     * @since 1.2     */    public static abstract class PutField {	/**	 * Put the value of the named boolean field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, boolean val);	/**	 * Put the value of the named byte field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, byte val);	/**	 * Put the value of the named char field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, char val);	/**	 * Put the value of the named short field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, short val);	/**	 * Put the value of the named int field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, int val);	/**	 * Put the value of the named long field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, long val);	/**	 * Put the value of the named float field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, float val);	/**	 * Put the value of the named double field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, double val);	/**	 * Put the value of the named Object field into the persistent field.	 *	 * @param  name the name of the serializable field	 * @param  val the value to assign to the field	 */	public abstract void put(String name, Object val);	/**	 * Write the data and fields to the specified ObjectOutput stream.	 * 	 * @param out the stream to write the data and fields to	 * @throws IOException if I/O errors occur while writing to the	 * underlying stream	 */	public abstract void write(ObjectOutput out) throws IOException;    }    /**     * Returns protocol version in use.     */    int getProtocolVersion() {	return protocol;    }    /**     * Writes string without allowing it to be replaced in stream.  Used by     * ObjectStreamClass to write class descriptor type strings.     */    void writeTypeString(String str) throws IOException {	int handle;	if (str == null) {	    writeNull();	} else if ((handle = handles.lookup(str)) != -1) {	    writeHandle(handle);	} else {	    writeString(str, false);	}    }    /**     * Verifies that this (possibly subclass) instance can be constructed     * without violating security constraints: the subclass must not override     * security-sensitive non-final methods, or else the     * "enableSubclassImplementation" SerializablePermission is checked.     */    private void verifySubclass() {        Class cl = getClass();        synchronized (subclassAudits) {            Boolean result = (Boolean) subclassAudits.get(cl);            if (result == null) {                 //                  // Note: only new Boolean instances (i.e., not Boolean.TRUE or                 // Boolean.FALSE) must be used as cache values, otherwise cache                 // entry will pin associated class.                 //                result = new Boolean(auditSubclass(cl));                subclassAudits.put(cl, result);            }            if (result.booleanValue()) {                return;            }        }        SecurityManager sm = System.getSecurityManager();        if (sm != null) {            sm.checkPermission(ObjectStreamConstants.SUBCLASS_IMPLEMENTATION_PERMISSION);        }    }    /**     * Performs reflective checks on given subclass to verify that it doesn't     * override security-sensitive non-final methods.  Returns true if subclass     * is "safe", false otherwise.     */    private static boolean auditSubclass(final Class subcl) {        Boolean result = (Boolean) AccessController.doPrivileged(            new PrivilegedAction() {                public Object run() {                    for (Class cl = subcl;                         cl != ObjectOutputStream.class;                         cl = cl.getSuperclass())                    {                        try {                            cl.getDeclaredMethod(                                "writeUnshared", new Class[] { Object.class });                            return Boolean.FALSE;                        } catch (NoSuchMethodException ex) {                        }                        try {                            cl.getDeclaredMethod("putFields", new Class[0]);                            return Boolean.FALSE;                        } catch (NoSuchMethodException ex) {                        }                    }                    return Boolean.TRUE;                }            }        );        return result.booleanValue();    }    /**     * Clears internal data structures.     */    private void clear() {	subs.clear();	handles.clear();    }    /**     * Underlying writeObject/writeUnshared implementation.     */    private void writeObject0(Object obj, boolean unshared) 	throws IOException     {	boolean oldMode = bout.setBlockDataMode(false);	depth++;	try {	    // handle previously written and non-replaceable objects	    int h;	    if ((obj = subs.lookup(obj)) == null) {		writeNull();		return;	    } else if (!unshared && (h = handles.lookup(obj)) != -1) {		writeHandle(h);		return;	    } else if (obj instanceof Class) {		writeClass((Class) obj, unshared);		return;	    } else if (obj instanceof ObjectStreamClass) {		writeClassDesc((ObjectStreamClass) obj, unshared);		return;	    }	    	    // check for replacement object	    Object orig = obj;	    Class cl = obj.getClass();	    ObjectStreamClass desc;	    for (;;) {		// TODO: skip this check for strings/arrays?		Class repCl;		desc = ObjectStreamClass.lookup(cl, true);		if (!desc.hasWriteReplaceMethod() ||		    (obj = desc.invokeWriteReplace(obj)) == null ||		    (repCl = obj.getClass()) == cl)		{		    break;		}		cl = repCl;	    }	    if (enableReplace) {		Object rep = replaceObject(obj);		if (rep != obj && rep != null) {		    cl = rep.getClass();		    desc = ObjectStreamClass.lookup(cl, true);		}		obj = rep;	    }	    // if object replaced, run through original checks a second time	    if (obj != orig) {		subs.assign(orig, obj);		if (obj == null) {		    writeNull();		    return;		} else if (!unshared && (h = handles.lookup(obj)) != -1) {		    writeHandle(h);		    return;		} else if (obj instanceof Class) {		    writeClass((Class) obj, unshared);		    return;		} else if (obj instanceof ObjectStreamClass) {		    writeClassDesc((ObjectStreamClass) obj, unshared);		    return;		}	    }	    	    // remaining cases	    if (obj instanceof String) {		writeString((String) obj, unshared);	    } else if (cl.isArray()) {		writeArray(obj, desc, unshared);	    } else if (obj instanceof Serializable) {		writeOrdinaryObject(obj, desc, unshared);	    } else {		throw new NotSerializableException(cl.getName());	    }	} finally {	    depth--;	    bout.setBlockDataMode(oldMode);	}    }        /**     * Writes null code to stream.     */    private void writeNull() throws IOException {	bout.writeByte(TC_NULL);    }        /**     * Writes given object handle to stream.     */    private void writeHandle(int handle) throws IOException {	bout.writeByte(TC_REFERENCE);	bout.writeInt(baseWireHandle + handle);    }        /**     * Writes representation of given class to stream.     */    private void writeClass(Class cl, boolean unshared) throws IOException {	bout.writeByte(TC_CLASS);	writeClassDesc(ObjectStreamClass.lookup(cl, true), false);	handles.assign(unshared ? null : cl);    }        /**     * Writes representation of given class descriptor to stream.     */    private void writeClassDesc(ObjectStreamClass desc, boolean unshared) 	throws IOException     {	int handle;	if (desc == null) {	    writeNull();	} else if (!unshared && (handle = handles.lookup(desc)) != -1) {	    writeHandle(handle);	} else if (desc.isProxy()) {	    writeProxyDesc(desc, unshared);	} else {	    writeNonProxyDesc(desc, unshared);	}    }        /**     * Writes class descriptor representing a dynamic proxy class to stream.     */    private void writeProxyDesc(ObjectStreamClass desc, boolean unshared) 	throws IOException     {	bout.writeByte(TC_PROXYCLASSDESC);	handles.assign(unshared ? null : desc);	Class cl = desc.forClass();	Class[] ifaces = cl.getInterfaces();	bout.writeInt(ifaces.length);	for (int i = 0; i < ifaces.length; i++) {	    bout.writeUTF(ifaces[i].getName());	}		bout.setBlockDataMode(true);	annotateProxyClass(cl);	bout.setBlockDataMode(false);	bout.writeByte(TC_ENDBLOCKDATA);		writeClassDesc(desc.getSuperDesc(), false);    }        /**     * Writes class descriptor representing a standard (i.e., not a dynamic     * proxy) class to stream.     */

⌨️ 快捷键说明

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