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; } }
/** * Writes this object to the stream passed. The object can be loaded again via its readObject * method. * * @param os strream to write data to. * @exception Exception if error of any kind occurs */ public synchronized void writeObject(OutputStream os) throws Exception { Enumeration keys = keys(); Enumeration values = elements(); PDataStream.writeInt(size(), os); // dictionary is never empty: // it either has elements or is null while (keys.hasMoreElements()) { PDataStream.writeUTF((String) keys.nextElement(), os); writeValue(values.nextElement(), os); } }
/** * 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; } }
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); }
// only if this is an object (not primitive) and non null! private static void writeRealObject(Object value, Class vClass, OutputStream os) throws IOException { try { if (vClass.equals(String.class)) { os.write(0); PDataStream.writeUTF((String) value, os); } else if (vClass.equals(Integer.class)) { os.write(1); PDataStream.writeInt(((Integer) value).intValue(), os); } else if (vClass.equals(Long.class)) { os.write(2); PDataStream.writeLong(((Long) value).longValue(), os); } else if (vClass.equals(Byte.class)) { os.write(3); os.write(((Byte) value).byteValue()); } else if (vClass.equals(Boolean.class)) { os.write(4); PDataStream.writeBoolean(((Boolean) value).booleanValue(), os); } else if (vClass.equals(Character.class)) { os.write(5); PDataStream.writeChar(((Character) value).charValue(), os); } else if (vClass.equals(Short.class)) { os.write(6); PDataStream.writeShort(((Short) value).shortValue(), os); } else if (vClass.equals(Float.class)) { os.write(7); PDataStream.writeFloat(((Float) value).floatValue(), os); } else if (vClass.equals(Double.class)) { os.write(8); PDataStream.writeDouble(((Double) value).doubleValue(), os); } else if (Externalizable.class.isAssignableFrom(vClass)) { os.write(11); String name = vClass.getName(); PDataStream.writeUTF(name, os); Externalizable tmp = (Externalizable) value; tmp.writeObject(os); } else { os.write(12); ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(value); } } catch (Exception exc) { throw new IOException(exc.toString()); } }
private static void writeValue(Object value, OutputStream os) throws IOException { if (value == null) { os.write(-1); } else { Class valueClass = value.getClass(); // write 0 for a single value, 1 for array, and 2 for Vector if (valueClass.isArray()) { os.write(1); int length = Array.getLength(value); PDataStream.writeInt(length, os); Class componentType = valueClass.getComponentType(); if (componentType.isPrimitive()) { // primitive PDataStream.writeBoolean(true, os); // 1: int;2: long; 3: byte; 4: boolean; 5: character; 6: // short; 7: float; 8: double writePrimitiveArray(componentType, value, length, os); } else { PDataStream.writeBoolean(false, os); PDataStream.writeUTF(componentType.getName(), os); Object[] oArr = (Object[]) value; for (int i = 0; i < length; i++) { writeValue(oArr[i], os); } } } else if (valueClass.equals(Vector.class)) { os.write(2); int size = ((Vector) value).size(); PDataStream.writeInt(size, os); for (int i = 0; i < size; i++) { writeValue(((Vector) value).elementAt(i), os); } } else { os.write(0); writeRealObject(value, valueClass, os); } } }
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); } }
// 1: int;2: long; 3: byte; 4: boolean; 5: character; // 6: short; 7: float; 8: double private static void writePrimitiveArray( Class componentType, Object array, int length, OutputStream os) throws IOException { if (componentType.equals(Integer.TYPE)) { int[] ints = (int[]) array; os.write(1); for (int i = 0; i < length; i++) { PDataStream.writeInt(ints[i], os); } } else if (componentType.equals(Long.TYPE)) { os.write(2); long[] longs = (long[]) array; for (int i = 0; i < length; i++) { PDataStream.writeLong(longs[i], os); } } else if (componentType.equals(Byte.TYPE)) { os.write(3); os.write((byte[]) array); } else if (componentType.equals(Boolean.TYPE)) { os.write(4); boolean[] booleans = (boolean[]) array; for (int i = 0; i < length; i++) { PDataStream.writeBoolean(booleans[i], os); } } else if (componentType.equals(Character.TYPE)) { os.write(5); char[] chars = (char[]) array; for (int i = 0; i < length; i++) { PDataStream.writeChar(chars[i], os); } } else if (componentType.equals(Short.TYPE)) { os.write(6); short[] shorts = (short[]) array; for (int i = 0; i < length; i++) { PDataStream.writeShort(shorts[i], os); } } else if (componentType.equals(Float.TYPE)) { os.write(7); float[] floats = (float[]) array; for (int i = 0; i < length; i++) { PDataStream.writeFloat(floats[i], os); } } else if (componentType.equals(Double.TYPE)) { os.write(8); double[] doubles = (double[]) array; for (int i = 0; i < length; i++) { PDataStream.writeDouble(doubles[i], os); } } else { throw new IllegalArgumentException("Unsupported Primitive Type: " + componentType); } }