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 readPrimitiveArray(int length, InputStream is) throws IOException { byte type = (byte) is.read(); if (type == 1) { int[] ints = new int[length]; for (int i = 0; i < length; i++) { ints[i] = PDataStream.readInt(is); } return ints; } else if (type == 2) { long[] longs = new long[length]; for (int i = 0; i < length; i++) { longs[i] = PDataStream.readLong(is); } return longs; } else if (type == 3) { byte[] bytes = new byte[length]; is.read(bytes); return bytes; } else if (type == 4) { boolean[] booleans = new boolean[length]; for (int i = 0; i < length; i++) { booleans[i] = PDataStream.readBoolean(is); } return booleans; } else if (type == 5) { char[] chars = new char[length]; for (int i = 0; i < length; i++) { chars[i] = PDataStream.readChar(is); } return chars; } else if (type == 6) { short[] shorts = new short[length]; for (int i = 0; i < length; i++) { shorts[i] = PDataStream.readShort(is); } return shorts; } else if (type == 7) { float[] floats = new float[length]; for (int i = 0; i < length; i++) { floats[i] = PDataStream.readFloat(is); } return floats; } else if (type == 8) { double[] doubles = new double[length]; for (int i = 0; i < length; i++) { doubles[i] = PDataStream.readDouble(is); } return doubles; } else { throw new IllegalArgumentException("Trying to read unsupported primitive type: " + type); } }
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); }