You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by rb...@apache.org on 2013/07/01 14:35:52 UTC

[1/5] git commit: removed direct access to constructors

Updated Branches:
  refs/heads/trunk ca12d4855 -> fffe4b565


removed direct access to constructors


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/fb03fe68
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/fb03fe68
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/fb03fe68

Branch: refs/heads/trunk
Commit: fb03fe6818487455b38ac4fcf9688bbf94d16170
Parents: ca12d48
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 11:01:41 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Sun Jun 30 11:03:48 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    | 26 ++++----
 .../org/apache/mina/codec/IoBufferTest.java     | 68 ++++++++++----------
 2 files changed, 49 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/fb03fe68/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index 8dcc7ec..cf127b7 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -36,16 +36,21 @@ public final class IoBuffer {
      * @see ByteBuffer#allocate(int)
      */
     public static IoBuffer allocate(int capacity) {
-        return new IoBuffer(ByteBuffer.allocate(capacity));
+        return wrap(ByteBuffer.allocate(capacity));
     }
 
     /**
      * @see ByteBuffer#allocateDirect(int)
      */
     public static IoBuffer allocateDirect(int capacity) {
-        return new IoBuffer(ByteBuffer.allocateDirect(capacity));
+        return wrap(ByteBuffer.allocateDirect(capacity));
     }
 
+    /**
+     * Build a new instance of {@link IoBuffer}
+     * 
+     * @return a new instance of {@link IoBuffer}
+     */
     public static IoBuffer newInstance() {
         return new IoBuffer();
     }
@@ -65,7 +70,7 @@ public final class IoBuffer {
      * @see ByteBuffer#wrap(byte[], int, int)
      */
     public static IoBuffer wrap(byte[] array, int offset, int length) {
-        return new IoBuffer(ByteBuffer.wrap(array, offset, length));
+        return wrap(ByteBuffer.wrap(array, offset, length));
     }
 
     public static IoBuffer wrap(ByteBuffer... buffers) {
@@ -102,15 +107,14 @@ public final class IoBuffer {
         mark = null;
     }
 
-    //TODO set this method deprecated
-    //    @Deprecated
-    public IoBuffer(ByteBuffer... buffers) {
-        this();
-        for (ByteBuffer b : buffers) {
-            add(b);
-        }
-    }
+    
 
+    /**
+     * Add one or more ByteBuffer to the current IoBuffer
+     * 
+     * @param buffers
+     * @return the current {@link IoBuffer}
+     */
     public IoBuffer add(ByteBuffer... buffers) {
         for (ByteBuffer buffer : buffers) {
             enqueue(buffer.slice());

http://git-wip-us.apache.org/repos/asf/mina/blob/fb03fe68/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
index 399eeef..3c92f66 100644
--- a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
@@ -53,7 +53,7 @@ public class IoBufferTest {
         bb3.put("6789".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         assertEquals(0, ioBuffer.position());
@@ -90,7 +90,7 @@ public class IoBufferTest {
         bb3.put("3456".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         assertEquals(0, ioBuffer.position());
@@ -123,7 +123,7 @@ public class IoBufferTest {
         bb2.put("3456".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2);
     }
 
@@ -142,7 +142,7 @@ public class IoBufferTest {
         bb2.put("3456".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2);
     }
 
@@ -239,7 +239,7 @@ public class IoBufferTest {
         bb.put("012".getBytes());
         bb.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb);
         assertEquals(0, ioBuffer.position());
         assertEquals(3, ioBuffer.limit());
 
@@ -267,7 +267,7 @@ public class IoBufferTest {
     public void testGetOneBuffer0Bytes() {
         ByteBuffer bb = ByteBuffer.allocate(0);
 
-        IoBuffer ioBuffer = new IoBuffer(bb);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb);
         assertEquals(0, ioBuffer.position());
         assertEquals(0, ioBuffer.limit());
 
@@ -294,7 +294,7 @@ public class IoBufferTest {
         bb2.put("345".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         assertEquals(0, ioBuffer.position());
         assertEquals(6, ioBuffer.limit());
@@ -334,7 +334,7 @@ public class IoBufferTest {
      */
     @Test
     public void testArrayEmptyByteBuffer() {
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
 
         byte[] array = ioBuffer.array();
         assertNotNull(array);
@@ -348,7 +348,7 @@ public class IoBufferTest {
     @Test
     public void testArrayOneByteBuffer() {
         ByteBuffer bb1 = ByteBuffer.allocate(5);
-        IoBuffer ioBuffer = new IoBuffer(bb1);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1);
 
         // Empty buffer first
         byte[] array = ioBuffer.array();
@@ -360,7 +360,7 @@ public class IoBufferTest {
         bb1.put("012".getBytes());
         bb1.flip();
 
-        ioBuffer = new IoBuffer(bb1);
+        ioBuffer = IoBuffer.wrap(bb1);
 
         assertNotNull(array);
         assertEquals(5, array.length);
@@ -373,7 +373,7 @@ public class IoBufferTest {
     @Test
     public void testArrayByteBufferNotInitialized() {
         ByteBuffer bb = ByteBuffer.allocate(3);
-        IoBuffer ioBuffer = new IoBuffer(bb);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb);
 
         byte[] array = ioBuffer.array();
         assertNotNull(array);
@@ -391,7 +391,7 @@ public class IoBufferTest {
         bb.putInt(67890);
         bb.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb);
         assertEquals(12345, ioBuffer.getInt());
         assertEquals(67890, ioBuffer.getInt());
     }
@@ -409,7 +409,7 @@ public class IoBufferTest {
         bb2.putInt(67890);
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         assertEquals(12345, ioBuffer.getInt());
         assertEquals(67890, ioBuffer.getInt());
@@ -431,7 +431,7 @@ public class IoBufferTest {
         bb2.putInt(67890);
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
         ioBuffer.order(ByteOrder.LITTLE_ENDIAN);
 
         assertEquals(12345, ioBuffer.getInt());
@@ -451,7 +451,7 @@ public class IoBufferTest {
         bb2.put(new byte[] { 0x02, 0x03, 0x04 });
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         assertEquals(0x01020304, ioBuffer.getInt());
     }
@@ -469,7 +469,7 @@ public class IoBufferTest {
         bb2.put(new byte[] { 0x02, 0x03 });
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         ioBuffer.getInt();
     }
@@ -483,7 +483,7 @@ public class IoBufferTest {
         bb.put("0123".getBytes());
         bb.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb);
 
         assertEquals('0', ioBuffer.get());
         assertEquals('1', ioBuffer.get());
@@ -515,7 +515,7 @@ public class IoBufferTest {
         bb2.put("4567".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         assertEquals('0', ioBuffer.get());
         assertEquals('1', ioBuffer.get());
@@ -557,7 +557,7 @@ public class IoBufferTest {
         bb2.put("345".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         assertEquals(6, ioBuffer.limit());
 
@@ -590,7 +590,7 @@ public class IoBufferTest {
         bb2.put("4567".getBytes());
         bb2.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
 
         // Move forward a bit
         ioBuffer.get();
@@ -616,7 +616,7 @@ public class IoBufferTest {
      */
     @Test
     public void testPositionEmptyBuffer() {
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
 
         assertEquals(0, ioBuffer.position());
     }
@@ -639,7 +639,7 @@ public class IoBufferTest {
         bb3.flip();
 
         // The resulting buffer will be seen as "0123456789"
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2, bb3);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
 
         // Iterate and check the position
         for (int i = 0; i < ioBuffer.limit(); i++) {
@@ -666,7 +666,7 @@ public class IoBufferTest {
      */
     @Test
     public void testPositionIntEmptyBuffer() {
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
 
         ioBuffer.position(0);
     }
@@ -688,7 +688,7 @@ public class IoBufferTest {
         bb3.put("89".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2, bb3);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
 
         ioBuffer.position(-1);
     }
@@ -711,7 +711,7 @@ public class IoBufferTest {
         bb3.flip();
 
         // The resulting buffer will be seen as "0123456789"
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2, bb3);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
 
         ioBuffer.position(11);
     }
@@ -734,7 +734,7 @@ public class IoBufferTest {
         bb3.flip();
 
         // The resulting buffer will be seen as "0123456789"
-        IoBuffer ioBuffer = new IoBuffer(bb1, bb2, bb3);
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
 
         // Set the position in the middle of bb2 (4-3)
         ioBuffer.position(5);
@@ -787,7 +787,7 @@ public class IoBufferTest {
         bb3.put("6789".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         ioBuffer.position(2);
@@ -809,7 +809,7 @@ public class IoBufferTest {
     public void testShort() {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putShort((short) 12345).rewind();
-            IoBuffer ioBuffer = new IoBuffer(bb).order(bo);
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
             assertEquals(3, ioBuffer.capacity());
             ioBuffer.extend(1);
             ioBuffer.position(2);
@@ -825,7 +825,7 @@ public class IoBufferTest {
     public void testInt() {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putInt(123456).rewind();
-            IoBuffer ioBuffer = new IoBuffer(bb).order(bo);
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
             assertEquals(5, ioBuffer.capacity());
             ioBuffer.extend(3);
             ioBuffer.position(4);
@@ -841,7 +841,7 @@ public class IoBufferTest {
     public void testLong() {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putLong(123456789012l).rewind();
-            IoBuffer ioBuffer = new IoBuffer(bb).order(bo);
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
             assertEquals(9, ioBuffer.capacity());
             ioBuffer.extend(7);
 
@@ -858,7 +858,7 @@ public class IoBufferTest {
     public void testChar() {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putChar('ë').rewind();
-            IoBuffer ioBuffer = new IoBuffer(bb).order(bo);
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
             
             assertEquals(3, ioBuffer.capacity());
             
@@ -888,7 +888,7 @@ public class IoBufferTest {
         bb3.put("6789".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         ioBuffer.position(2);
@@ -916,7 +916,7 @@ public class IoBufferTest {
         bb3.put("6789".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         byte seq[] = "abcdefghij".getBytes();
@@ -943,7 +943,7 @@ public class IoBufferTest {
         bb3.put("6789".getBytes());
         bb3.flip();
 
-        IoBuffer ioBuffer = new IoBuffer();
+        IoBuffer ioBuffer = IoBuffer.newInstance();
         ioBuffer.add(bb1, bb2).add(bb3);
 
         ioBuffer.position(2);


[2/5] git commit: added asInputStream in IoBuffer and some documentation

Posted by rb...@apache.org.
added asInputStream in IoBuffer and some documentation


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/68ca8151
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/68ca8151
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/68ca8151

Branch: refs/heads/trunk
Commit: 68ca8151b70a33e0bc312518d53b5ceb7142cf8d
Parents: fb03fe6
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 11:50:02 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Sun Jun 30 11:50:02 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    | 79 +++++++++++++++-----
 1 file changed, 61 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/68ca8151/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index cf127b7..a768394 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -18,6 +18,8 @@
  */
 package org.apache.mina.codec;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -32,6 +34,7 @@ import java.nio.ReadOnlyBufferException;
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public final class IoBuffer {
+
     /**
      * @see ByteBuffer#allocate(int)
      */
@@ -73,6 +76,12 @@ public final class IoBuffer {
         return wrap(ByteBuffer.wrap(array, offset, length));
     }
 
+    /**
+     * Build new IoBuffer containing 
+     * 
+     * @param buffers
+     * @return the new {@link IoBuffer}
+     */
     public static IoBuffer wrap(ByteBuffer... buffers) {
         IoBuffer ioBuffer = new IoBuffer();
         for (ByteBuffer b : buffers) {
@@ -107,8 +116,6 @@ public final class IoBuffer {
         mark = null;
     }
 
-    
-
     /**
      * Add one or more ByteBuffer to the current IoBuffer
      * 
@@ -149,6 +156,25 @@ public final class IoBuffer {
     }
 
     /**
+     * Provides an input stream which is actually reading the {@link IoBuffer}
+     * instance.
+     * <p>
+     * Further reads on the returned inputstream move the reading head of the {@link IoBuffer}
+     * instance used for it's creation</i>
+     *
+     * @return an input stream
+     */
+    public InputStream asInputStream() {
+        return new InputStream() {
+
+            @Override
+            public int read() throws IOException {
+                return hasRemaining() ? get() & 0xff : -1;
+            }
+        };
+    }
+
+    /**
      * @see ByteBuffer#asReadOnlyBuffer()
      */
     public IoBuffer asReadOnlyBuffer() {
@@ -187,6 +213,11 @@ public final class IoBuffer {
         return this;
     }
 
+    /**
+     * Returns a copy of the current {@link IoBuffer}, with an independent copy if the postion, limit and mark.
+     * 
+     * @return the copied {@link IoBuffer}
+     */
     public IoBuffer duplicate() {
         IoBuffer buffer = new IoBuffer();
 
@@ -228,6 +259,9 @@ public final class IoBuffer {
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public boolean equals(Object ob) {
         if (this == ob) {
@@ -249,6 +283,12 @@ public final class IoBuffer {
         return true;
     }
 
+    /**
+     * Extends the current IoBuffer capacity.
+     * 
+     * @param size the number of bytes to extend the current IoBuffer 
+     * @return the current {@link IoBuffer}
+     */
     public IoBuffer extend(int size) {
         ByteBuffer extension = isDirect() ? ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
         add(extension);
@@ -486,6 +526,9 @@ public final class IoBuffer {
         return out;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public int hashCode() {
         int hash = 0;
@@ -735,6 +778,17 @@ public final class IoBuffer {
     }
 
     /**
+     * @see ByteBuffer#putLong(int, int)
+     */
+    public IoBuffer putLong(int index, long value) {
+        int oldPos = position();
+        position(index);
+        putLong(value);
+        position(oldPos);
+        return this;
+    }
+
+    /**
      * @see ByteBuffer#putLong(long)
      */
     public IoBuffer putLong(long value) {
@@ -750,12 +804,12 @@ public final class IoBuffer {
     }
 
     /**
-     * @see ByteBuffer#putLong(int, int)
+     * @see ByteBuffer#putShort(int, short)
      */
-    public IoBuffer putLong(int index, long value) {
+    public IoBuffer putShort(int index, short value) {
         int oldPos = position();
         position(index);
-        putLong(value);
+        putShort(value);
         position(oldPos);
         return this;
     }
@@ -775,17 +829,6 @@ public final class IoBuffer {
     }
 
     /**
-     * @see ByteBuffer#putShort(int, short)
-     */
-    public IoBuffer putShort(int index, short value) {
-        int oldPos = position();
-        position(index);
-        putShort(value);
-        position(oldPos);
-        return this;
-    }
-
-    /**
      * @see ByteBuffer#remaining()
      */
     public int remaining() {
@@ -917,11 +960,11 @@ public final class IoBuffer {
             return position - node.getOffset();
         }
 
-        public void setNode(BufferNode node) {
+        private void setNode(BufferNode node) {
             this.node = node;
         }
 
-        public void setPosition(int position) {
+        private void setPosition(int position) {
             this.position = position;
         }
 


[3/5] git commit: fix in IoBuffer.putFloat

Posted by rb...@apache.org.
fix in IoBuffer.putFloat


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/5a48e1a5
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/5a48e1a5
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/5a48e1a5

Branch: refs/heads/trunk
Commit: 5a48e1a5b28bbee9db2fc8c8bfb90c521875d7c2
Parents: 68ca815
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 20:12:09 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Sun Jun 30 20:12:09 2013 +0200

----------------------------------------------------------------------
 codec/src/main/java/org/apache/mina/codec/IoBuffer.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/5a48e1a5/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index a768394..081da51 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -738,7 +738,7 @@ public final class IoBuffer {
      * @see ByteBuffer#putFloat(float)
      */
     public IoBuffer putFloat(float value) {
-        return putLong(Float.floatToIntBits(value));
+        return putInt(Float.floatToIntBits(value));
     }
 
     /**


[4/5] git commit: removed transitive dependency of hamcrest-core-1.1 through mockito-core

Posted by rb...@apache.org.
removed transitive dependency of hamcrest-core-1.1 through mockito-core


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/8884bafd
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/8884bafd
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/8884bafd

Branch: refs/heads/trunk
Commit: 8884bafd0d0e308fbeef0ca03af69e1cc8a208c4
Parents: 5a48e1a
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 22:09:20 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Sun Jun 30 22:09:20 2013 +0200

----------------------------------------------------------------------
 codec/pom.xml | 6 ++++++
 1 file changed, 6 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/8884bafd/codec/pom.xml
----------------------------------------------------------------------
diff --git a/codec/pom.xml b/codec/pom.xml
index 02af71e..596eb2c 100644
--- a/codec/pom.xml
+++ b/codec/pom.xml
@@ -46,6 +46,12 @@
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
       <scope>test</scope>
+      <exclusions>
+	    <exclusion>
+	      <artifactId>hamcrest-core</artifactId>
+	      <groupId>org.hamcrest</groupId>
+	    </exclusion>
+	  </exclusions>
     </dependency>
   </dependencies>
 


[5/5] git commit: improved coverage in IoBuffer, various improvements

Posted by rb...@apache.org.
improved coverage in IoBuffer, various improvements


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/fffe4b56
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/fffe4b56
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/fffe4b56

Branch: refs/heads/trunk
Commit: fffe4b5658907eabfc3d6682cadc8173f3ac8489
Parents: 8884baf
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 22:34:56 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Mon Jul 1 08:28:34 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    |  46 ++++-
 .../org/apache/mina/codec/IoBufferTest.java     | 169 ++++++++++++++++++-
 2 files changed, 199 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/fffe4b56/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index 081da51..9b659c8 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -168,6 +168,13 @@ public final class IoBuffer {
         return new InputStream() {
 
             @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                int toRead = Math.min(remaining(), len);
+                get(b, off, toRead);
+                return toRead;
+            }
+
+            @Override
             public int read() throws IOException {
                 return hasRemaining() ? get() & 0xff : -1;
             }
@@ -275,11 +282,17 @@ public final class IoBuffer {
             return false;
         }
         int p = this.position();
-        for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
-            if (this.get(i) != that.get(j)) {
+        int q = that.position();
+        while (this.hasRemaining() && that.hasRemaining()) {
+            if (this.get() != that.get()) {
+                this.position(p);
+                that.position(q);
                 return false;
             }
+
         }
+        this.position(p);
+        that.position(q);
         return true;
     }
 
@@ -697,10 +710,6 @@ public final class IoBuffer {
      * @see ByteBuffer#putChar(char)
      */
     public IoBuffer putChar(char value) {
-        if (remaining() < 2) {
-            throw new BufferUnderflowException();
-        }
-
         putShort((short) value);
         return this;
     }
@@ -888,8 +897,22 @@ public final class IoBuffer {
         return out;
     }
 
+    @Override
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append(getClass().getName());
+        sb.append("[pos=");
+        sb.append(position());
+        sb.append(" lim=");
+        sb.append(limit());
+        sb.append(" cap=");
+        sb.append(capacity());
+        sb.append("]");
+        return sb.toString();
+    }
+
     private void updatePosition() {
-        while (!position.getNode().getBuffer().hasRemaining()) {
+        while (!position.getNode().getBuffer().hasRemaining() && position.getNode().hasNext()) {
             position.setNode(position.getNode().getNext());
             position.getNode().getBuffer().rewind();
         }
@@ -970,7 +993,14 @@ public final class IoBuffer {
 
         @Override
         public String toString() {
-            return "Pointer [node=" + node + ", position=" + position + "]";
+            StringBuffer sb = new StringBuffer();
+            sb.append(getClass().getName());
+            sb.append("[node=");
+            sb.append(getNode());
+            sb.append(", pos=");
+            sb.append(getPosition());            
+            sb.append("]");
+            return sb.toString();           
         }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/fffe4b56/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
index 3c92f66..831c5ce 100644
--- a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
@@ -18,17 +18,22 @@
  */
 package org.apache.mina.codec;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.Arrays;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 /**
@@ -564,7 +569,7 @@ public class IoBufferTest {
         // Move forward a bit
         ioBuffer.get();
         ioBuffer.get();
-        
+
         ioBuffer.limit(3);
 
         // Clear
@@ -818,6 +823,23 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(12345, ioBuffer.getShort());
             assertEquals(-23456, ioBuffer.getShort());
+
+            ioBuffer.putShort(1, (short) 12345);
+            assertEquals((short) 12345, ioBuffer.getShort(1));
+
+            try {
+                ioBuffer.putShort(3, (short) 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getShort(3);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -834,6 +856,23 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(123456, ioBuffer.getInt());
             assertEquals(-23456789, ioBuffer.getInt());
+
+            ioBuffer.putInt(2, 1234567890);
+            assertEquals(1234567890, ioBuffer.getInt(2));
+
+            try {
+                ioBuffer.putInt(5, 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getInt(5);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -851,6 +890,61 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(123456789012l, ioBuffer.getLong());
             assertEquals(-23456789023l, ioBuffer.getLong());
+
+            ioBuffer.putLong(4, 1234567890);
+            assertEquals(1234567890, ioBuffer.getLong(4));
+
+            try {
+                ioBuffer.putLong(9, 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getLong(9);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+        }
+    }
+
+    @Test
+    public void testFloat() {
+        for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
+            ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putFloat(-0.68f).rewind();
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
+            assertEquals(5, ioBuffer.capacity());
+            ioBuffer.extend(3);
+            ioBuffer.position(4);
+            assertEquals(8, ioBuffer.capacity());
+            ioBuffer.putFloat(3.14f);
+            ioBuffer.rewind();
+            assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f);
+            assertEquals(3.14f, ioBuffer.getFloat(), 0.001f);
+            ioBuffer.putFloat(2, -12.34f);
+            assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f);
+        }
+    }
+
+    @Test
+    public void testDouble() {
+        for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
+            ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putDouble(Math.PI).rewind();
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
+            assertEquals(9, ioBuffer.capacity());
+            ioBuffer.extend(7);
+
+            ioBuffer.position(8);
+            assertEquals(16, ioBuffer.capacity());
+            ioBuffer.putDouble(-Math.E);
+            ioBuffer.rewind();
+            assertEquals(Math.PI, ioBuffer.getDouble(), 1E-10);
+            assertEquals(-Math.E, ioBuffer.getDouble(), 1E-10);
+
+            ioBuffer.putDouble(4, 12.34);
+            assertEquals(12.34, ioBuffer.getDouble(4), 1E-10);
         }
     }
 
@@ -859,18 +953,35 @@ public class IoBufferTest {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putChar('ë').rewind();
             IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
-            
+
             assertEquals(3, ioBuffer.capacity());
-            
+
             ioBuffer.extend(1);
             ioBuffer.order(bo);
             ioBuffer.position(2);
             assertEquals(4, ioBuffer.capacity());
             ioBuffer.putChar('ü');
             ioBuffer.rewind();
-            
+
             assertEquals('ë', ioBuffer.getChar());
             assertEquals('ü', ioBuffer.getChar());
+            
+            ioBuffer.putChar(1, 'ç');
+            assertEquals('ç', ioBuffer.getChar(1));
+            
+            try {
+                ioBuffer.putChar(3, 'ñ');
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here                
+            }
+
+            try {
+                ioBuffer.getChar(3);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -928,7 +1039,7 @@ public class IoBufferTest {
             assertEquals(expected[i], ioBuffer.get(i));
         }
     }
-    
+
     @Test
     public void testCompact() {
         ByteBuffer bb1 = ByteBuffer.allocate(5);
@@ -950,12 +1061,54 @@ public class IoBufferTest {
         ioBuffer.limit(8);
 
         ioBuffer.compact();
-        assertEquals(ioBuffer.capacity(),ioBuffer.limit());
-        assertEquals(6,ioBuffer.position());
-        
+        assertEquals(ioBuffer.capacity(), ioBuffer.limit());
+        assertEquals(6, ioBuffer.position());
+
         byte seg[] = "234567".getBytes();
         for (int i = 0; i < 6; i++) {
             assertEquals(seg[i], ioBuffer.get(i));
         }
     }
+
+    @Test
+    public void testInputStreamGetByte() throws IOException {
+        String hw = "HelloWorld";
+        IoBuffer bb = IoBuffer.wrap(hw.getBytes());
+        InputStream is = bb.asInputStream();
+        for (int i = 0; i < 10; i++) {
+            assertEquals(i, bb.position());
+            assertEquals(hw.getBytes()[i], is.read());
+        }
+        assertEquals(-1, is.read());
+    }
+
+    @Test
+    public void testInputStreamGetByteArray() throws IOException {
+        String hw = "HelloWorld";
+        IoBuffer bb = IoBuffer.wrap(hw.getBytes());
+        InputStream is = bb.asInputStream();
+        byte array[] = new byte[15];
+
+        assertEquals(5, is.read(array, 0, 5));
+        assertEquals(5, bb.position());
+        assertEquals(5, is.read(array, 5, 10));
+        assertEquals(10, bb.position());
+
+        for (int i = 0; i < 10; i++) {
+            assertEquals(hw.getBytes()[i], array[i]);
+        }
+    }
+
+    @Test
+    public void testEquals() {
+        String h = "Hello";
+        String w = "World";
+        IoBuffer hw1b = IoBuffer.wrap((h + w).getBytes());
+        IoBuffer wh1b = IoBuffer.wrap((w + h).getBytes());
+        IoBuffer hw2b = IoBuffer.newInstance();
+        hw2b.add(ByteBuffer.wrap(h.getBytes()));
+        hw2b.add(ByteBuffer.wrap(w.getBytes()));
+        assertEquals(hw2b, hw1b);
+        Assert.assertThat(wh1b, is(not(hw1b)));
+    }
 }