📄 xmlobjectinputstream.java
字号:
try { return (String) readObject(); } catch (ClassNotFoundException e) { throw new IOException("ReadUTF caused " + e); } } /** * Method which is called by ObjectInputStreamm.readObject(), and reads the * next object from the stream, and returns the result. If the object (or any * of it's descendents) has a readResolve() method, that object is returned. * * @return The value from the stream * @throws IOException If an error occurs * @throws ClassNotFoundException If a class in the stream cannot be found */ protected Object readObjectOverride() throws IOException, ClassNotFoundException { reader.readStartTag(); Object result = readObjectHelper(); if (depth == 0) { vlist.doCallbacks(); } return result; } /** * Method which reads the next object from the stream and does not record a * reference to the object. This guarantees that in future references to this * object in the stream will throw an IOException. * * @return The next object from the stream, without recording a reference * @throws IOException If an error occurs * @throws ClassNotFoundException If a class in the stream cannot be found */ public Object readUnshared() throws IOException, ClassNotFoundException { reader.readStartTag(); Object result = readUnsharedHelper(false); if (depth == 0) { vlist.doCallbacks(); } return result; } /** * Method which can be called by objects if they have a readObject() method. * This method initiates the default field deserialization mechanism, and sets * all of the object's fields from the stream. If this method is called, the * readFields() method CANNOT be called by the same object. However, one of * these two methods MUST be called in the context of a readObject(). * * @throws IOException If an error occurs * @throws ClassNotFoundException If a class in the stream cannot be found * @throws NotActiveException If a object is not currently being read */ public void defaultReadObject() throws IOException, ClassNotFoundException { if (currentObjects.peek() != null) { readFields(currentObjects.peek(), (Class) currentClasses.peek()); } else { throw new NotActiveException("defaultReadObject called with empty stack!"); } } /** * Method which can be called by objects if they have a readObject() method. * This method initiates the default field deserialization mechanism, and * reads all of the default fields in the stream. It does NOT set the field * values of the object, however, but instead returns the result to the Object * as a GetField object. It is explicitly the responsibility of the object to * set its fields appropriately. If this method is called, the * defaultReadObject() method CANNOT be called by the same object. However, * one of these two methods MUST be called in the context of a readObject(). * * @return A GetField, representing all of this object's fields * @throws IOException If an error occurs * @throws ClassNotFoundException If a class in the stream cannot be found * @throws NotActiveException If a object is not currently being read */ public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException { if (currentObjects.peek() != null) { return readGetFields(); } else { throw new NotActiveException("readFields called with empty stack!"); } } /** * 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 * @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 (currentObjects.peek() == null) { throw new NotActiveException("registerValidation called with empty stack!"); } vlist.register(obj, prio); } /** * Method which returns a new instance of the provided class. It does this by * getting the class's serializable constructor (described in * getSerializableConstructor()) and using it to create a new object. Thus, * all of the Serializable superclasses of the object must be initialized from * the stream. * * @param c The class to create a new instance of * @return DESCRIBE THE RETURN VALUE * @throws IOException If an error occurs */ protected Object newInstance(Class c) throws IOException { try { Constructor cons = (Constructor) CONSTRUCTORS.get(c); if (cons == null) { if (Externalizable.class.isAssignableFrom(c)) { cons = c.getDeclaredConstructor(new Class[0]); } else { cons = getSerializableConstructor(c); } cons.setAccessible(true); CONSTRUCTORS.put(c, cons); } return cons.newInstance(new Object[0]); } catch (InstantiationException e) { throw new IOException("Could not instanciate new class " + c); } catch (IllegalAccessException e) { throw new IOException("Could not instanciate new class " + c); } catch (InvocationTargetException e) { throw new IOException("Could not instanciate new class " + c); } catch (NoSuchMethodException e) { throw new IOException("Could not instanciate new class " + c); } } // ----- ObjectInputStreamm Referencing Methods ----- /** * Method which adds a reference in the hashtable of references. Multiple * calls to this method will replace prior objects. * * @param reference The reference name to use * @param o The object to reference */ protected void putReference(String reference, Object o) { references.put(reference, o); } // ----- Internal readX methods ----- /** * Method which reads a primitive value from the stream and returns the String * representation to the callee for processing. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @param type The type of primitive which should be next * @return The String representation of the primitive * @throws IOException If an error occurs */ protected String readPrimitive(String type) throws IOException { reader.assertStartTag("primitive"); reader.assertAttribute("type", type); String value = reader.getAttribute("value"); reader.readEndTag("primitive"); return value; } /** * Method which reads an int from the stream. This method assumes that the XML * reader is currently on a primitive tag, and does consume the end primitive * tag. * * @return The int read from the stream * @throws IOException If an error occurs */ protected int readIntHelper() throws IOException { return Integer.parseInt(readPrimitive("int")); } /** * Method which reads an boolean from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The boolean read from the stream * @throws IOException If an error occurs */ protected boolean readBooleanHelper() throws IOException { return readPrimitive("boolean").equals("true"); } /** * Method which reads an byte from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The byte read from the stream * @throws IOException If an error occurs */ protected byte readByteHelper() throws IOException { return Byte.parseByte(readPrimitive("byte")); } /** * Method which reads an char from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The char read from the stream * @throws IOException If an error occurs */ protected char readCharHelper() throws IOException { return readPrimitive("char").charAt(0); } /** * Method which reads an double from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The double read from the stream * @throws IOException If an error occurs */ protected double readDoubleHelper() throws IOException { return Double.parseDouble(readPrimitive("double")); } /** * Method which reads an float from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The float read from the stream * @throws IOException If an error occurs */ protected float readFloatHelper() throws IOException { return Float.parseFloat(readPrimitive("float")); } /** * Method which reads an long from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The long read from the stream * @throws IOException If an error occurs */ protected long readLongHelper() throws IOException { return Long.parseLong(readPrimitive("long")); } /** * Method which reads an short from the stream. This method assumes that the * XML reader is currently on a primitive tag, and does consume the end * primitive tag. * * @return The short read from the stream * @throws IOException If an error occurs */ protected short readShortHelper() throws IOException { return Short.parseShort(readPrimitive("short")); } /** * Method which reads an object from the stream. This method assumes that the * XML reader is currently on a start tag, and does consume the corresponding * end tag. If the next object represents a reference or null, then the * appropriate helper is called. Otherwise, readObjectUnshared() is called to * process the object. * * @return The object read from the stream * @throws IOException If an error occurs * @throws ClassNotFoundException If the class cannot be found */ protected Object readObjectHelper() throws IOException, ClassNotFoundException { reader.assertStartTag(); depth++; Object result = null; if (reader.getStartTag().equals("reference")) { result = readReference(); } else if (reader.getStartTag().equals("null")) { result = readNull(); } else { result = readUnsharedHelper(true); } depth--; return result; } /** * Method which reads an object from the stream. This method assumes that the * XML reader is currently on a start tag, and does consume the corresponding * end tag. This method also assumes that the type of object which is being * read is *sharable*, even if it is not going to be shared. Thus, this method * can read objects of type String, Array, or Serializable. * * @param shared Whether or not to record a reference to this object * @return The object read from the stream * @throws IOException If an error occurs * @throws ClassNotFoundException If the class cannot be found */ protected Object readUnsharedHelper(boolean shared) throws IOException, ClassNotFoundException { reader.assertStartTag(); if (reader.getStartTag().equals("string")) { return readString(shared); } else if (reader.getStartTag().equals("array")) { return readArray(shared); } else if (reader.getStartTag().equals("object")) { return readOrdinaryObject(shared); } else { throw new IOException("Unknown element name " + reader.getStartTag()); } } /** * Method which reads a reference off of the stream, and looks the reference * up in the references table. If not corresponding object is found, an * IOException is thrown. This method expected that a reference tag has just * been read, and it does consume the end reference tag. * * @return DESCRIBE THE RETURN VALUE * @throws IOException If an error occurs or if a reference is not found * @throws ClassNotFoundException If the class cannot be found */ protected Object readReference() throws IOException, ClassNotFoundException { reader.assertStartTag("reference"); Object result = getReference(reader.getAttribute("idref")); if (result == null) { throw new IOException("Invalid reference " + reader.getAttribute("idref") + " found."); } reader.readEndTag("reference"); return result; } /** * Method which reads a null item off of the stream. This method expected that * a null tag has just been read, and it does consume the end null tag. * * @return DESCRIBE THE RETURN VALUE * @throws IOException If an error occurs * @throws ClassNotFoundException If the class cannot be found */ protected Object readNull() throws IOException, ClassNotFoundException { reader.assertStartTag("null"); reader.readEndTag("null"); return null; } /** * Method which reads a string item off of the stream. This method expects * that a start string tag has just been read, an it does consume the end * start tag. If this string is to be shared, a reference is added to the * references tag. * * @param shared Whether or not to add this string to the references table * @return DESCRIBE THE RETURN VALUE * @throws IOException If an error occurs * @throws ClassNotFoundException If the class cannot be found */ protected Object readString(boolean shared) throws IOException, ClassNotFoundException { reader.assertStartTag("string"); String result = new String(reader.getAttribute("value")); if (shared && (reader.getAttribute("id") != null)) { putReference(reader.getAttribute("id"), result); } reader.readEndTag("string"); return result; } /** * Method which reads an ordinary object from the stream (not a String or * Array). This method assumes that the XML reader is currently on a start * tag, and does consume the corresponding end tag. This method first reads * the class type, and if it is Serializable, it constructs a new instance. * Then, if the object is shared and the id field is not null, it records a * reference to the object. If the object is Externalizable, the * readExternal() method is called and the object is returned. Otherwise, each * of the superclasses are read, from highest to lowest, using the readClass() * method. Lastly, if the object defines a readResolve() method, it is called * and the result is recorded and returned. * * @param shared Whether or not to record a reference to this object * @return The object read from the stream * @throws IOException If an error occurs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -