[jboss-svn-commits] JBL Code SVN: r17398 - labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Dec 25 18:11:22 EST 2007


Author: haruki_zaemon
Date: 2007-12-25 18:11:21 -0500 (Tue, 25 Dec 2007)
New Revision: 17398

Modified:
   labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
   labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectOutputStream.java
   labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectStreamConstants.java
Log:
Write a stream header with version number, etc.

Modified: labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
===================================================================
--- labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java	2007-12-25 21:49:38 UTC (rev 17397)
+++ labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java	2007-12-25 23:11:21 UTC (rev 17398)
@@ -30,14 +30,15 @@
     private InternalRuleBase ruleBase;
     private ClassFieldExtractorCache extractorFactory;
 
-    public DroolsObjectInputStream(InputStream stream) {
-        this(stream, Thread.currentThread().getContextClassLoader());
+    public DroolsObjectInputStream(InputStream in) throws IOException {
+        this(in, Thread.currentThread().getContextClassLoader());
     }
 
-    public DroolsObjectInputStream(InputStream stream, ClassLoader classLoader) {
-        super(stream);
+    public DroolsObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException {
+        super(in);
         this.classLoader = classLoader;
         this.extractorFactory = ClassFieldExtractorCache.getInstance();
+        readStreamHeader();
     }
 
     public ClassLoader getClassLoader() {
@@ -65,59 +66,59 @@
     }
 
     public Object readObject() throws ClassNotFoundException, IOException {
-        byte type = unmarshalRecordType();
+        byte type = readRecordType();
 
         Object object;
 
         switch (type) {
-            case NULL:
-                object = unmarshalNull();
+            case RT_NULL:
+                object = readNull();
                 break;
-            case EMPTY_SET:
-                object = unmarshalEmptySet();
+            case RT_EMPTY_SET:
+                object = readEmptySet();
                 break;
-            case EMPTY_LIST:
-                object = unmarshalEmptyList();
+            case RT_EMPTY_LIST:
+                object = readEmptyList();
                 break;
-            case EMPTY_MAP:
-                object = unmarshalEmptyMap();
+            case RT_EMPTY_MAP:
+                object = readEmptyMap();
                 break;
             default:
-                object = unmarshalObject(type);
+                object = readObject(type);
         }
 
         return object;
     }
 
-    private Object unmarshalObject(byte type) throws IOException, ClassNotFoundException {
-        int handle = unmarshalHandle();
+    private Object readObject(byte type) throws IOException, ClassNotFoundException {
+        int handle = readHandle();
 
         Object object;
 
         switch (type) {
-            case EXTERNALIZABLE:
-                object = unmarshalExternalizable();
+            case RT_EXTERNALIZABLE:
+                object = readExternalizable();
                 break;
-            case REPLACEABLE:
-                object = unmarshalResolvable();
+            case RT_REPLACEABLE:
+                object = readResolvable();
                 break;
-            case STRING:
-                object = unmarshalString();
+            case RT_STRING:
+                object = readString();
                 break;
-            case MAP:
-                object = unmarshalMap();
+            case RT_MAP:
+                object = readMap();
                 break;
-            case COLLECTION:
-                object = unmarshalCollection();
+            case RT_COLLECTION:
+                object = readCollection();
                 break;
-            case ARRAY:
-                object = unmarshalArray();
+            case RT_ARRAY:
+                object = readArray();
                 break;
-            case CLASS:
-                object = unmarshalClass();
+            case RT_CLASS:
+                object = readClass();
                 break;
-            case REFERENCE:
-                return unmarshalReference(handle);
+            case RT_REFERENCE:
+                return readReference(handle);
             default:
                 throw new StreamCorruptedException("Unsupported object type: " + type);
         }
@@ -126,115 +127,115 @@
         return object;
     }
 
-    private Object unmarshalArray() throws IOException, ClassNotFoundException {
+    private Object readArray() throws IOException, ClassNotFoundException {
         Class clazz = (Class) readObject();
         int length = readInt();
         Class componentType = clazz.getComponentType();
         Object array = Array.newInstance(componentType, length);
         if (componentType.isPrimitive()) {
-            unmarshalPrimitiveArray(array, length, componentType);
+            readPrimitiveArray(array, length, componentType);
         } else {
-            unmarshalObjectArray((Object[]) array, length);
+            readObjectArray((Object[]) array, length);
         }
         return array;
     }
 
-    private void unmarshalPrimitiveArray(Object array, int length, Class clazz) throws IOException {
+    private void readPrimitiveArray(Object array, int length, Class clazz) throws IOException {
         if (clazz == Integer.TYPE) {
-            unmarshalIntArray((int[]) array, length);
+            readIntArray((int[]) array, length);
         } else if (clazz == Byte.TYPE) {
-            unmarshalByteArray((byte[]) array, length);
+            readByteArray((byte[]) array, length);
         } else if (clazz == Long.TYPE) {
-            unmarshalLongArray((long[]) array, length);
+            readLongArray((long[]) array, length);
         } else if (clazz == Float.TYPE) {
-            unmarshalFloatArray((float[]) array, length);
+            readFloatArray((float[]) array, length);
         } else if (clazz == Double.TYPE) {
-            unmarshalDoubleArray((double[]) array, length);
+            readDoubleArray((double[]) array, length);
         } else if (clazz == Short.TYPE) {
-            unmarshalShortArray((short[]) array, length);
+            readShortArray((short[]) array, length);
         } else if (clazz == Character.TYPE) {
-            unmarshalCharArray((char[]) array, length);
+            readCharArray((char[]) array, length);
         } else if (clazz == Boolean.TYPE) {
-            unmarshalBooleanArray((boolean[]) array, length);
+            readBooleanArray((boolean[]) array, length);
         } else {
             throw new StreamCorruptedException("Unsupported array type: " + clazz);
         }
     }
 
-    private void unmarshalIntArray(int[] ints, int length) throws IOException {
+    private void readIntArray(int[] ints, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             ints[i] = readInt();
         }
     }
 
-    private void unmarshalByteArray(byte[] bytes, int length) throws IOException {
+    private void readByteArray(byte[] bytes, int length) throws IOException {
         readFully(bytes, 0, length);
     }
 
-    private void unmarshalLongArray(long[] longs, int length) throws IOException {
+    private void readLongArray(long[] longs, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             longs[i] = readLong();
         }
     }
 
-    private void unmarshalFloatArray(float[] floats, int length) throws IOException {
+    private void readFloatArray(float[] floats, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             floats[i] = readFloat();
         }
     }
 
-    private void unmarshalDoubleArray(double[] doubles, int length) throws IOException {
+    private void readDoubleArray(double[] doubles, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             doubles[i] = readDouble();
         }
     }
 
-    private void unmarshalShortArray(short[] shorts, int length) throws IOException {
+    private void readShortArray(short[] shorts, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             shorts[i] = readShort();
         }
     }
 
-    private void unmarshalCharArray(char[] chars, int length) throws IOException {
+    private void readCharArray(char[] chars, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             chars[i] = readChar();
         }
     }
 
-    private void unmarshalBooleanArray(boolean[] booleans, int length) throws IOException {
+    private void readBooleanArray(boolean[] booleans, int length) throws IOException {
         for (int i = 0; i < length; ++i) {
             booleans[i] = readBoolean();
         }
     }
 
-    private void unmarshalObjectArray(Object[] objects, int length) throws IOException, ClassNotFoundException {
+    private void readObjectArray(Object[] objects, int length) throws IOException, ClassNotFoundException {
         for (int i = 0; i < length; ++i) {
             objects[i] = readObject();
         }
     }
 
-    private Object unmarshalNull() {
+    private Object readNull() {
         return null;
     }
 
-    private Set unmarshalEmptySet() {
+    private Set readEmptySet() {
         return Collections.EMPTY_SET;
     }
 
-    private List unmarshalEmptyList() {
+    private List readEmptyList() {
         return Collections.EMPTY_LIST;
     }
 
-    private Map unmarshalEmptyMap() {
+    private Map readEmptyMap() {
         return Collections.EMPTY_MAP;
     }
 
-    private Object unmarshalResolvable() throws ClassNotFoundException, IOException {
-        Resolvable resolvable = (Resolvable) unmarshalExternalizable();
+    private Object readResolvable() throws ClassNotFoundException, IOException {
+        Resolvable resolvable = (Resolvable) readExternalizable();
         return resolvable.readResolve();
     }
 
-    private Externalizable unmarshalExternalizable() throws ClassNotFoundException, IOException {
+    private Externalizable readExternalizable() throws ClassNotFoundException, IOException {
         Class clazz = (Class) readObject();
         Externalizable externalizable = createExternalizable(clazz);
         externalizable.readExternal(this);
@@ -246,11 +247,11 @@
         return (Externalizable) instantiator.newInstance();
     }
 
-    private String unmarshalString() throws IOException {
+    private String readString() throws IOException {
         return readUTF();
     }
 
-    private Map unmarshalMap() throws IOException, ClassNotFoundException {
+    private Map readMap() throws IOException, ClassNotFoundException {
         Class clazz = (Class) readObject();
         int size = readInt();
         Map map = (Map) createCollection(clazz, size);
@@ -262,7 +263,7 @@
         return map;
     }
 
-    private Collection unmarshalCollection() throws IOException, ClassNotFoundException {
+    private Collection readCollection() throws IOException, ClassNotFoundException {
         Class clazz = (Class) readObject();
         int size = readInt();
         Collection collection = (Collection) createCollection(clazz, size);
@@ -287,23 +288,29 @@
         }
     }
 
-    private int unmarshalHandle() throws IOException {
+    private int readHandle() throws IOException {
         return readInt();
     }
 
-    private Class unmarshalClass() throws IOException, ClassNotFoundException {
+    private Class readClass() throws IOException, ClassNotFoundException {
         String className = (String) readObject();
         return this.classLoader.loadClass(className);
     }
 
-    private byte unmarshalRecordType() throws IOException {
+    private byte readRecordType() throws IOException {
         return readByte();
     }
 
-    private Object unmarshalReference(int handle) {
+    private Object readReference(int handle) {
         return objectsByHandle.get(handle);
     }
 
+    private void readStreamHeader() throws IOException {
+        if (readInt() != STREAM_MAGIC || readShort() != STREAM_VERSION) {
+            throw new StreamCorruptedException("Invalid stream header");
+        }
+    }
+
     private void registerObject(int handle, Object object) {
         objectsByHandle.put(handle, object);
     }

Modified: labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectOutputStream.java
===================================================================
--- labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectOutputStream.java	2007-12-25 21:49:38 UTC (rev 17397)
+++ labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectOutputStream.java	2007-12-25 23:11:21 UTC (rev 17398)
@@ -22,96 +22,97 @@
     private final Map handlesByObject = new IdentityHashMap();
     private int handleCount = 0;
 
-    public DroolsObjectOutputStream(OutputStream stream) {
+    public DroolsObjectOutputStream(OutputStream stream) throws IOException {
         super(stream);
+        writeStreamHeader();
     }
 
     public void writeObject(Object object) throws IOException {
         if (object == null) {
-            marshalNull();
+            writeNull();
         } else {
             Class clazz = object.getClass();
 
             if (clazz == EMPTY_SET_CLASS) {
-                marshalEmptySet();
+                writeEmptySet();
             } else if (clazz == EMPTY_LIST_CLASS) {
-                marshalEmptyList();
+                writeEmptyList();
             } else if (clazz == EMPTY_MAP_CLASS) {
-                marshalEmptyMap();
+                writeEmptyMap();
             } else if (clazz == String.class) {
-                marshalObjectOrReference(((String) object).intern(), clazz);
+                writeObjectOrReference(((String) object).intern(), clazz);
             } else {
-                marshalObjectOrReference(object, clazz);
+                writeObjectOrReference(object, clazz);
             }
         }
     }
 
-    private void marshalNull() throws IOException {
-        marshalRecordType(NULL);
+    private void writeNull() throws IOException {
+        writeRecordType(RT_NULL);
     }
 
-    private void marshalObjectOrReference(Object object, Class clazz) throws IOException {
+    private void writeObjectOrReference(Object object, Class clazz) throws IOException {
         int handle = registerObject(object);
         if (handle < 0) {
-            marshalObject(object, clazz, -handle);
+            writeObject(object, clazz, -handle);
         } else {
-            marshalReference(handle);
+            writeReference(handle);
         }
     }
 
-    private void marshalObject(Object object, Class clazz, int handle) throws IOException {
+    private void writeObject(Object object, Class clazz, int handle) throws IOException {
         if (Externalizable.class.isAssignableFrom(clazz)) {
-            marshalExternalizable((Externalizable) object, clazz, handle);
+            writeExternalizable((Externalizable) object, clazz, handle);
         } else if (clazz == String.class) {
-            marshalString((String) object, handle);
+            writeString((String) object, handle);
         } else if (Map.class.isAssignableFrom(clazz)) {
-            marshalMap((Map) object, clazz, handle);
+            writeMap((Map) object, clazz, handle);
         } else if (Collection.class.isAssignableFrom(clazz)) {
-            marshalCollection((Collection) object, clazz, handle);
+            writeCollection((Collection) object, clazz, handle);
         } else if (clazz == Class.class) {
-            marshalClass((Class) object, handle);
+            writeClass((Class) object, handle);
         } else if (clazz.isArray()) {
-            marshalArray(object, clazz, handle);
+            writeArray(object, clazz, handle);
         } else {
             throw new NotSerializableException("Unsupported class: " + clazz);
         }
     }
 
-    private void marshalArray(Object array, Class clazz, int handle) throws IOException {
-        marshalRecordType(ARRAY);
-        marshalHandle(handle);
+    private void writeArray(Object array, Class clazz, int handle) throws IOException {
+        writeRecordType(RT_ARRAY);
+        writeHandle(handle);
         writeObject(clazz);
         Class componentType = clazz.getComponentType();
         if (componentType.isPrimitive()) {
-            marshalPrimitiveArray(array, componentType);
+            writePrimitiveArray(array, componentType);
         } else {
-            marshalObjectArray((Object[]) array);
+            writeObjectArray((Object[]) array);
         }
     }
 
-    private void marshalPrimitiveArray(Object array, Class clazz) throws IOException {
+    private void writePrimitiveArray(Object array, Class clazz) throws IOException {
         if (clazz == Integer.TYPE) {
-            marshalIntArray(array);
+            writeIntArray(array);
         } else if (clazz == Byte.TYPE) {
-            marshalByteArray(array);
+            writeByteArray(array);
         } else if (clazz == Long.TYPE) {
-            marshalLongArray(array);
+            writeLongArray(array);
         } else if (clazz == Float.TYPE) {
-            marshalFloatArray(array);
+            writeFloatArray(array);
         } else if (clazz == Double.TYPE) {
-            marshalDoubleArray(array);
+            writeDoubleArray(array);
         } else if (clazz == Short.TYPE) {
-            marshalShortArray(array);
+            writeShortArray(array);
         } else if (clazz == Character.TYPE) {
-            marshalCharArray(array);
+            writeCharArray(array);
         } else if (clazz == Boolean.TYPE) {
-            marshalBooleanArray(array);
+            writeBooleanArray(array);
         } else {
             throw new NotSerializableException("Unsupported array type: " + clazz);
         }
     }
 
-    private void marshalIntArray(Object array) throws IOException {
+    private void writeIntArray(Object array) throws IOException {
         int[] ints = (int[]) array;
         int length = ints.length;
         writeInt(length);
@@ -120,14 +121,14 @@
         }
     }
 
-    private void marshalByteArray(Object array) throws IOException {
+    private void writeByteArray(Object array) throws IOException {
         byte[] bytes = (byte[]) array;
         int length = bytes.length;
         writeInt(length);
         write(bytes, 0, length);
     }
 
-    private void marshalLongArray(Object array) throws IOException {
+    private void writeLongArray(Object array) throws IOException {
         long[] longs = (long[]) array;
         int length = longs.length;
         writeInt(length);
@@ -136,7 +137,7 @@
         }
     }
 
-    private void marshalFloatArray(Object array) throws IOException {
+    private void writeFloatArray(Object array) throws IOException {
         float[] floats = (float[]) array;
         int length = floats.length;
         writeFloat(length);
@@ -145,7 +146,7 @@
         }
     }
 
-    private void marshalDoubleArray(Object array) throws IOException {
+    private void writeDoubleArray(Object array) throws IOException {
         double[] doubles = (double[]) array;
         int length = doubles.length;
         writeInt(length);
@@ -154,7 +155,7 @@
         }
     }
 
-    private void marshalShortArray(Object array) throws IOException {
+    private void writeShortArray(Object array) throws IOException {
         short[] shorts = (short[]) array;
         int length = shorts.length;
         writeInt(length);
@@ -163,7 +164,7 @@
         }
     }
 
-    private void marshalCharArray(Object array) throws IOException {
+    private void writeCharArray(Object array) throws IOException {
         char[] chars = (char[]) array;
         int length = chars.length;
         writeInt(length);
@@ -172,7 +173,7 @@
         }
     }
 
-    private void marshalBooleanArray(Object array) throws IOException {
+    private void writeBooleanArray(Object array) throws IOException {
         boolean[] booleans = (boolean[]) array;
         int length = booleans.length;
         writeInt(length);
@@ -181,7 +182,7 @@
         }
     }
 
-    private void marshalObjectArray(Object[] objects) throws IOException {
+    private void writeObjectArray(Object[] objects) throws IOException {
         int length = objects.length;
         writeInt(length);
         for (int i = 0; i < length; ++i) {
@@ -189,37 +190,37 @@
         }
     }
 
-    private void marshalClass(Class clazz, int handle) throws IOException {
-        marshalRecordType(CLASS);
-        marshalHandle(handle);
+    private void writeClass(Class clazz, int handle) throws IOException {
+        writeRecordType(RT_CLASS);
+        writeHandle(handle);
         writeObject(clazz.getName());
     }
 
-    private void marshalReference(int handle) throws IOException {
-        marshalRecordType(REFERENCE);
-        marshalHandle(handle);
+    private void writeReference(int handle) throws IOException {
+        writeRecordType(RT_REFERENCE);
+        writeHandle(handle);
     }
 
-    private void marshalString(String string, int handle) throws IOException {
-        marshalRecordType(STRING);
-        marshalHandle(handle);
+    private void writeString(String string, int handle) throws IOException {
+        writeRecordType(RT_STRING);
+        writeHandle(handle);
         writeUTF(string);
     }
 
-    private void marshalExternalizable(Externalizable marshallable, Class clazz, int handle) throws IOException {
+    private void writeExternalizable(Externalizable writelable, Class clazz, int handle) throws IOException {
         if (Resolvable.class.isAssignableFrom(clazz)) {
-            marshalRecordType(REPLACEABLE);
+            writeRecordType(RT_REPLACEABLE);
         } else {
-            marshalRecordType(EXTERNALIZABLE);
+            writeRecordType(RT_EXTERNALIZABLE);
         }
-        marshalHandle(handle);
+        writeHandle(handle);
         writeObject(clazz);
-        marshallable.writeExternal(this);
+        writelable.writeExternal(this);
     }
 
-    private void marshalMap(Map map, Class clazz, int handle) throws IOException {
-        marshalRecordType(MAP);
-        marshalHandle(handle);
+    private void writeMap(Map map, Class clazz, int handle) throws IOException {
+        writeRecordType(RT_MAP);
+        writeHandle(handle);
         writeObject(clazz);
         writeInt(map.size());
         for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
@@ -229,9 +230,9 @@
         }
     }
 
-    private void marshalCollection(Collection collection, Class clazz, int handle) throws IOException {
-        marshalRecordType(COLLECTION);
-        marshalHandle(handle);
+    private void writeCollection(Collection collection, Class clazz, int handle) throws IOException {
+        writeRecordType(RT_COLLECTION);
+        writeHandle(handle);
         writeObject(clazz);
         writeInt(collection.size());
         for (Iterator i = collection.iterator(); i.hasNext();) {
@@ -239,26 +240,31 @@
         }
     }
 
-    private void marshalEmptySet() throws IOException {
-        marshalRecordType(EMPTY_SET);
+    private void writeEmptySet() throws IOException {
+        writeRecordType(RT_EMPTY_SET);
     }
 
-    private void marshalEmptyList() throws IOException {
-        marshalRecordType(EMPTY_LIST);
+    private void writeEmptyList() throws IOException {
+        writeRecordType(RT_EMPTY_LIST);
     }
 
-    private void marshalEmptyMap() throws IOException {
-        marshalRecordType(EMPTY_MAP);
+    private void writeEmptyMap() throws IOException {
+        writeRecordType(RT_EMPTY_MAP);
     }
 
-    private void marshalRecordType(byte type) throws IOException {
+    private void writeRecordType(byte type) throws IOException {
         writeByte(type);
     }
 
-    private void marshalHandle(int handle) throws IOException {
+    private void writeHandle(int handle) throws IOException {
         writeInt(handle);
     }
 
+    private void writeStreamHeader() throws IOException {
+        writeInt(STREAM_MAGIC);
+        writeShort(STREAM_VERSION);
+    }
+
     private int registerObject(Object object) {
         Integer handle = (Integer) handlesByObject.get(object);
         if (handle == null) {

Modified: labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectStreamConstants.java
===================================================================
--- labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectStreamConstants.java	2007-12-25 21:49:38 UTC (rev 17397)
+++ labs/jbossrules/branches/serialization/drools-core/src/main/java/org/drools/common/DroolsObjectStreamConstants.java	2007-12-25 23:11:21 UTC (rev 17398)
@@ -1,19 +1,22 @@
 package org.drools.common;
 
 public final class DroolsObjectStreamConstants {
-    public static final byte CLASS = 11;
-    public static final byte EXTERNALIZABLE = 12;
-    public static final byte REFERENCE = 13;
-    public static final byte EMPTY_SET = 14;
-    public static final byte EMPTY_LIST = 15;
-    public static final byte EMPTY_MAP = 16;
-    public static final byte MAP = 17;
-    public static final byte ARRAY = 18;
-    public static final byte STRING = 19;
-    public static final byte NULL = 20;
-    public static final byte COLLECTION = 21;
-    public static final byte REPLACEABLE = 22;
+    public static final int STREAM_MAGIC = 0x0051002d;
+    public static final short STREAM_VERSION = 400;
 
+    public static final byte RT_CLASS = 11;
+    public static final byte RT_EXTERNALIZABLE = 12;
+    public static final byte RT_REFERENCE = 13;
+    public static final byte RT_EMPTY_SET = 14;
+    public static final byte RT_EMPTY_LIST = 15;
+    public static final byte RT_EMPTY_MAP = 16;
+    public static final byte RT_MAP = 17;
+    public static final byte RT_ARRAY = 18;
+    public static final byte RT_STRING = 19;
+    public static final byte RT_NULL = 20;
+    public static final byte RT_COLLECTION = 21;
+    public static final byte RT_REPLACEABLE = 22;
+
     private DroolsObjectStreamConstants() {
     }
 }




More information about the jboss-svn-commits mailing list