Author: jcompagner
Date: Tue Feb 13 16:19:24 2007
New Revision: 507329

URL: http://svn.apache.org/viewvc?view=rev&rev=507329
Log:
some rewrites and optimizations for primitive arrays.
Defaulting for now to the wicket object out

Modified:
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/ClassStreamHandler.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectInputStream.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectOutputStream.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java
    
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/io/WicketOutputStreamTest.java

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/FilePageStore.java
 Tue Feb 13 16:19:24 2007
@@ -408,17 +408,9 @@
 
        private byte[] serializePage(SessionPageKey key, Page page)
        {
-               if (page.getCurrentVersionNumber() != key.versionNumber)
-               {
-                       System.err.println("ERROR versionnumber dont match1!");
-               }
                long t1 = System.currentTimeMillis();
                byte[] bytes = Objects.objectToByteArray(page);
                totalSerializationTime += (System.currentTimeMillis() - t1);
-               if (page.getCurrentVersionNumber() != key.versionNumber)
-               {
-                       System.err.println("ERROR versionnumber dont match2!");
-               }
                return bytes;
        }
 

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/ClassStreamHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/ClassStreamHandler.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/ClassStreamHandler.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/ClassStreamHandler.java
 Tue Feb 13 16:19:24 2007
@@ -21,6 +21,7 @@
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.Serializable;
+import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
@@ -44,23 +45,6 @@
 {
        private static Unsafe unsafe;
 
-
-       private static Map handlesClasses = new HashMap();
-       
-       private static short classCounter = 0;
-
-       private class FieldAndIndex
-       {
-               Field field;
-               long index;
-
-               FieldAndIndex(Field field, long index)
-               {
-                       this.field = field;
-                       this.index = index;
-               }
-       }
-
        static
        {
                try
@@ -87,6 +71,11 @@
        private static final ReflectionFactory reflFactory = 
(ReflectionFactory)AccessController
                        .doPrivileged(new 
ReflectionFactory.GetReflectionFactoryAction());
 
+
+       private static Map handlesClasses = new HashMap();
+       
+       private static short classCounter = 0;
+
        /**
         * 
         */
@@ -152,6 +141,9 @@
 
        private List readObjectMethod;
 
+
+       private PrimitiveArray primitiveArray;
+
        /**
         * Construct.
         * 
@@ -193,6 +185,38 @@
                        cons = null;
                        writeObjectMethod = null;
                        readObjectMethod = null;
+                       if (clz == boolean.class)
+                       {
+                               primitiveArray = new BooleanPrimitiveArray();
+                       }
+                       else if (clz == byte.class)
+                       {
+                               primitiveArray = new BytePrimitiveArray();
+                       }
+                       else if (clz == short.class)
+                       {
+                               primitiveArray = new ShortPrimitiveArray();
+                       }
+                       else if (clz == char.class)
+                       {
+                               primitiveArray = new CharPrimitiveArray();
+                       }
+                       else if (clz == int.class)
+                       {
+                               primitiveArray = new IntPrimitiveArray();
+                       }
+                       else if (clz == long.class)
+                       {
+                               primitiveArray = new LongPrimitiveArray();
+                       }
+                       else if (clz == float.class)
+                       {
+                               primitiveArray = new FloatPrimitiveArray();
+                       }
+                       else if (clz == double.class)
+                       {
+                               primitiveArray = new DoublePrimitiveArray();
+                       }
                }
 
        }
@@ -206,6 +230,27 @@
        }
 
        /**
+        * @return
+        */
+       public short getClassId()
+       {
+               return classId;
+       }
+
+       /**
+        * @return
+        * @throws InvocationTargetException
+        * @throws IllegalAccessException
+        * @throws InstantiationException
+        * @throws IllegalArgumentException
+        */
+       public Object createObject() throws IllegalArgumentException, 
InstantiationException,
+                       IllegalAccessException, InvocationTargetException
+       {
+               return cons.newInstance(null);
+       }
+
+       /**
         * @param cls
         */
        private void fillFields(Class cls)
@@ -218,7 +263,45 @@
                        if (!Modifier.isStatic(field.getModifiers())
                                        && 
!Modifier.isTransient(field.getModifiers()))
                        {
-                               FieldAndIndex fai = new FieldAndIndex(field, 
unsafe.objectFieldOffset(field));
+                               FieldAndIndex fai = null;
+                               Class clz = field.getType();
+                               long offset = unsafe.objectFieldOffset(field);
+                               if (clz == boolean.class)
+                               {
+                                       fai = new BooleanFieldAndIndex(field);
+                               }
+                               else if (clz == byte.class)
+                               {
+                                       fai = new ByteFieldAndIndex(field);
+                               }
+                               else if (clz == short.class)
+                               {
+                                       fai = new ShortFieldAndIndex(field);
+                               }
+                               else if (clz == char.class)
+                               {
+                                       fai = new CharFieldAndIndex(field);
+                               }
+                               else if (clz == int.class)
+                               {
+                                       fai = new IntFieldAndIndex(field);
+                               }
+                               else if (clz == long.class)
+                               {
+                                       fai = new LongFieldAndIndex(field);
+                               }
+                               else if (clz == float.class)
+                               {
+                                       fai = new FloatFieldAndIndex(field);
+                               }
+                               else if (clz == double.class)
+                               {
+                                       fai = new DoubleFieldAndIndex(field);
+                               }
+                               else
+                               {
+                                       fai = new ObjectFieldAndIndex(field);
+                               }
                                this.fields.add(fai);
                        }
                }
@@ -231,28 +314,6 @@
        }
 
        /**
-        * Returns non-static private method with given signature defined by 
given
-        * class, or null if none found. Access checks are disabled on the 
returned
-        * method (if any).
-        */
-       private static Method getPrivateMethod(Class cl, String name, Class[] 
argTypes, Class returnType)
-       {
-               try
-               {
-                       Method meth = cl.getDeclaredMethod(name, argTypes);
-                       meth.setAccessible(true);
-                       int mods = meth.getModifiers();
-                       return ((meth.getReturnType() == returnType) && ((mods 
& Modifier.STATIC) == 0) && ((mods & Modifier.PRIVATE) != 0))
-                                       ? meth
-                                       : null;
-               }
-               catch (NoSuchMethodException ex)
-               {
-                       return null;
-               }
-       }
-
-       /**
         * @param woos
         * @param obj
         * @param out
@@ -264,62 +325,13 @@
                        for (int i = 0; i < fields.size(); i++)
                        {
                                FieldAndIndex fai = 
(FieldAndIndex)fields.get(i);
-                               Field field = fai.field;
-                               Object value = field.get(obj);
-                               if (value instanceof Boolean)
-                               {
-                                       
woos.getOutputStream().writeBoolean(((Boolean)value).booleanValue());
-                                       continue;
-                               }
-                               else if (value instanceof Byte)
-                               {
-                                       
woos.getOutputStream().writeByte(((Byte)value).intValue());
-                                       continue;
-                               }
-                               else if (value instanceof Short)
-                               {
-                                       
woos.getOutputStream().writeShort(((Short)value).intValue());
-                                       continue;
-                               }
-                               else if (value instanceof Character)
-                               {
-                                       
woos.getOutputStream().writeChar(((Character)value).charValue());
-                                       continue;
-                               }
-                               else if (value instanceof Integer)
-                               {
-                                       
woos.getOutputStream().writeInt(((Integer)value).intValue());
-                                       continue;
-                               }
-                               else if (value instanceof Long)
-                               {
-                                       
woos.getOutputStream().writeLong(((Long)value).longValue());
-                                       continue;
-                               }
-                               else if (value instanceof Float)
-                               {
-                                       
woos.getOutputStream().writeFloat(((Float)value).floatValue());
-                                       continue;
-                               }
-                               else if (value instanceof Double)
-                               {
-                                       
woos.getOutputStream().writeDouble(((Double)value).doubleValue());
-                                       continue;
-                               }
-                               else
-                               {
-                                       woos.writeObject(value);
-                               }
+                               fai.writeField(obj, woos);
                        }
                }
                catch (IllegalArgumentException ex)
                {
                        throw new RuntimeException(ex);
                }
-               catch (IllegalAccessException ex)
-               {
-                       throw new RuntimeException(ex);
-               }
                catch (IOException ex)
                {
                        throw new RuntimeException(ex);
@@ -328,28 +340,6 @@
        }
 
        /**
-        * @return
-        */
-       public short getClassId()
-       {
-               return classId;
-       }
-
-       /**
-        * @return
-        * @throws InvocationTargetException
-        * @throws IllegalAccessException
-        * @throws InstantiationException
-        * @throws IllegalArgumentException
-        */
-       public Object createObject() throws IllegalArgumentException, 
InstantiationException,
-                       IllegalAccessException, InvocationTargetException
-       {
-               return cons.newInstance(null);
-       }
-
-
-       /**
         * @param wois
         */
        public void readFields(WicketObjectInputStream wois, Object object)
@@ -358,123 +348,8 @@
                {
                        for (int i = 0; i < fields.size(); i++)
                        {
-                               Object value;
                                FieldAndIndex fai = 
(FieldAndIndex)fields.get(i);
-                               Field field = fai.field;
-                               Class cls = field.getType();
-                               boolean prim = cls.isPrimitive();
-                               if (!prim)
-                               {
-                                       prim = 
Number.class.isAssignableFrom(cls);
-                               }
-                               if (prim)
-                               {
-                                       if (cls == Boolean.class || cls == 
boolean.class)
-                                       {
-                                               value = new 
Boolean(wois.getInputStream().readBoolean());
-                                               if (cls == boolean.class)
-                                               {
-                                                       
unsafe.putBoolean(object, fai.index, ((Boolean)value).booleanValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Byte.class || cls == 
byte.class)
-                                       {
-                                               value = new 
Byte(wois.getInputStream().readByte());
-                                               if (cls == byte.class)
-                                               {
-                                                       unsafe.putByte(object, 
fai.index, ((Byte)value).byteValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Short.class || cls == 
short.class)
-                                       {
-                                               value = new 
Short(wois.getInputStream().readShort());
-                                               if (cls == short.class)
-                                               {
-                                                       unsafe.putShort(object, 
fai.index, ((Short)value).byteValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Character.class || cls 
== char.class)
-                                       {
-                                               value = new 
Character(wois.getInputStream().readChar());
-                                               if (cls == char.class)
-                                               {
-                                                       unsafe.putChar(object, 
fai.index, ((Character)value).charValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Integer.class || cls == 
int.class)
-                                       {
-                                               value = new 
Integer(wois.getInputStream().readInt());
-                                               if (cls == int.class)
-                                               {
-                                                       unsafe.putInt(object, 
fai.index, ((Integer)value).intValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Long.class || cls == 
long.class)
-                                       {
-                                               value = new 
Long(wois.getInputStream().readLong());
-                                               if (cls == long.class)
-                                               {
-                                                       unsafe.putLong(object, 
fai.index, ((Long)value).longValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Float.class || cls == 
float.class)
-                                       {
-                                               value = new 
Float(wois.getInputStream().readFloat());
-                                               if (cls == float.class)
-                                               {
-                                                       unsafe.putFloat(object, 
fai.index, ((Float)value).floatValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else if (cls == Double.class || cls == 
double.class)
-                                       {
-                                               value = new 
Double(wois.getInputStream().readDouble());
-                                               if (cls == double.class)
-                                               {
-                                                       
unsafe.putDouble(object, fai.index, ((Double)value).doubleValue());
-                                               }
-                                               else
-                                               {
-                                                       
unsafe.putObject(object, fai.index, value);
-                                               }
-                                       }
-                                       else
-                                       {
-                                               throw new RuntimeException("not 
support prim type?? "  + cls);
-                                       }
-                               }
-                               else
-                               {
-                                       value = wois.readObject();
-                                       unsafe.putObject(object, fai.index, 
value);
-                               }
+                               fai.readField(object, wois);
                        }
                }
                catch (IllegalArgumentException ex)
@@ -490,58 +365,17 @@
                        throw new RuntimeException(ex);
                }
        }
-
-       private static Constructor getSerializableConstructor(Class cl)
-       {
-               Class initCl = cl;
-               while (Serializable.class.isAssignableFrom(initCl))
-               {
-                       if ((initCl = initCl.getSuperclass()) == null)
-                       {
-                               return null;
-                       }
-               }
-               try
-               {
-                       Constructor cons = 
initCl.getDeclaredConstructor((Class[])null);
-                       int mods = cons.getModifiers();
-                       if ((mods & Modifier.PRIVATE) != 0
-                                       || ((mods & (Modifier.PUBLIC | 
Modifier.PROTECTED)) == 0 && !packageEquals(cl,
-                                                       initCl)))
-                       {
-                               return null;
-                       }
-                       cons = reflFactory.newConstructorForSerialization(cl, 
cons);
-                       cons.setAccessible(true);
-                       return cons;
-               }
-               catch (NoSuchMethodException ex)
-               {
-                       return null;
-               }
-       }
-
-       private static boolean packageEquals(Class cl1, Class cl2)
+       
+       public void writeArray(Object obj, WicketObjectOutputStream wois) 
throws IOException
        {
-               return (cl1.getClassLoader() == cl2.getClassLoader() && 
getPackageName(cl1).equals(
-                               getPackageName(cl2)));
+               primitiveArray.writeArray(obj,wois);
        }
 
-       /**
-        * Returns package name of given class.
-        */
-       private static String getPackageName(Class cl)
+       public Object readArray(WicketObjectInputStream wois) throws IOException
        {
-               String s = cl.getName();
-               int i = s.lastIndexOf('[');
-               if (i >= 0)
-               {
-                       s = s.substring(i + 2);
-               }
-               i = s.lastIndexOf('.');
-               return (i >= 0) ? s.substring(0, i) : "";
+               return primitiveArray.readArray(wois);
        }
-
+       
        /**
         * @param woos
         * @param obj
@@ -613,4 +447,576 @@
                return false;
        }
 
+       private static Constructor getSerializableConstructor(Class cl)
+       {
+               Class initCl = cl;
+               while (Serializable.class.isAssignableFrom(initCl))
+               {
+                       if ((initCl = initCl.getSuperclass()) == null)
+                       {
+                               return null;
+                       }
+               }
+               try
+               {
+                       Constructor cons = 
initCl.getDeclaredConstructor((Class[])null);
+                       int mods = cons.getModifiers();
+                       if ((mods & Modifier.PRIVATE) != 0
+                                       || ((mods & (Modifier.PUBLIC | 
Modifier.PROTECTED)) == 0 && !packageEquals(cl,
+                                                       initCl)))
+                       {
+                               return null;
+                       }
+                       cons = reflFactory.newConstructorForSerialization(cl, 
cons);
+                       cons.setAccessible(true);
+                       return cons;
+               }
+               catch (NoSuchMethodException ex)
+               {
+                       return null;
+               }
+       }
+
+       /**
+        * Returns non-static private method with given signature defined by 
given
+        * class, or null if none found. Access checks are disabled on the 
returned
+        * method (if any).
+        */
+       private static Method getPrivateMethod(Class cl, String name, Class[] 
argTypes, Class returnType)
+       {
+               try
+               {
+                       Method meth = cl.getDeclaredMethod(name, argTypes);
+                       meth.setAccessible(true);
+                       int mods = meth.getModifiers();
+                       return ((meth.getReturnType() == returnType) && ((mods 
& Modifier.STATIC) == 0) && ((mods & Modifier.PRIVATE) != 0))
+                                       ? meth
+                                       : null;
+               }
+               catch (NoSuchMethodException ex)
+               {
+                       return null;
+               }
+       }
+
+       private static boolean packageEquals(Class cl1, Class cl2)
+       {
+               return (cl1.getClassLoader() == cl2.getClassLoader() && 
getPackageName(cl1).equals(
+                               getPackageName(cl2)));
+       }
+
+       /**
+        * Returns package name of given class.
+        */
+       private static String getPackageName(Class cl)
+       {
+               String s = cl.getName();
+               int i = s.lastIndexOf('[');
+               if (i >= 0)
+               {
+                       s = s.substring(i + 2);
+               }
+               i = s.lastIndexOf('.');
+               return (i >= 0) ? s.substring(0, i) : "";
+       }
+
+       private abstract class FieldAndIndex
+       {
+               final Field field;
+               final long index;
+
+               FieldAndIndex(Field field)
+               {
+                       this.field = field;
+                       this.index = unsafe.objectFieldOffset(field);
+               }
+               
+               public abstract void writeField(Object object, 
WicketObjectOutputStream dos)  throws IOException;
+               
+               public abstract void readField(Object object, 
WicketObjectInputStream dos)  throws IOException, ClassNotFoundException;
+       }
+       
+       private final class BooleanFieldAndIndex extends FieldAndIndex
+       {
+               BooleanFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeBoolean(unsafe.getBoolean(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putBoolean(object, index, dos.readBoolean());
+               }
+       }
+       
+       private final class ByteFieldAndIndex extends FieldAndIndex
+       {
+               ByteFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeByte(unsafe.getByte(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putByte(object, index, dos.readByte());
+               }
+       }       
+
+       private final class ShortFieldAndIndex extends FieldAndIndex
+       {
+               ShortFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeShort(unsafe.getShort(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putShort(object, index, dos.readShort());
+               }
+       }       
+       
+       private final class CharFieldAndIndex extends FieldAndIndex
+       {
+               CharFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeChar(unsafe.getChar(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putChar(object, index, dos.readChar());
+               }
+       }       
+       
+       private final class IntFieldAndIndex extends FieldAndIndex
+       {
+               IntFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeInt(unsafe.getInt(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putInt(object, index, dos.readInt());
+               }
+       }       
+       
+       private final class LongFieldAndIndex extends FieldAndIndex
+       {
+               LongFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeLong(unsafe.getLong(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putLong(object, index, dos.readLong());
+               }
+       }       
+       
+       private final class FloatFieldAndIndex extends FieldAndIndex
+       {
+               FloatFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeFloat(unsafe.getFloat(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putFloat(object, index, dos.readFloat());
+               }
+       }       
+       
+       private final class DoubleFieldAndIndex extends FieldAndIndex
+       {
+               DoubleFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeDouble(unsafe.getDouble(object, index));
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException
+               {
+                       unsafe.putDouble(object, index, dos.readDouble());
+               }
+       }
+       
+       private final class ObjectFieldAndIndex extends FieldAndIndex
+       {
+               ObjectFieldAndIndex(Field field)
+               {
+                       super(field);
+               }
+               
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#writeField(java.lang.Object)
+                */
+               public void writeField(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       dos.writeObject(unsafe.getObject(object, index));
+               }
+               
+               /**
+                * @throws ClassNotFoundException 
+                * @see 
wicket.util.io.ClassStreamHandler.FieldAndIndex#readField(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public void readField(Object object, WicketObjectInputStream 
dos) throws IOException, ClassNotFoundException
+               {
+                       unsafe.putObject(object, index, dos.readObject());
+               }
+       }
+       
+
+       private abstract class PrimitiveArray
+       {
+               public abstract void writeArray(Object object, 
WicketObjectOutputStream dos)  throws IOException;
+               
+               public abstract Object readArray(WicketObjectInputStream dos)  
throws IOException;
+       }
+       
+       private final class BooleanPrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeBoolean(Array.getBoolean(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setBoolean(array, i, dos.readBoolean());
+                       }
+                       return array;
+               }
+       }
+       
+       private final class BytePrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeByte(Array.getByte(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setByte(array, i, dos.readByte());
+                       }
+                       return array;
+               }
+       }       
+
+       private final class ShortPrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeShort(Array.getShort(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setShort(array, i, dos.readShort());
+                       }
+                       return array;
+               }
+       }       
+       
+       private final class CharPrimitiveArray extends PrimitiveArray
+       {
+       
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeChar(Array.getChar(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setChar(array, i, dos.readChar());
+                       }
+                       return array;
+               }
+       }       
+       
+       private final class IntPrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeInt(Array.getInt(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setInt(array, i, dos.readInt());
+                       }
+                       return array;
+               }
+       }       
+       
+       private final class LongPrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeLong(Array.getLong(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setLong(array, i, dos.readLong());
+                       }
+                       return array;
+               }
+       }       
+       
+       private final class FloatPrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeFloat(Array.getFloat(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setFloat(array, i, dos.readFloat());
+                       }
+                       return array;
+               }
+       }       
+       
+       private final class DoublePrimitiveArray extends PrimitiveArray
+       {
+               /**
+                * @throws IOException 
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#writeArray(java.lang.Object)
+                */
+               public void writeArray(Object object, WicketObjectOutputStream 
dos) throws IOException
+               {
+                       int length = Array.getLength(object);
+                       dos.writeInt(length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               dos.writeDouble(Array.getDouble(object, i));
+                       }
+               }
+               
+               /**
+                * @see 
wicket.util.io.ClassStreamHandler.PrimitiveArray#readArray(java.lang.Object, 
java.io.WicketObjectInputStream)
+                */
+               public Object readArray(WicketObjectInputStream dos) throws 
IOException
+               {
+                       int length = dos.readInt();
+                       Object array = Array.newInstance(getStreamClass(), 
length);
+                       for (int i = 0; i < length; i++)
+                       {
+                               Array.setDouble(array, i, dos.readDouble());
+                       }
+                       return array;
+               }
+       }
+       
 }

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectInputStream.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectInputStream.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectInputStream.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectInputStream.java
 Tue Feb 13 16:19:24 2007
@@ -136,14 +136,8 @@
                {
                        short classDef = in.readShort();
                        ClassStreamHandler lookup = 
ClassStreamHandler.lookup(classDef);
-                       int length = in.readInt();
-                       Object array = 
Array.newInstance(lookup.getStreamClass(), length);
-                       handledObjects.put(handleCounter++,array);
-                       for (int i = 0; i < length; i++)
-                       {
-                               Array.set(array, i, readObjectOverride());
-                       }
-                       value = array;
+                       value = lookup.readArray(this);
+                       handledObjects.put(handleCounter++,value);
                }
                else
                {
@@ -165,13 +159,6 @@
                }
        }
 
-       /**
-        * @return
-        */
-       DataInputStream getInputStream()
-       {
-               return in;
-       }
        
        /**
         * @see java.io.ObjectInputStream#close()
@@ -327,7 +314,8 @@
      */
     public String readUTF() throws IOException
     {
-       return in.readUTF();
+       String s = in.readUTF();
+       return s;
     }
     
     /**

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectOutputStream.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectOutputStream.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectOutputStream.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/io/WicketObjectOutputStream.java
 Tue Feb 13 16:19:24 2007
@@ -21,8 +21,6 @@
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.List;
 
 import wicket.util.collections.HandleArrayListStack;
 
@@ -38,6 +36,10 @@
        
        private final DataOutputStream out;
        private ClassStreamHandler classHandler;
+
+       private int booleanCounter;
+
+       private int byteCounter;
        
        
        /**
@@ -66,7 +68,7 @@
                if ( handle != -1)
                {
                        out.write(ClassStreamHandler.HANDLE);
-                       out.writeShort((int)handle);
+                       out.writeShort(handle);
                }
                else
                {
@@ -89,14 +91,7 @@
                                        {
                                                
out.write(ClassStreamHandler.PRIMITIVE_ARRAY);
                                                
out.writeShort(classHandler.getClassId());
-                                               // this should be different!!
-                                               // write directly the 
primitives 
-                                               int length = 
Array.getLength(obj);
-                                               out.writeInt(length);
-                                               for (int i = 0; i < length; i++)
-                                               {
-                                                       
writeObjectOverride(Array.get(obj, i));
-                                               }
+                                               
classHandler.writeArray(obj,this);
                                        }
                                        else
                                        {
@@ -148,15 +143,6 @@
                        classHandler.writeFields(this,currentObject);
                }
        }
-
-       /**
-        * @return
-        */
-       DataOutputStream getOutputStream()
-       {
-               return out;
-       }
-
        
          /**
      * Writes a boolean.
@@ -265,15 +251,14 @@
      *                 stream
      */
     public void writeChars(String str) throws IOException {
-       out.writeChars(str);
+       out.writeChars(str);
     }
        
     /**
      * @see java.io.ObjectOutputStream#write(byte[])
      */
     public void write(byte[] buf) throws IOException
-    {
-       out.write(buf);
+    {          out.write(buf);
     }
     
     /**

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java
 Tue Feb 13 16:19:24 2007
@@ -39,6 +39,7 @@
 import wicket.settings.IApplicationSettings;
 import wicket.util.io.ByteCountingOutputStream;
 import wicket.util.io.IObjectStreamFactory;
+import wicket.util.io.WicketObjectStreamFactory;
 import wicket.util.io.IObjectStreamFactory.DefaultObjectStreamFactory;
 import wicket.util.string.Strings;
 
@@ -194,7 +195,8 @@
         * opposed to in Application, as the Application most likely isn't 
available
         * in the threads we'll be using this with.
         */
-       private static IObjectStreamFactory objectStreamFactory = new 
IObjectStreamFactory.DefaultObjectStreamFactory();
+       private static IObjectStreamFactory objectStreamFactory = new 
WicketObjectStreamFactory();
+       //private static IObjectStreamFactory objectStreamFactory = new 
IObjectStreamFactory.DefaultObjectStreamFactory();
 
        static
        {

Modified: 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/io/WicketOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/io/WicketOutputStreamTest.java?view=diff&rev=507329&r1=507328&r2=507329
==============================================================================
--- 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/io/WicketOutputStreamTest.java
 (original)
+++ 
incubator/wicket/branches/wicket-1.x/wicket/src/test/java/wicket/util/io/WicketOutputStreamTest.java
 Tue Feb 13 16:19:24 2007
@@ -17,8 +17,6 @@
 package wicket.util.io;
 
 import java.io.ByteArrayInputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.util.GregorianCalendar;
 
 import junit.framework.Assert;
@@ -30,6 +28,9 @@
 public class WicketOutputStreamTest extends TestCase
 {
 
+       /**
+        * @throws Exception
+        */
        public void testGregorianCalendar() throws Exception
        {
                GregorianCalendar gc = new GregorianCalendar(2005,10,10);
@@ -48,4 +49,5 @@
                Assert.assertEquals(gc, gc2);
                
        }
+
 }


Reply via email to