⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlobjectinputstream.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util;import java.io.*;import java.lang.reflect.*;import java.security.*;import java.util.*;import sun.reflect.*;/** * XMLObjectInputStreamm is an extension of ObjectInputStreamm which provides * for deserialization for objects which have been converted to XML via a * XMLObjectOutputStream. This class supports all of the features of the * ObjectInputStreamm, including serialization support for any Java object graph * implementing the Serializable interface, support for Externalizable classes, * custom deserialization via the readObject() method, class evolution via the * readObjectNoData() method, alternate field reading mechanisms via the * readFields() method, support for the readUnshared method, and dynamic object * replacement via the readResolve() method. The format of the XML data to be * read in should conform to the JSX XML Schema, available online at * http://www.jsx.org/jsx.xsd. This class is designed to be able to read objects * serialized using JSX, however, this has not been fully tested and bugs may be * encountered. * * @version $Id: XMLObjectInputStream.java 3039 2006-02-07 12:20:12Z jstewart $ * @author Alan Mislove */public class XMLObjectInputStream extends ObjectInputStream {  /**   * The underlying reader, which parses the XML   */  protected XMLReader reader;  /**   * The hashtable of references, which is updated each time a new object is   * read off of the stream.   */  protected Hashtable references;  /**   * The stack of objects which are currently being read off of the stream   */  protected Stack currentObjects;  /**   * The stack of class types which are being read off of the stream   */  protected Stack currentClasses;  /**   * The ReflectionFactory, which allows for prividged construction of objects   */  protected ReflectionFactory reflFactory = (ReflectionFactory) AccessController.doPrivileged(new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());  /**   * The list of validation objects waiting for the entire object graph to be   * read in   */  protected ValidationList vlist;  /**   * The depth at which we are currently at in the object tree   */  protected int depth;  /**   * The hashmap of readResolve methods, mapping Class->Method   */  protected static SoftHashMap READ_RESOLVES = new SoftHashMap();  /**   * The hashmap of readObject methods, mapping Class->Method   */  protected static SoftHashMap READ_OBJECTS = new SoftHashMap();  /**   * A cache of constructors, mapping classes to serialization constructors   */  protected static SoftHashMap CONSTRUCTORS = new SoftHashMap();  /**   * Constructor which reads data from the given input stream in order   * deserialize objects. This constructor also reads the header from the   * stream, and throws an IOException if the correct header is not seen.   *   * @param in The input stream to read data from   * @exception IOException DESCRIBE THE EXCEPTION   * @throws IOException If the stream header is corrupt   */  public XMLObjectInputStream(InputStream in) throws IOException {    super();    this.reader = new XMLReader(new InputStreamReader(in));    this.currentObjects = new Stack();    this.currentClasses = new Stack();    this.references = new Hashtable();    this.vlist = new ValidationList();    this.depth = 0;    readStreamHeader();  }  /**   * Method which returns the Serializable constructor for the provided class.   * This Constructor is the no-arg Constructor for the first non-Serializable   * class in the class's superclass heirarchy. This method does not cache the   * result, but does set the constructor to be accessible, regardless of the   * Java security protection.   *   * @param c The class to fetch the constructor for   * @return The SerializableConstructor value   * @throws IOException If an error occurs   * @throws NoSuchMethodException If the first non-Serializable class does not   *      have a no-arg Constructor   */  protected Constructor getSerializableConstructor(Class c) throws IOException, NoSuchMethodException {    Class initCl = c;    while (Serializable.class.isAssignableFrom(initCl)) {      initCl = initCl.getSuperclass();    }    Constructor cons = initCl.getDeclaredConstructor(new Class[0]);    cons = reflFactory.newConstructorForSerialization(c, cons);    cons.setAccessible(true);    return cons;  }  /**   * Method which returns the class object for class names written to the   * stream. If the name represents a primitive, the the X.TYPE class is   * returned, otherwise, Class.forName() is used.   *   * @param name The name of the class to return   * @return The Class value   * @throws ClassNotFoundException If the class cannot be found   */  protected Class getClass(String name) throws ClassNotFoundException {    if (name.equals("int")) {      return Integer.TYPE;    } else if (name.equals("boolean")) {      return Boolean.TYPE;    } else if (name.equals("byte")) {      return Byte.TYPE;    } else if (name.equals("char")) {      return Character.TYPE;    } else if (name.equals("double")) {      return Double.TYPE;    } else if (name.equals("float")) {      return Float.TYPE;    } else if (name.equals("long")) {      return Long.TYPE;    } else if (name.equals("short")) {      return Short.TYPE;    } else {      return Class.forName(name);    }  }  /**   * Method which returns a previously stored reference. If the reference cannot   * be found, null is returned.   *   * @param reference The reference to use   * @return The referenced object, or null if none can be found   */  protected Object getReference(String reference) {    return references.get(reference);  }  // ----- ObjectInputStreamm Overriding Methods -----  /**   * Method which reads the XML header off of the stream. Usually, this is the   * <?xml version="1.0"?> tag, but it may include processing instructions.   *   * @throws IOException If the stream header is corrupt   */  protected void readStreamHeader() throws IOException {    reader.readHeader();  }  /**   * Method which closes the underlying input stream for reading. Any subsequent   * reads will throw an IOException.   *   * @throws IOException If an error occurs   */  public void close() throws IOException {    reader.close();  }  /**   * Method which resets the input stream, which removes the binding of all   * previously stored references.   *   * @throws IOException If an error occurs   */  public void reset() throws IOException {    references = new Hashtable();    vlist.clear();  }  /**   * Method which reads a byte from the underlying output stream. Simply calls   * readByte()   *   * @return DESCRIBE THE RETURN VALUE   * @throws IOException If an error occurs   */  public int read() throws IOException {    return readByte();  }  /**   * Method which reads a array of bytes from the underlying output stream.   * Simply calls b[x] = readByte() on each of the array elements.   *   * @param b DESCRIBE THE PARAMETER   * @param offset DESCRIBE THE PARAMETER   * @param length DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   * @throws IOException If an error occurs   */  public int read(byte[] b, int offset, int length) throws IOException {    reader.readStartTag("base64");    byte[] bytes = reader.readBase64();    int written = (length < bytes.length ? length : bytes.length);    System.arraycopy(bytes, 0, b, offset, written);    return written;  }  /**   * Method which reads a byte from the underlying output stream. Simply calls   * read()   *   * @param b DESCRIBE THE PARAMETER   * @throws IOException If an error occurs   */  public void readFully(byte[] b) throws IOException {    readFully(b, 0, b.length);  }  /**   * Method which reads a byte from the underlying output stream. Simply calls   * read()   *   * @param b DESCRIBE THE PARAMETER   * @param offset DESCRIBE THE PARAMETER   * @param length DESCRIBE THE PARAMETER   * @throws IOException If an error occurs   */  public void readFully(byte[] b, int offset, int length) throws IOException {    read(b, offset, length);  }  /**   * Method which reads an unsigned byte from the underlying output stream.   * Simply calls readByte()   *   * @return DESCRIBE THE RETURN VALUE   * @throws IOException If an error occurs   */  public int readUnsignedByte() throws IOException {    return readByte();  }  /**   * Method which reads an unsigned short from the underlying output stream.   * Simply calls readShort()   *   * @return DESCRIBE THE RETURN VALUE   * @throws IOException If an error occurs   */  public int readUnsignedShort() throws IOException {    return readShort();  }  /**   * Method which reads an int from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public int readInt() throws IOException {    reader.readStartTag("primitive");    return readIntHelper();  }  /**   * Method which reads a boolean from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public boolean readBoolean() throws IOException {    reader.readStartTag("primitive");    return readBooleanHelper();  }  /**   * Method which reads a byte from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public byte readByte() throws IOException {    reader.readStartTag("primitive");    return readByteHelper();  }  /**   * Method which reads a char from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public char readChar() throws IOException {    reader.readStartTag("primitive");    return readCharHelper();  }  /**   * Method which reads a double from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public double readDouble() throws IOException {    reader.readStartTag("primitive");    return readDoubleHelper();  }  /**   * Method which reads a float from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public float readFloat() throws IOException {    reader.readStartTag("primitive");    return readFloatHelper();  }  /**   * Method which reads a long from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public long readLong() throws IOException {    reader.readStartTag("primitive");    return readLongHelper();  }  /**   * Method which reads a short from the stream and returns the result.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public short readShort() throws IOException {    reader.readStartTag("primitive");    return readShortHelper();  }  /**   * Method which reads a UTF-encoded String from the stream and returns the   * result. This method simply calls readObject(), as all strings are UTF   * encoded in XML.   *   * @return The value from the stream   * @throws IOException If an error occurs   */  public String readUTF() throws IOException {

⌨️ 快捷键说明

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