Author: trustin
Date: Thu Nov 15 17:31:56 2007
New Revision: 595517

URL: http://svn.apache.org/viewvc?rev=595517&view=rev
Log:
Resolved issue: DIRMINA-481 (ByteBuffer.get/putObject() cannot handler 
pritimive classes.)
* Changed IoBuffer.get/putObject to take care of primitive classes by calling 
super.writeClassDescriptor()
* Added void type to DefaultIoEventSizeEstimator and DefaultMessageSizeEstimator


Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
    mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java
    
mina/trunk/core/src/main/java/org/apache/mina/filter/executor/DefaultIoEventSizeEstimator.java
    
mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/DefaultMessageSizeEstimator.java
    mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java

Modified: 
mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
Thu Nov 15 17:31:56 2007
@@ -19,12 +19,14 @@
  */
 package org.apache.mina.common;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.OutputStream;
+import java.io.StreamCorruptedException;
 import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteOrder;
@@ -38,6 +40,8 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.mina.common.support.ByteBufferHexDumper;
 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
@@ -246,6 +250,20 @@
         return wrap(java.nio.ByteBuffer.wrap(byteArray, offset, length));
     }
 
+    private static final Set primitiveTypeNames = new HashSet();
+    
+    static {
+        primitiveTypeNames.add("void");
+        primitiveTypeNames.add("boolean");
+        primitiveTypeNames.add("byte");
+        primitiveTypeNames.add("char");
+        primitiveTypeNames.add("short");
+        primitiveTypeNames.add("int");
+        primitiveTypeNames.add("long");
+        primitiveTypeNames.add("float");
+        primitiveTypeNames.add("double");
+    }
+
     protected ByteBuffer() {
     }
 
@@ -1477,9 +1495,22 @@
             ObjectInputStream in = new ObjectInputStream(asInputStream()) {
                 protected ObjectStreamClass readClassDescriptor()
                         throws IOException, ClassNotFoundException {
-                    String className = readUTF();
-                    Class clazz = Class.forName(className, true, classLoader);
-                    return ObjectStreamClass.lookup(clazz);
+                    int type = read();
+                    if (type < 0) {
+                        throw new EOFException();
+                    }
+                    switch (type) {
+                    case 0: // Primitive types
+                        return super.readClassDescriptor();
+                    case 1: // Non-primitive types
+                        String className = readUTF();
+                        Class clazz =
+                            Class.forName(className, true, classLoader);
+                        return ObjectStreamClass.lookup(clazz);
+                    default:
+                        throw new StreamCorruptedException(
+                                "Unexpected class descriptor type: " + type);
+                    }
                 }
             };
             return in.readObject();
@@ -1500,7 +1531,14 @@
             ObjectOutputStream out = new ObjectOutputStream(asOutputStream()) {
                 protected void writeClassDescriptor(ObjectStreamClass desc)
                         throws IOException {
-                    writeUTF(desc.getName());
+                    String className = desc.getName();
+                    if (primitiveTypeNames.contains(className)) {
+                        write(0);
+                        super.writeClassDescriptor(desc);
+                    } else {
+                        write(1);
+                        writeUTF(desc.getName());
+                    }
                 }
             };
             out.writeObject(o);

Modified: 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
(original)
+++ 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
Thu Nov 15 17:31:56 2007
@@ -19,9 +19,6 @@
  */
 package org.apache.mina.common;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 import java.nio.BufferOverflowException;
 import java.nio.ByteOrder;
 import java.nio.ReadOnlyBufferException;
@@ -33,6 +30,9 @@
 import java.util.Date;
 import java.util.List;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 /**
  * Tests [EMAIL PROTECTED] ByteBuffer}.
  *
@@ -516,6 +516,7 @@
         buf.setAutoExpand(true);
         List o = new ArrayList();
         o.add(new Date());
+        o.add(long.class);
 
         // Test writing an object.
         buf.putObject(o);

Modified: 
mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
Thu Nov 15 17:31:56 2007
@@ -19,12 +19,14 @@
  */
 package org.apache.mina.common;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.OutputStream;
+import java.io.StreamCorruptedException;
 import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteOrder;
@@ -38,6 +40,8 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.mina.common.support.ByteBufferHexDumper;
 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
@@ -246,6 +250,20 @@
         return wrap(java.nio.ByteBuffer.wrap(byteArray, offset, length));
     }
 
+    private static final Set<String> primitiveTypeNames = new 
HashSet<String>();
+    
+    static {
+        primitiveTypeNames.add("void");
+        primitiveTypeNames.add("boolean");
+        primitiveTypeNames.add("byte");
+        primitiveTypeNames.add("char");
+        primitiveTypeNames.add("short");
+        primitiveTypeNames.add("int");
+        primitiveTypeNames.add("long");
+        primitiveTypeNames.add("float");
+        primitiveTypeNames.add("double");
+    }
+
     protected ByteBuffer() {
     }
 
@@ -527,6 +545,7 @@
      */
     public abstract ByteBuffer compact();
 
+    @Override
     public String toString() {
         StringBuffer buf = new StringBuffer();
         if (isDirect()) {
@@ -546,6 +565,7 @@
         return buf.toString();
     }
 
+    @Override
     public int hashCode() {
         int h = 1;
         int p = position();
@@ -555,6 +575,7 @@
         return h;
     }
 
+    @Override
     public boolean equals(Object o) {
         if (!(o instanceof ByteBuffer)) {
             return false;
@@ -788,18 +809,22 @@
      */
     public InputStream asInputStream() {
         return new InputStream() {
+            @Override
             public int available() {
                 return ByteBuffer.this.remaining();
             }
 
+            @Override
             public synchronized void mark(int readlimit) {
                 ByteBuffer.this.mark();
             }
 
+            @Override
             public boolean markSupported() {
                 return true;
             }
 
+            @Override
             public int read() {
                 if (ByteBuffer.this.hasRemaining()) {
                     return ByteBuffer.this.get() & 0xff;
@@ -808,6 +833,7 @@
                 }
             }
 
+            @Override
             public int read(byte[] b, int off, int len) {
                 int remaining = ByteBuffer.this.remaining();
                 if (remaining > 0) {
@@ -819,10 +845,12 @@
                 }
             }
 
+            @Override
             public synchronized void reset() {
                 ByteBuffer.this.reset();
             }
 
+            @Override
             public long skip(long n) {
                 int bytes;
                 if (n > Integer.MAX_VALUE) {
@@ -846,10 +874,12 @@
      */
     public OutputStream asOutputStream() {
         return new OutputStream() {
+            @Override
             public void write(byte[] b, int off, int len) {
                 ByteBuffer.this.put(b, off, len);
             }
 
+            @Override
             public void write(int b) {
                 ByteBuffer.this.put((byte) b);
             }
@@ -1474,12 +1504,25 @@
         limit(position() + length);
         try {
             ObjectInputStream in = new ObjectInputStream(asInputStream()) {
+                @Override
                 protected ObjectStreamClass readClassDescriptor()
                         throws IOException, ClassNotFoundException {
-                    String className = readUTF();
-                    Class<?> clazz = Class
-                            .forName(className, true, classLoader);
-                    return ObjectStreamClass.lookup(clazz);
+                    int type = read();
+                    if (type < 0) {
+                        throw new EOFException();
+                    }
+                    switch (type) {
+                    case 0: // Primitive types
+                        return super.readClassDescriptor();
+                    case 1: // Non-primitive types
+                        String className = readUTF();
+                        Class<?> clazz =
+                            Class.forName(className, true, classLoader);
+                        return ObjectStreamClass.lookup(clazz);
+                    default:
+                        throw new StreamCorruptedException(
+                                "Unexpected class descriptor type: " + type);
+                    }
                 }
             };
             return in.readObject();
@@ -1498,9 +1541,17 @@
         skip(4); // Make a room for the length field.
         try {
             ObjectOutputStream out = new ObjectOutputStream(asOutputStream()) {
+                @Override
                 protected void writeClassDescriptor(ObjectStreamClass desc)
                         throws IOException {
-                    writeUTF(desc.getName());
+                    String className = desc.getName();
+                    if (primitiveTypeNames.contains(className)) {
+                        write(0);
+                        super.writeClassDescriptor(desc);
+                    } else {
+                        write(1);
+                        writeUTF(desc.getName());
+                    }
                 }
             };
             out.writeObject(o);

Modified: 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
(original)
+++ 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
Thu Nov 15 17:31:56 2007
@@ -19,9 +19,6 @@
  */
 package org.apache.mina.common;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
-
 import java.nio.BufferOverflowException;
 import java.nio.ByteOrder;
 import java.nio.ReadOnlyBufferException;
@@ -33,6 +30,9 @@
 import java.util.Date;
 import java.util.List;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 /**
  * Tests [EMAIL PROTECTED] ByteBuffer}.
  *
@@ -45,9 +45,11 @@
         junit.textui.TestRunner.run(ByteBufferTest.class);
     }
 
+    @Override
     protected void setUp() throws Exception {
     }
 
+    @Override
     protected void tearDown() throws Exception {
     }
 
@@ -516,6 +518,7 @@
         buf.setAutoExpand(true);
         List<Object> o = new ArrayList<Object>();
         o.add(new Date());
+        o.add(long.class);
 
         // Test writing an object.
         buf.putObject(o);

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/IoBuffer.java Thu Nov 
15 17:31:56 2007
@@ -19,12 +19,14 @@
  */
 package org.apache.mina.common;
 
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.OutputStream;
+import java.io.StreamCorruptedException;
 import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
@@ -41,9 +43,9 @@
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
 import java.util.EnumSet;
+import java.util.HashSet;
 import java.util.Set;
 
-
 /**
  * A byte buffer used by MINA applications.
  * <p>
@@ -150,8 +152,13 @@
  */
 public abstract class IoBuffer implements Comparable<IoBuffer> {
     private static IoBufferAllocator allocator = new SimpleBufferAllocator();
-    private static final IoBuffer EMPTY_DIRECT_BUFFER = allocator.allocate(0, 
true);
-    private static final IoBuffer EMPTY_HEAP_BUFFER = allocator.allocate(0, 
false);
+
+    private static final IoBuffer EMPTY_DIRECT_BUFFER = allocator.allocate(0,
+            true);
+
+    private static final IoBuffer EMPTY_HEAP_BUFFER = allocator.allocate(0,
+            false);
+
     private static boolean useDirectBuffer = false;
 
     /**
@@ -222,13 +229,13 @@
      */
     public static IoBuffer allocate(int capacity, boolean direct) {
         if (capacity == 0) {
-            return direct? EMPTY_DIRECT_BUFFER : EMPTY_HEAP_BUFFER;
+            return direct ? EMPTY_DIRECT_BUFFER : EMPTY_HEAP_BUFFER;
         }
-        
+
         if (capacity < 0) {
             throw new IllegalArgumentException("capacity: " + capacity);
         }
-        
+
         return allocator.allocate(capacity, direct);
     }
 
@@ -252,7 +259,7 @@
     public static IoBuffer wrap(byte[] byteArray, int offset, int length) {
         return wrap(ByteBuffer.wrap(byteArray, offset, length));
     }
-    
+
     /**
      * Normalizes the specified capacity of the buffer to power of 2,
      * which is often helpful for optimal memory usage and performance. 
@@ -262,16 +269,40 @@
     protected static int normalizeCapacity(int requestedCapacity) {
         switch (requestedCapacity) {
         case 0:
-        case 1 <<  0: case 1 <<  1: case 1 <<  2: case 1 <<  3: case 1 <<  4:
-        case 1 <<  5: case 1 <<  6: case 1 <<  7: case 1 <<  8: case 1 <<  9:
-        case 1 << 10: case 1 << 11: case 1 << 12: case 1 << 13: case 1 << 14:
-        case 1 << 15: case 1 << 16: case 1 << 17: case 1 << 18: case 1 << 19:
-        case 1 << 21: case 1 << 22: case 1 << 23: case 1 << 24: case 1 << 25:
-        case 1 << 26: case 1 << 27: case 1 << 28: case 1 << 29: case 1 << 30:
+        case 1 << 0:
+        case 1 << 1:
+        case 1 << 2:
+        case 1 << 3:
+        case 1 << 4:
+        case 1 << 5:
+        case 1 << 6:
+        case 1 << 7:
+        case 1 << 8:
+        case 1 << 9:
+        case 1 << 10:
+        case 1 << 11:
+        case 1 << 12:
+        case 1 << 13:
+        case 1 << 14:
+        case 1 << 15:
+        case 1 << 16:
+        case 1 << 17:
+        case 1 << 18:
+        case 1 << 19:
+        case 1 << 21:
+        case 1 << 22:
+        case 1 << 23:
+        case 1 << 24:
+        case 1 << 25:
+        case 1 << 26:
+        case 1 << 27:
+        case 1 << 28:
+        case 1 << 29:
+        case 1 << 30:
         case Integer.MAX_VALUE:
             return requestedCapacity;
         }
-        
+
         int newCapacity = 1;
         while (newCapacity < requestedCapacity) {
             newCapacity <<= 1;
@@ -281,15 +312,27 @@
         }
         return newCapacity;
     }
-
+    
+    private static final Set<String> primitiveTypeNames = new 
HashSet<String>();
+    
+    static {
+        primitiveTypeNames.add("void");
+        primitiveTypeNames.add("boolean");
+        primitiveTypeNames.add("byte");
+        primitiveTypeNames.add("char");
+        primitiveTypeNames.add("short");
+        primitiveTypeNames.add("int");
+        primitiveTypeNames.add("long");
+        primitiveTypeNames.add("float");
+        primitiveTypeNames.add("double");
+    }
 
     /**
      * Creates a new instance.  This is an empty constructor.
-     *
      */
     protected IoBuffer() {
     }
-    
+
     /**
      * Declares this buffer and all its derived buffers are not used anymore
      * so that it can be reused by some [EMAIL PROTECTED] IoBufferAllocator} 
implementations.
@@ -302,12 +345,12 @@
      * Returns the underlying NIO buffer instance.
      */
     public abstract ByteBuffer buf();
-    
+
     /**
      * @see ByteBuffer#isDirect()
      */
     public abstract boolean isDirect();
-    
+
     /**
      * returns <tt>true</tt> if and only if this buffer is derived from other 
buffer
      * via [EMAIL PROTECTED] #duplicate()}, [EMAIL PROTECTED] #slice()} or 
[EMAIL PROTECTED] #asReadOnlyBuffer()}.
@@ -326,7 +369,7 @@
      * of the buffer.
      */
     public abstract int minimumCapacity();
-    
+
     /**
      * Sets the minimum capacity of this buffer which is used to determine
      * the new capacity of the buffer shrunk by [EMAIL PROTECTED] #compact()} 
and
@@ -358,7 +401,7 @@
      * Turns on or off <tt>autoExpand</tt>.
      */
     public abstract IoBuffer setAutoExpand(boolean autoExpand);
-    
+
     /**
      * Returns <tt>true</tt> if and only if <tt>autoShrink</tt> is turned on.
      */
@@ -387,7 +430,7 @@
      * <tt>true</tt>.
      */
     public abstract IoBuffer expand(int position, int expectedRemaining);
-    
+
     /**
      * Changes the capacity of this buffer so this buffer occupies as less
      * memory as possible while retaining the position, limit and the
@@ -396,7 +439,7 @@
      * The mark is discarded once the capacity changes.
      */
     public abstract IoBuffer shrink();
-    
+
     /**
      * @see java.nio.Buffer#position()
      */
@@ -1746,10 +1789,22 @@
                 @Override
                 protected ObjectStreamClass readClassDescriptor()
                         throws IOException, ClassNotFoundException {
-                    String className = readUTF();
-                    Class<?> clazz = Class
-                            .forName(className, true, classLoader);
-                    return ObjectStreamClass.lookup(clazz);
+                    int type = read();
+                    if (type < 0) {
+                        throw new EOFException();
+                    }
+                    switch (type) {
+                    case 0: // Primitive types
+                        return super.readClassDescriptor();
+                    case 1: // Non-primitive types
+                        String className = readUTF();
+                        Class<?> clazz =
+                            Class.forName(className, true, classLoader);
+                        return ObjectStreamClass.lookup(clazz);
+                    default:
+                        throw new StreamCorruptedException(
+                                "Unexpected class descriptor type: " + type);
+                    }
                 }
             };
             return in.readObject();
@@ -1771,7 +1826,14 @@
                 @Override
                 protected void writeClassDescriptor(ObjectStreamClass desc)
                         throws IOException {
-                    writeUTF(desc.getName());
+                    String className = desc.getName();
+                    if (primitiveTypeNames.contains(className)) {
+                        write(0);
+                        super.writeClassDescriptor(desc);
+                    } else {
+                        write(1);
+                        writeUTF(desc.getName());
+                    }
                 }
             };
             out.writeObject(o);

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/filter/executor/DefaultIoEventSizeEstimator.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/executor/DefaultIoEventSizeEstimator.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/filter/executor/DefaultIoEventSizeEstimator.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/filter/executor/DefaultIoEventSizeEstimator.java
 Thu Nov 15 17:31:56 2007
@@ -56,6 +56,7 @@
         class2size.put(long.class, 8);
         class2size.put(float.class, 4);
         class2size.put(double.class, 8);
+        class2size.put(void.class, 0);
     }
     
     public int estimateSize(IoEvent event) {

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/DefaultMessageSizeEstimator.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/DefaultMessageSizeEstimator.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/DefaultMessageSizeEstimator.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/DefaultMessageSizeEstimator.java
 Thu Nov 15 17:31:56 2007
@@ -55,6 +55,7 @@
         class2size.put(long.class, 8);
         class2size.put(float.class, 4);
         class2size.put(double.class, 8);
+        class2size.put(void.class, 0);
     }
     
     public int estimateSize(Object message) {

Modified: mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java?rev=595517&r1=595516&r2=595517&view=diff
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java 
(original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/IoBufferTest.java Thu 
Nov 15 17:31:56 2007
@@ -544,6 +544,7 @@
         buf.setAutoExpand(true);
         List<Object> o = new ArrayList<Object>();
         o.add(new Date());
+        o.add(long.class);
 
         // Test writing an object.
         buf.putObject(o);


Reply via email to