📄 xmlobjectoutputstream.java
字号:
* Method which writes a byte to the underlying output stream. Simply calls * writeByte(b) * * @param b DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void write(byte b) throws IOException { writeByte(b); } /** * Method which writes a array of bytes to the underlying output stream. * Simply calls writeByte(b[x]) on each of the array elements. * * @param b DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void write(byte[] b) throws IOException { write(b, 0, b.length); } /** * Method which writes a array of bytes to the underlying output stream. * Simply calls writeByte(b[x]) on each of the array elements. * * @param b DESCRIBE THE PARAMETER * @param offset DESCRIBE THE PARAMETER * @param length DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void write(byte[] b, int offset, int length) throws IOException { writer.start("base64"); writer.attribute("length", length); writer.writeBase64(b, offset, length); writer.end("base64"); } /** * Method which writes an int to the stream. * * @param i The value to write to the stream * @throws IOException If an error occurs */ public void writeInt(int i) throws IOException { writePrimitive(i, null); } /** * Method which writes an boolean to the stream. * * @param b The value to write to the stream * @throws IOException If an error occurs */ public void writeBoolean(boolean b) throws IOException { writePrimitive(b, null); } /** * Method which writes an byte to the stream. * * @param i The value to write to the stream, casted to a byte * @throws IOException If an error occurs */ public void writeByte(int i) throws IOException { writeByte((byte) i); } /** * Method which writes an byte to the stream. * * @param b The value to write to the stream * @throws IOException If an error occurs */ public void writeByte(byte b) throws IOException { writePrimitive(b, null); } /** * Method which writes an char to the stream. * * @param i The value to write to the stream, casted to an int * @throws IOException If an error occurs */ public void writeChar(int i) throws IOException { writeChar((char) i); } /** * Method which writes an char to the stream. * * @param c The value to write to the stream * @throws IOException If an error occurs */ public void writeChar(char c) throws IOException { writePrimitive(c, null); } /** * Method which writes an double to the stream. * * @param d The value to write to the stream * @throws IOException If an error occurs */ public void writeDouble(double d) throws IOException { writePrimitive(d, null); } /** * Method which writes an float to the stream. * * @param f The value to write to the stream * @throws IOException If an error occurs */ public void writeFloat(float f) throws IOException { writePrimitive(f, null); } /** * Method which writes an long to the stream. * * @param l The value to write to the stream * @throws IOException If an error occurs */ public void writeLong(long l) throws IOException { writePrimitive(l, null); } /** * Method which writes an short to the stream. * * @param i The value to write to the stream, casted to a short * @throws IOException If an error occurs */ public void writeShort(int i) throws IOException { writeShort((short) i); } /** * Method which writes an short to the stream. * * @param s The value to write to the stream * @throws IOException If an error occurs */ public void writeShort(short s) throws IOException { writePrimitive(s, null); } /** * Method which writes a UTF-encoded String to the stream. This method simply * calls writeObject(), as all strings are UTF encoded in XML. * * @param s DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void writeUTF(String s) throws IOException { writeObject(s); } /** * Method which writes a String as a sequence of chars to the stream. This * method is equivalent to calling s.toCharArray() and calling writeChar() on * each element. Using this method is *NOT* recommended, as it is an * increadible waste of space. * * @param s DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void writeChars(String s) throws IOException { char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { writeChar(chars[i]); } } /** * Method which writes a String as a sequence of chars to the stream. This * method is equivalent to calling s.toCharArray() and calling writeChar() on * each element. Using this method is *NOT* recommended, as it is an * increadible waste of space. * * @param s DESCRIBE THE PARAMETER * @throws IOException If an error occurs */ public void writeBytes(String s) throws IOException { byte[] bytes = s.getBytes(); for (int i = 0; i < bytes.length; i++) { writeByte(bytes[i]); } } /** * Method which is called by ObjectOutputStream.writeObject(), and writes the * given object to the stream. If the object (or any of it's descendents) has * a writeReplace() method, that object is written instead of the original * object. * * @param o The value to write to the stream * @throws IOException If an error occurs */ public void writeObjectOverride(Object o) throws IOException { writeObject(o, null); } /** * Method which writes the given object to the stream and does not record a * reference to the object. This guarantees that in future writes of this * object will not reference this copy of the object. * * @param o The value to write to the stream * @throws IOException If an error occurs */ public void writeUnshared(Object o) throws IOException { writeObjectUnshared(o, null, false); } /** * Method which can be called by objects if they have a writeObject() method. * This method initiates the default field serialization mechanism, and writes * all of the object's fields to the stream. If this method is called, the * putFields() method CANNOT be called by the same object. However, one of * these two methods MUST be called in the context of a writeObject(). * * @throws IOException If an error occurs * @throws NotActiveException If a object is not currently being written */ public void defaultWriteObject() throws IOException { if (currentObjects.peek() != null) { writeFields(currentObjects.peek(), (Class) currentClasses.peek()); } else { throw new NotActiveException(); } } /** * Method which can be called by objects if they have a writeObject() method. * This method returns an object which the callee can then use to specify * exactly which fields and values should be written to the steam, and can * specify fields which do not exist as a member of the object. If this method * is called, the defaultWriteObject() method CANNOT be called by the same * object. However, one of these two methods MUST be called in the context of * a writeObject(). * * @return A PutField, a container for fields to be written * @throws IOException If an error occurs * @throws NotActiveException If a object is not currently being read */ public ObjectOutputStream.PutField putFields() throws IOException { if (currentPutFields.peek() != null) { return (PutField) currentPutFields.peek(); } else { throw new NotActiveException(); } } /** * Method which writes the current state of the PutField object to the stream * as this object's fields. If this method is called, the defaultWriteObject() * method CANNOT be called by the same object. However, one of these two * methods MUST be called in the context of a writeObject(). * * @throws IOException If an error occurs * @throws NotActiveException If a object is not currently being read */ public void writeFields() throws IOException { if (currentObjects.peek() != null) { writePutFields((PutField) putFields()); } else { throw new NotActiveException(); } } // ----- ObjectOutputStream Referencing Methods ----- /** * Method which determines a unique hash value for each object. This * implementation uses the System.identityHashCode() method, which returns the * memory address. * * @param o DESCRIBE THE PARAMETER * @return An integer, representing a unqiue hash value */ protected int hash(Object o) { return System.identityHashCode(o) & 0x7FFFFFFF; } /** * Method which adds a reference in the hashtable of references. Multiple * calls to this method will replace prior objects. * * @param o The object to reference * @param reference The reference name to use */ protected void putReference(Object o, String reference) { Reference ref = new Reference(o); if (references.get(ref) == null) { references.put(ref, reference); } else { System.out.println("SERIOUS ERROR: Attempt to re-store reference: " + "EXISTING: " + ((Reference) references.get(ref)).object + " " + references.get(ref).hashCode() + "NEW: " + o + " " + o.hashCode()); } } /** * Method which assigns a new unique reference. * * @return A new unique reference */ protected String assignReference() { return "i" + (next++); } // ----- ObjectOutputStream WriteX Methods ----- /** * Method which writes a reset command to the stream. * * @throws IOException If an error occurs */ protected void writeReset() throws IOException { writer.start("reset"); writer.end("reset"); } /** * Method which writes an object to the stream as the given field name If the * next object represents a reference or null, then the appropriate helper is * called. Otherwise, writeObjectUnshared() is called to process the 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 * @throws ClassNotFoundException If the class cannot be found */ protected void writeObject(Object o, String field) throws IOException { if (o == null) { writeNull(field); } else if (getReference(o) != null) { writeReference(o, field); } else { writeObjectUnshared(o, field, true); } } /** * Method which writes an object to the stream. This method 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 write objects of type String, Array, or * Serializable. This method first calls the object's writeReplace() method, * if such a method exists, and if a different object is returned, that object * is written instead. * * @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 writeObjectUnshared(Object o, String field, boolean shared) throws IOException { Method replace = getWriteReplace(o.getClass()); if (replace != null) { try { o = replace.invoke(o, new Object[0]); if (o == null) { writeNull(field); return; } else if (getReference(o) != null) { writeReference(o, field); return; } } catch (IllegalAccessException e) { throw new IOException("IllegalAccessException thrown! " + e); } catch (InvocationTargetException e) { throw new IOException("InvocationTargetException thrown! " + e.getTargetException()); } } if (shared) { putReference(o, assignReference()); } if (o instanceof String) { writeString((String) o, field, shared); } else if (o.getClass().isArray()) { writeArray(o, field, shared); } else { writeOrdinaryObject(o, field, shared); } } /** * Method which writes a null item to the stream as the provided field. * * @param field The field name to write the object as * @throws IOException If an error occurs */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -