objectinputstream.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,784 行 · 第 1/5 页
JAVA
1,784 行
ClassNotFoundException ex = handles.lookupException(passHandle); if (ex != null) { throw ex; } if (depth == 0) { vlist.doCallbacks(); } return obj; } finally { passHandle = outerHandle; if (closed && depth == 0) { clear(); } } } /** * This method is called by trusted subclasses of ObjectOutputStream * that constructed ObjectOutputStream using the * protected no-arg constructor. The subclass is expected to provide * an override method with the modifier "final". * * @return the Object read from the stream. * @exception java.lang.ClassNotFoundException Class definition of a * serialized object cannot be found. * @exception OptionalDataException Primitive data was found in the * stream instead of objects. * @exception IOException if I/O errors occurred while reading from the * underlying stream * * @see #ObjectInputStream() * @see #readObject() * @since 1.2 */ protected Object readObjectOverride() throws OptionalDataException, ClassNotFoundException, IOException { return null; } /** * Reads an "unshared" object from the ObjectInputStream. This method is * identical to readObject, except that it prevents subsequent calls to * readObject and readUnshared from returning additional references to the * deserialized instance obtained via this call. Specifically: * <ul> * <li>If readUnshared is called to deserialize a back-reference (the * stream representation of an object which has been written * previously to the stream), an ObjectStreamException will be * thrown. * * <li>If readUnshared returns successfully, then any subsequent attempts * to deserialize back-references to the stream handle deserialized * by readUnshared will cause an ObjectStreamException to be thrown. * </ul> * Deserializing an object via readUnshared invalidates the stream handle * associated with the returned object. Note that this in itself does not * always guarantee that the reference returned by readUnshared is unique; * the deserialized object may define a readResolve method which returns an * object visible to other parties, or readUnshared may return a Class * object obtainable elsewhere in the stream or through external means. * * <p>However, for objects which are not instances of java.lang.Class and * do not define readResolve methods, readUnshared guarantees that the * returned object reference is unique and cannot be obtained a second time * from the ObjectInputStream that created it, even if the underlying data * stream has been manipulated. This guarantee applies only to the * base-level object returned by readUnshared, and not to any transitively * referenced sub-objects in the returned object graph. * * <p>ObjectInputStream subclasses which override this method can only be * constructed in security contexts possessing the * "enableSubclassImplementation" SerializablePermission; any attempt to * instantiate such a subclass without this permission will cause a * SecurityException to be thrown. * * @return reference to deserialized object * @throws ClassNotFoundException if class of an object to deserialize * cannot be found * @throws StreamCorruptedException if control information in the stream * is inconsistent * @throws ObjectStreamException if object to deserialize has already * appeared in stream * @throws OptionalDataException if primitive data is next in stream * @throws IOException if an I/O error occurs during deserialization * */ public Object readUnshared() throws IOException, ClassNotFoundException { // if nested read, passHandle contains handle of enclosing object int outerHandle = passHandle; try { Object obj = readObject0(true); handles.markDependency(outerHandle, passHandle); ClassNotFoundException ex = handles.lookupException(passHandle); if (ex != null) { throw ex; } if (depth == 0) { vlist.doCallbacks(); } return obj; } finally { passHandle = outerHandle; if (closed && depth == 0) { clear(); } } } /** * Read the non-static and non-transient fields of the current class from * this stream. This may only be called from the readObject method of the * class being deserialized. It will throw the NotActiveException if it is * called otherwise. * * @throws ClassNotFoundException if the class of a serialized object * could not be found. * @throws IOException if an I/O error occurs. * @throws NotActiveException if the stream is not currently reading * objects. */ public void defaultReadObject() throws IOException, ClassNotFoundException { if (curObj == null || curDesc == null) { throw new NotActiveException("not in call to readObject"); } bin.setBlockDataMode(false); defaultReadFields(curObj, curDesc); bin.setBlockDataMode(true); if (!curDesc.hasWriteObjectData()) { /* * Fix for 4360508: since stream does not contain terminating * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere * knows to simulate end-of-custom-data behavior. */ defaultDataEnd = true; } ClassNotFoundException ex = handles.lookupException(passHandle); if (ex != null) { throw ex; } } /** * Reads the persistent fields from the stream and makes them * available by name. * * @return the <code>GetField</code> object representing the persistent * fields of the object being deserialized * @exception java.lang.ClassNotFoundException if the class of a serialized * object could not be found. * @exception IOException if an I/O error occurs. * @exception NotActiveException if the stream is not currently reading * objects. * @since 1.2 */ public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException, NotActiveException { if (curGet == null) { if (curObj == null || curDesc == null) { throw new NotActiveException("not in call to readObject"); } curGet = new GetFieldImpl(curDesc); } bin.setBlockDataMode(false); curGet.readFields(); bin.setBlockDataMode(true); if (!curDesc.hasWriteObjectData()) { /* * Fix for 4360508: since stream does not contain terminating * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere * knows to simulate end-of-custom-data behavior. */ defaultDataEnd = true; } return curGet; } /** * Register an object to be validated before the graph is returned. While * similar to resolveObject these validations are called after the entire * graph has been reconstituted. Typically, a readObject method will * register the object with the stream so that when all of the objects are * restored a final set of validations can be performed. * * @param obj the object to receive the validation callback. * @param prio controls the order of callbacks;zero is a good default. * Use higher numbers to be called back earlier, lower numbers for * later callbacks. Within a priority, callbacks are processed in * no particular order. * @throws NotActiveException The stream is not currently reading objects * so it is invalid to register a callback. * @throws InvalidObjectException The validation object is null. */ public void registerValidation(ObjectInputValidation obj, int prio) throws NotActiveException, InvalidObjectException { if (depth == 0) { throw new NotActiveException("stream inactive"); } vlist.register(obj, prio); } /** * Load the local class equivalent of the specified stream class * description. Subclasses may implement this method to allow classes to * be fetched from an alternate source. * * <p>The corresponding method in <code>ObjectOutputStream</code> is * <code>annotateClass</code>. This method will be invoked only once for * each unique class in the stream. This method can be implemented by * subclasses to use an alternate loading mechanism but must return a * <code>Class</code> object. Once returned, the serialVersionUID of the * class is compared to the serialVersionUID of the serialized class. If * there is a mismatch, the deserialization fails and an exception is * raised. * * <p>By default the class name is resolved relative to the class that * called <code>readObject</code>. * * @param desc an instance of class <code>ObjectStreamClass</code> * @return a <code>Class</code> object corresponding to <code>desc</code> * @throws IOException any of the usual input/output exceptions * @throws ClassNotFoundException if class of a serialized object cannot * be found */ protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { String name = desc.getName(); try { return Class.forName(name, false, latestUserDefinedLoader()); } catch (ClassNotFoundException ex) { Class cl = (Class) primClasses.get(name); if (cl != null) { return cl; } else { throw ex; } } } /** * Returns a proxy class that implements the interfaces named in a * proxy class descriptor; subclasses may implement this method to * read custom data from the stream along with the descriptors for * dynamic proxy classes, allowing them to use an alternate loading * mechanism for the interfaces and the proxy class. * * <p>This method is called exactly once for each unique proxy class * descriptor in the stream. * * <p>The corresponding method in <code>ObjectOutputStream</code> is * <code>annotateProxyClass</code>. For a given subclass of * <code>ObjectInputStream</code> that overrides this method, the * <code>annotateProxyClass</code> method in the corresponding * subclass of <code>ObjectOutputStream</code> must write any data or * objects read by this method. * * <p>The default implementation of this method in * <code>ObjectInputStream</code> returns the result of calling * <code>Proxy.getProxyClass</code> with the list of * <code>Class</code> objects for the interfaces that are named in * the <code>interfaces</code> parameter. The <code>Class</code> * object for each interface name <code>i</code> is the value * returned by calling * <pre> * Class.forName(i, false, loader) * </pre> * where <code>loader</code> is that of the first non-null class * loader up the execution stack, or <code>null</code> if no non-null * class loaders are on the stack (the same class loader choice used * by the <code>resolveClass</code> method). This same value of * <code>loader</code> is also the class loader passed to * <code>Proxy.getProxyClass</code>. If <code>Proxy.getProxyClass</code> * throws an <code>IllegalArgumentException</code>, * <code>resolveProxyClass</code> will throw a * <code>ClassNotFoundException</code> containing the * <code>IllegalArgumentException</code>. * * @param interfaces the list of interface names that were * deserialized in the proxy class descriptor * @return a proxy class for the specified interfaces * @throws IOException any exception thrown by the underlying * <code>InputStream</code> * @throws ClassNotFoundException if the proxy class or any of the * named interfaces could not be found * @see ObjectOutputStream#annotateProxyClass(Class) * @since 1.3 */ protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException { ClassLoader latestLoader = latestUserDefinedLoader(); ClassLoader nonPublicLoader = null; boolean hasNonPublicInterface = false; // define proxy in class loader of non-public interface(s), if any Class[] classObjs = new Class[interfaces.length]; for (int i = 0; i < interfaces.length; i++) { Class cl = Class.forName(interfaces[i], false, latestLoader); if ((cl.getModifiers() & Modifier.PUBLIC) == 0) { if (hasNonPublicInterface) { if (nonPublicLoader != cl.getClassLoader()) { throw new IllegalAccessError( "conflicting non-public interface class loaders"); } } else { nonPublicLoader = cl.getClassLoader(); hasNonPublicInterface = true; } } classObjs[i] = cl; } try { return Proxy.getProxyClass( hasNonPublicInterface ? nonPublicLoader : latestLoader, classObjs); } catch (IllegalArgumentException e) { throw new ClassNotFoundException(null, e); } } /** * This method will allow trusted subclasses of ObjectInputStream to * substitute one object for another during deserialization. Replacing * objects is disabled until enableResolveObject is called. The * enableResolveObject method checks that the stream requesting to resolve * object can be trusted. Every reference to serializable objects is passed * to resolveObject. To insure that the private state of objects is not * unintentionally exposed only trusted streams may use resolveObject. * * <p>This method is called after an object has been read but before it is * returned from readObject. The default resolveObject method just returns * the same object. * * <p>When a subclass is replacing objects it must insure 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. * * @param obj object to be substituted * @return the substituted object * @throws IOException Any of the usual Input/Output exceptions. */ protected Object resolveObject(Object obj) throws IOException { return obj; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?