private static Object readValue(InputStream is, ClassLoader loader) throws Exception { byte type = (byte) is.read(); if (type == -1) { return null; } Class vClass = null; if (type == 2) { int length = PDataStream.readInt(is); Vector v = new Vector(length); for (int i = 0; i < length; i++) { v.insertElementAt(readValue(is, loader), i); } return v; } else if (type == 0) { return readRealObject((byte) is.read(), is, loader); } else { int length = PDataStream.readInt(is); boolean primitive = PDataStream.readBoolean(is); if (primitive) { return readPrimitiveArray(length, is); } vClass = loader == null ? Class.forName(PDataStream.readUTF(is)) : loader.loadClass(PDataStream.readUTF(is)); Object array = Array.newInstance(vClass, length); for (int i = 0; i < length; i++) { Array.set(array, i, readValue(is, loader)); } return array; } }
private static Object readRealObject(byte type, InputStream is, ClassLoader loader) throws IOException { try { if (type == 0) { return PDataStream.readUTF(is); } else if (type == 1) { return new Integer(PDataStream.readInt(is)); } else if (type == 2) { return new Long(PDataStream.readLong(is)); } else if (type == 3) { return new Byte((byte) is.read()); } else if (type == 4) { return PDataStream.readBoolean(is) ? Boolean.TRUE : Boolean.FALSE; } else if (type == 5) { return new Character(PDataStream.readChar(is)); } else if (type == 6) { return new Short(PDataStream.readShort(is)); } else if (type == 7) { return new Float(PDataStream.readFloat(is)); } else if (type == 8) { return new Double(PDataStream.readDouble(is)); } else if (type == 11) { String name = PDataStream.readUTF(is); Class c = loader == null ? Class.forName(name) : loader.loadClass(name); if (Externalizable.class.isAssignableFrom(c)) { Externalizable obj = (Externalizable) c.newInstance(); obj.readObject(is); return obj; } throw new IOException("Could not read object " + name); } else if (type == 12) { ObjectInputStream in = loader == null ? new ObjectInputStream(is) : (ObjectInputStream) new XObjectInputStream(loader, is); return in.readObject(); } } catch (ClassNotFoundException cnfe) { throw new IOException("Could not find class " + cnfe.toString()); } catch (Exception exc) { throw exc instanceof IOException ? (IOException) exc : new IOException("Could not read object " + exc.toString()); } throw new IllegalArgumentException("Unsupported Typed Object: " + type); }
/** * Reads the data from the InputStream and loads the data in the table. * * @param is stream to read dictionary's data from * @exception Exception if an error of any kind occurs while reading */ public synchronized void readObject(InputStream is) throws Exception { try { int size = PDataStream.readInt(is); if (table == null) { if (size > 0) { initTable(size); } else initTable(MIN_CAPACITY); } for (int i = 0; i < size; i++) { put(PDataStream.readUTF(is), readValue(is, loader)); } } catch (Exception e) { throw e; } }