📄 xmlobjectoutputstream.java
字号:
protected void writeNull(String field) throws IOException { writer.start("null"); if (field != null) { writer.attribute("field", field); } writer.end("null"); } /** * Method which writes a string to the stream as the provided field. * * @param field The field name to write the object as * @param shared Whether or not to record a reference to this object * @param s DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ protected void writeString(String s, String field, boolean shared) throws IOException { writer.start("string"); if (field != null) { writer.attribute("field", field); } if (shared && getReference(s) != null) { writer.attribute("id", getReference(s)); } writer.attribute("value", s); writer.end("string"); } /** * Method which writes an array to the stream. This method writes the array * header, and then recursively writes all of the objects in the array to the * stream. If a non-serializable object is found, the object is replaced with * null. * * @param o The object to write to the stream * @param field The field name to write the object as * @param shared Whether or not to record a reference to this object * @throws IOException If an error occurs */ protected void writeArray(Object o, String field, boolean shared) throws IOException { writer.start("array"); if (field != null) { writer.attribute("field", field); } if (shared && getReference(o) != null) { writer.attribute("id", getReference(o)); } writer.attribute("base", o.getClass().getComponentType().getName()); writer.attribute("dim", getDimension(o.getClass())); writer.attribute("length", Array.getLength(o)); if (o.getClass().getComponentType().isPrimitive()) { Class c = o.getClass().getComponentType(); for (int i = 0; i < Array.getLength(o); i++) { if (c.equals(Integer.TYPE)) { writePrimitive(Array.getInt(o, i), null); } else if (c.equals(Boolean.TYPE)) { writePrimitive(Array.getBoolean(o, i), null); } else if (c.equals(Byte.TYPE)) { writePrimitive(Array.getByte(o, i), null); } else if (c.equals(Character.TYPE)) { writePrimitive(Array.getChar(o, i), null); } else if (c.equals(Double.TYPE)) { writePrimitive(Array.getDouble(o, i), null); } else if (c.equals(Float.TYPE)) { writePrimitive(Array.getFloat(o, i), null); } else if (c.equals(Long.TYPE)) { writePrimitive(Array.getLong(o, i), null); } else if (c == Short.TYPE) { writePrimitive(Array.getShort(o, i), null); } else { throw new IllegalArgumentException("Class " + c + " is not primitive!"); } } } else { for (int i = 0; i < Array.getLength(o); i++) { if (Array.get(o, i) instanceof Serializable) { writeObject(Array.get(o, i), null); } else { writeNull(null); } } } writer.end("array"); } /** * Method which writes a reference to the stream, determined from the * references table. This method throws an IOException if a reference is not * found to the provided object. * * @param o The object to write to the stream * @param field The field name to write the object as * @throws IOException If an error occurs */ protected void writeReference(Object o, String field) throws IOException { writer.start("reference"); if (field != null) { writer.attribute("field", field); } writer.attribute("idref", getReference(o)); writer.end("reference"); } /** * Method which writes an ordinary object to the stream (not a String or * Array). This method first writes the class type, and a list of the classes' * supertypes. If the object is Externalizable, the writeExternal() method is * called and the method returns. Otherwise, each of the superclasses are * written, from highest to lowest, using the writeClass() method. If the * object is not serializable, a NotSerializableException is thrown. * * @param o The object to write to the stream * @param field The field name to write the object as * @param shared Whether or not to record a reference to this object * @throws IOException If an error occurs * @throws NotSerializableException If the object is not serializable */ protected void writeOrdinaryObject(Object o, String field, boolean shared) throws IOException { if (!Serializable.class.isAssignableFrom(o.getClass())) { if (field == null) { throw new NotSerializableException(o.getClass().getName() + " " + field + " " + shared); } else { return; } } writer.start("object"); if (shared && getReference(o) != null) { writer.attribute("id", getReference(o)); } if (field != null) { writer.attribute("field", field); } writer.attribute("class", o.getClass().getName()); StringBuffer classes = new StringBuffer(); Class[] supers = getSuperClasses(o.getClass()); for (int i = 0; i < supers.length; i++) { classes.append(supers[i].getName()); classes.append(","); } writer.attribute("superclasses", classes.toString()); if (o instanceof Externalizable) { ((Externalizable) o).writeExternal(this); } else { for (int i = supers.length - 1; i >= 0; i--) { if (Serializable.class.isAssignableFrom(supers[i])) { writeClass(o, supers[i]); } } writeClass(o, o.getClass()); } writer.end("object"); } /** * Method which writes the information for one class for a given object to the * stream. This method first writes the class type, and if it defines a * writeObject() method, it is called. Otherwise, the fields are written in a * default manner. Any extra data not read in the stream is read and ignored. * * @param o The object to write to the stream * @param c The class of the object to write * @throws IOException If an error occurs */ protected void writeClass(Object o, Class c) throws IOException { writer.start("declaredClass"); writer.attribute("class", c.getName()); Method method = getWriteObject(c); if (method != null) { try { currentObjects.push(o); currentClasses.push(c); currentPutFields.push(new PutField()); method.invoke(o, new Object[]{this}); currentObjects.pop(); currentClasses.pop(); currentPutFields.pop(); } catch (IllegalAccessException e) { throw new IOException("IllegalAccessException thrown! " + e); } catch (InvocationTargetException e) { throw new IOException("InvocationTargetException thrown! " + e.getTargetException()); } } else { writeFields(o, c); } writer.end("declaredClass"); } /** * Method which initiates the default field writing mechanism for the given * object's class. This method only writes the non-static and non-transient * fields to the stream - all others are ignored. It reads all of the fields * using the writeField() method. * * @param o The object to write to the stream * @param c The class of the object to write * @throws IOException If an error occurs */ protected void writeFields(Object o, Class c) throws IOException { writer.start("default"); Field[] fields = getPersistentFields(c); for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); if (!(Modifier.isStatic(fields[i].getModifiers()) || Modifier.isTransient(fields[i].getModifiers()))) { if (fields[i].getType().isPrimitive()) { writePrimitiveField(o, fields[i]); } else { try { writeObject(fields[i].get(o), fields[i].getName()); } catch (IllegalAccessException e) { throw new IOException("IllegalAccessException thrown " + e); } } } } writer.end("default"); } /** * Method which writes out the data from the given PutField class as the data * for the given class. Each item in the PutField is written as the type it * was inserted as. Thus, fields can be written which do not actually exist in * the class. * * @param p The fields to write to the stream * @throws IOException If an error occurs */ protected void writePutFields(PutField p) throws IOException { writer.start("default"); String[] primitives = p.getPrimitives(); for (int i = 0; i < primitives.length; i++) { String name = primitives[i]; Object primitive = p.getPrimitive(name); if (primitive.getClass().equals(Integer.class)) { writePrimitive(p.getInt(name), name); } else if (primitive.getClass().equals(Boolean.class)) { writePrimitive(p.getBoolean(name), name); } else if (primitive.getClass().equals(Byte.class)) { writePrimitive(p.getByte(name), name); } else if (primitive.getClass().equals(Character.class)) { writePrimitive(p.getChar(name), name); } else if (primitive.getClass().equals(Double.class)) { writePrimitive(p.getDouble(name), name); } else if (primitive.getClass().equals(Float.class)) { writePrimitive(p.getFloat(name), name); } else if (primitive.getClass().equals(Long.class)) { writePrimitive(p.getLong(name), name); } else if (primitive.getClass().equals(Short.class)) { writePrimitive(p.getShort(name), name); } else { throw new IllegalArgumentException("Field " + name + " is not primitive!"); } } String[] objects = p.getObjects(); for (int i = 0; i < objects.length; i++) { writeObject(p.getObject(objects[i]), objects[i]); } writer.end("default"); } /** * Method which writes a primitive field to the stream. This method determiens * the type of field that needs to be written, and calls f.getX(o), wiritng it * to the stream. * * @param o The object get the primitive from * @param f The field representing the primitive about to be written * @throws IOException If an error occurs */ protected void writePrimitiveField(Object o, Field f) throws IOException { try { if (f.getType().equals(Integer.TYPE)) { writePrimitive(f.getInt(o), f.getName()); } else if (f.getType().equals(Boolean.TYPE)) { writePrimitive(f.getBoolean(o), f.getName()); } else if (f.getType().equals(Byte.TYPE)) { writePrimitive(f.getByte(o), f.getName()); } else if (f.getType().equals(Character.TYPE)) { writePrimitive(f.getChar(o), f.getName()); } else if (f.getType().equals(Double.TYPE)) { writePrimitive(f.getDouble(o), f.getName()); } else if (f.getType().equals(Float.TYPE)) { writePrimitive(f.getFloat(o), f.getName()); } else if (f.getType().equals(Long.TYPE)) { writePrimitive(f.getLong(o), f.getName()); } else if (f.getType().equals(Short.TYPE)) { writePrimitive(f.getShort(o), f.getName()); } else { throw new IllegalArgumentException("Field " + f + " is not primitive!"); } } catch (IllegalAccessException e) { throw new IOException("IllegalAccessException thrown " + e); } } /** * Method which writes a int to the stream, as the given field name. * * @param i The value to write to the stream * @param field The field name to use * @throws IOException If an error occurs */ protected void writePrimitive(int i, String field) throws IOException { writer.start("primitive"); if (field != null) { writer.attribute("field", field); } writer.attribute("type", "int"); writer.attribute("value", i); writer.end("primitive"); } /** * Method which writes a boolean to the stream, as the given field name. * * @param b The value to write to the stream * @param field The field name to use * @throws IOException If an error occurs */ protected void writePrimitive(boolean b, String field) throws IOException { writer.start("primitive"); if (field != null) { writer.attribute("field", field); } writer.attribute("type", "boolean"); writer.attribute("value", b); writer.end("primitive"); } /** * Method which writes a byte to the stream, as the given field name. * * @param b The value to write to the stream * @param field The field name to use * @throws IOException If an error occurs */ protected void writePrimitive(byte b, String field) throws IOException { writer.start("primitive"); if (field != null) { writer.attribute("field", field); } writer.attribute("type", "byte"); writer.attribute("value", b); writer.end("primitive"); } /** * Method which writes a char to the stream, as the given field name. * * @param c The value to write to the stream * @param field The field name to use * @throws IOException If an error occurs */ protected void writePrimitive(char c, String field) throws IOException { writer.start("primitive"); if (field != null) { writer.attribute("field", field);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -