You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2011/11/29 12:11:59 UTC

svn commit: r1207826 - in /mina/trunk/core/src: main/java/org/apache/mina/util/IoBuffer.java test/java/org/apache/mina/util/IoBufferTest.java

Author: elecharny
Date: Tue Nov 29 11:11:58 2011
New Revision: 1207826

URL: http://svn.apache.org/viewvc?rev=1207826&view=rev
Log:
o Made the constructor accept multiple ByteBuffers
o Implemented the get(byte[]), order() and order(ByteOrder) methods


Modified:
    mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java
    mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java?rev=1207826&r1=1207825&r2=1207826&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/IoBuffer.java Tue Nov 29 11:11:58 2011
@@ -61,7 +61,7 @@ public class IoBuffer {
     private boolean readOnly;
     
     /** The bytes order (BIG_INDIAN or LITTLE_INDIAN) */
-    private ByteOrder order;
+    private ByteOrder order = ByteOrder.BIG_ENDIAN;
     
     /** The two types of buffer we handle */
     public enum BufferType {
@@ -95,6 +95,28 @@ public class IoBuffer {
     }
     
     /**
+     * Construct a IoBuffer with some ByteBuffers. The IoBuffer type will be selected
+     * from the first ByteBuffer type, so will the order.
+     * @param byteBuffers the ByteBuffers added to the IoBuffer list
+     */
+    public IoBuffer(ByteBuffer... byteBuffers) {
+        if ((byteBuffers == null) || (byteBuffers.length == 0)){
+            position = 0;
+            mark = 0;
+            limit = 0;
+            type = null;
+            order = null;
+        } else {
+            for (ByteBuffer byteBuffer : byteBuffers) {
+                
+                if (byteBuffer.limit() > 0) {
+                    buffers.add(byteBuffer);
+                }
+            }
+        }
+    }
+    
+    /**
      * Construct a IoBuffer with no ByteBuffer. The IoBuffer type is not direct.
      * (ie direct or heap will be deduce from this first ByteBuffer characteristic.
      * @param byteBuffer the first ByteBuffer added to the IoBuffer list
@@ -385,10 +407,33 @@ public class IoBuffer {
     
     /**
      * @see ByteBuffer#get(byte[])
+     * Reads bytes from the current position into the specified byte array and
+     * increases the position by the number of bytes read.
+     * <p>
+     * Calling this method has the same effect as
+     * {@code get(dest, 0, dest.length)}.
+     *
+     * @param dest the destination byte array.
+     * @return this IoBuffer.
+     * @exception BufferUnderflowException if {@code dest.length} is greater than {@code remaining()}.
      */
     public IoBuffer get(byte[] dst) {
-        // TODO code me !
-        throw new UnsupportedOperationException();
+        if (dst.length > remaining()) {
+            throw new BufferUnderflowException();
+        }
+        
+        int size = dst.length;
+        int destPos = 0;
+        BufferNode node = buffers.current;
+        
+        while (size > 0) {
+            int length = node.buffer.limit() - node.buffer.position();
+            System.arraycopy(node.buffer.array(), node.buffer.position(), dst, destPos, length );
+            destPos += length;
+            node = buffers.getNext();
+        }
+        
+        return this;
     }
     
     /**
@@ -626,18 +671,37 @@ public class IoBuffer {
 
     /**
      * @see ByteBuffer#order()
+     * Returns the byte order used by this Iouffer when converting bytes from/to
+     * other primitive types.
+     * <p>
+     * The default byte order of byte buffer is always
+     * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
+     *
+     * @return the byte order used by this IoBuffer when converting bytes from/to
+     * other primitive types.
+
      */
-    public IoBuffer order() {
-        // TODO code me !
-        throw new UnsupportedOperationException();
+    public ByteOrder order() {
+        return order;
     }
     
     /**
      * @see ByteBuffer#order(ByteOrder)
+     * Sets the byte order of this IoBuffer.
+     * 
+     * @param byteOrder the byte order to set. If {@code null} then the order
+     * will be {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}.
+     * @return this IoBuffer.
+     * @see ByteOrder
      */
     public IoBuffer order(ByteOrder bo) {
-        // TODO code me !
-        throw new UnsupportedOperationException();
+        if (bo == null) {
+            order = ByteOrder.LITTLE_ENDIAN;
+        } else {
+            order = bo;
+        }
+        
+        return this;
     }
     
     /**
@@ -972,8 +1036,8 @@ public class IoBuffer {
                 }
             }
             
-            // Check the endianness
-            if (order == null) {
+            // Check the ByteOrder
+            if (size == 0) {
                 order = byteBuffer.order();
             } else if (order != byteBuffer.order()) {
                 throw new RuntimeException();

Modified: mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java?rev=1207826&r1=1207825&r2=1207826&view=diff
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java (original)
+++ mina/trunk/core/src/test/java/org/apache/mina/util/IoBufferTest.java Tue Nov 29 11:11:58 2011
@@ -249,8 +249,8 @@ public class IoBufferTest
         bb2.put("345".getBytes());
         bb2.flip();
         
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+
         assertEquals(0, ioBuffer.position());
         assertEquals(6, ioBuffer.limit());
         assertTrue(ioBuffer.hasRemaining());
@@ -336,8 +336,7 @@ public class IoBufferTest
         bb3.put("3456".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2, bb3);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2, bb3);
         
         byte[] array = ioBuffer.array();
         assertNotNull(array);
@@ -373,8 +372,7 @@ public class IoBufferTest
         bb2.putInt(67890);
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
 
         assertEquals(12345, ioBuffer.getInt());
         assertEquals(67890, ioBuffer.getInt());
@@ -396,8 +394,7 @@ public class IoBufferTest
         bb2.putInt(67890);
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
 
         assertEquals(12345, ioBuffer.getInt());
         assertEquals(67890, ioBuffer.getInt());
@@ -416,8 +413,7 @@ public class IoBufferTest
         bb2.put(new byte[]{0x02, 0x03, 0x04});
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
 
         assertEquals(0x01020304, ioBuffer.getInt());
     }
@@ -435,8 +431,7 @@ public class IoBufferTest
         bb2.put(new byte[]{0x02, 0x03});
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
-        ioBuffer.add(bb1, bb2);
+        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
 
         ioBuffer.getInt();
     }