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/18 12:06:58 UTC

[1/4] git commit: DIRMINA-949 Fixed IoBuffer.Pointer.setPosition to work in O(1) instead of O(n) when moving the pointer forwards

Updated Branches:
  refs/heads/trunk ce745a543 -> 714f7bacd


DIRMINA-949 Fixed IoBuffer.Pointer.setPosition to work in O(1) instead of O(n) when moving the pointer forwards


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

Branch: refs/heads/trunk
Commit: f5f9b9d443964e530de6fd15a1519c55b4bb3c2f
Parents: ce745a5
Author: Esko Luontola <es...@gmail.com>
Authored: Wed Jul 17 18:13:22 2013 +0300
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Thu Jul 18 09:52:36 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    | 21 +-------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/f5f9b9d4/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
old mode 100644
new mode 100755
index e3ff463..e19bfcd
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -998,21 +998,6 @@ public final class IoBuffer {
             return new Pointer(getPosition());
         }
 
-        private BufferNode getBufferNodeByPosition(int pos) {
-            if (head == null) {
-                return null;
-            }
-            BufferNode currentNode = head;
-            int rpos = pos;
-            while (rpos >= currentNode.getBuffer().capacity() && currentNode.hasNext()) {
-
-                rpos -= currentNode.getBuffer().capacity();
-                currentNode = currentNode.getNext();
-            }
-
-            return currentNode;
-        }
-
         public BufferNode getNode() {
             return node;
         }
@@ -1030,11 +1015,7 @@ public final class IoBuffer {
         }
 
         public void setPosition(int newPosition) {
-            if (node == null || node.offset < newPosition) {
-
-                node = getBufferNodeByPosition(newPosition);
-
-            } else {
+            if (node == null || newPosition < node.offset) {
                 node = head;
             }
             positionInBuffer = node == null ? 0 : newPosition - node.offset;


[2/4] git commit: Refactorings and javadoc fixes

Posted by rb...@apache.org.
Refactorings and javadoc fixes


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

Branch: refs/heads/trunk
Commit: 7a525a0b1596959c0f828c6ec6c12250986a178f
Parents: f5f9b9d
Author: Esko Luontola <es...@gmail.com>
Authored: Wed Jul 17 18:30:21 2013 +0300
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Thu Jul 18 09:53:25 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    | 40 ++++++++++----------
 1 file changed, 21 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/7a525a0b/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 e19bfcd..a985060 100755
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -28,19 +28,20 @@ import java.nio.InvalidMarkException;
 import java.nio.ReadOnlyBufferException;
 
 /**
- * A proxy class used to manage ByteBuffers as if they were just a big ByteBuffer. We can add as many buffers as needed,
- * when accumulating data. From the user PoV, the methods are the very same than what we can get from ByteBuffer. <br/>
- * IoBuffer instances are *not* thread safe.
- * 
- * The IoBuffer uses a single-chained queue to handle the multiple Buffers. Thus, the sequential access is 
- * very efficient and the random access is not. It fits well the common usage of IoBuffer.
- * 
+ * A proxy class used to manage ByteBuffers as if they were just a big ByteBuffer. We can add as many buffers as needed
+ * when accumulating data. From the user point of view, the methods are the very same as ByteBuffer provides.
+ *
+ * <p>IoBuffer instances are *not* thread safe.
+ *
+ * <p>The IoBuffer uses a singly linked list to handle the multiple Buffers. Thus sequential access is
+ * very efficient and random access is not. It fits well with the common usage patterns of IoBuffer.
+ *
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public final class IoBuffer {
     private static final int BYTE_MASK = 0xff;
 
-    private static final long BYTE_MASK_L = 0xffl;
+    private static final long BYTE_MASK_L = 0xffL;
 
     /**
      * @see ByteBuffer#allocate(int)
@@ -84,9 +85,9 @@ public final class IoBuffer {
     }
 
     /**
-     * Build new IoBuffer containing 
+     * Wraps ByteBuffers into a new IoBuffer
      * 
-     * @param buffers
+     * @param buffers the ByteBuffers to wrap
      * @return the new {@link IoBuffer}
      */
     public static IoBuffer wrap(ByteBuffer... buffers) {
@@ -126,7 +127,7 @@ public final class IoBuffer {
     /**
      * Add one or more ByteBuffer to the current IoBuffer
      * 
-     * @param buffers
+     * @param buffers the ByteBuffers to add
      * @return the current {@link IoBuffer}
      */
     public IoBuffer add(ByteBuffer... buffers) {
@@ -166,8 +167,8 @@ 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>
+     * Further reads on the returned InputStream move the reading head of the {@link IoBuffer}
+     * instance used for it's creation
      *
      * @return an input stream
      */
@@ -232,7 +233,7 @@ public final class IoBuffer {
     }
 
     /**
-     * Returns a copy of the current {@link IoBuffer}, with an independent copy if the postion, limit and mark.
+     * Returns a copy of the current {@link IoBuffer}, with an independent copy of the position, limit and mark.
      * 
      * @return the copied {@link IoBuffer}
      */
@@ -580,11 +581,12 @@ public final class IoBuffer {
      * @see ByteBuffer#mark()
      */
     public void mark() {
+        // FIXME: this is broken and untested (it should set the mark instead of limit)
         this.limit = position.duplicate();
     }
 
     /**
-     * Returns the byte order used by this Iouffer when converting bytes from/to other primitive
+     * Returns the byte order used by this IoBuffer when converting bytes from/to other primitive
      * types.
      * <p>
      * The default byte order of byte buffer is always {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
@@ -600,7 +602,7 @@ public final class IoBuffer {
     /**
      * 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
+     * @param bo the byte order to set. If {@code null} then the order will be {@link ByteOrder#LITTLE_ENDIAN
      *        LITTLE_ENDIAN}.
      * @return this IoBuffer.
      * @see ByteBuffer#order(ByteOrder)
@@ -826,7 +828,7 @@ public final class IoBuffer {
     }
 
     /**
-     * @see ByteBuffer#putLong(int, int)
+     * @see ByteBuffer#putLong(int, long)
      */
     public IoBuffer putLong(int index, long value) {
         return putLong(getPointerByPosition(index), value);
@@ -933,7 +935,7 @@ public final class IoBuffer {
 
     @Override
     public String toString() {
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         sb.append(getClass().getName());
         sb.append("[pos=");
         sb.append(position());
@@ -1028,7 +1030,7 @@ public final class IoBuffer {
 
         @Override
         public String toString() {
-            StringBuffer sb = new StringBuffer();
+            StringBuilder sb = new StringBuilder();
             sb.append(getClass().getName());
             sb.append("[pos=");
             sb.append(getPosition());


[4/4] git commit: IoBuffer.Pointer.setPosition lazy as it was already in incrementPosition

Posted by rb...@apache.org.
IoBuffer.Pointer.setPosition lazy as it was already in incrementPosition


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

Branch: refs/heads/trunk
Commit: 714f7bacd3dfcd2b53726283e392fbbbb4df4407
Parents: a0d839c
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Thu Jul 18 11:58:44 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Thu Jul 18 11:58:44 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/714f7bac/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 1368620..6209184 100755
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -1008,6 +1008,7 @@ public final class IoBuffer {
         }
 
         public int getPositionInNode() {
+            updatePos();
             return positionInBuffer;
         }
 
@@ -1020,7 +1021,6 @@ public final class IoBuffer {
                 node = head;
             }
             positionInBuffer = node == null ? 0 : newPosition - node.offset;
-            updatePos();
         }
 
         public void incrementPosition(int positionIncrement) {


[3/4] git commit: DIRMINA-949 IoBuffer.mark() fix and new tests (testMark and testSetPosition)

Posted by rb...@apache.org.
DIRMINA-949 IoBuffer.mark() fix and new tests (testMark and testSetPosition)


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

Branch: refs/heads/trunk
Commit: a0d839cd503426aaf23c78a507f090a9f0703294
Parents: 7a525a0
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Thu Jul 18 09:28:09 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Thu Jul 18 09:55:07 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    |  3 +-
 .../org/apache/mina/codec/IoBufferTest.java     | 77 ++++++++++++++++++--
 2 files changed, 71 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/a0d839cd/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 a985060..1368620 100755
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -581,8 +581,7 @@ public final class IoBuffer {
      * @see ByteBuffer#mark()
      */
     public void mark() {
-        // FIXME: this is broken and untested (it should set the mark instead of limit)
-        this.limit = position.duplicate();
+        this.mark = position.duplicate();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/mina/blob/a0d839cd/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 3731cde..29e8475 100644
--- a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
@@ -31,6 +31,7 @@ import java.io.InputStream;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.nio.InvalidMarkException;
 import java.util.Arrays;
 
 import org.junit.Assert;
@@ -583,6 +584,41 @@ public class IoBufferTest {
     }
 
     /**
+     * Test the mark() method
+     */
+    @Test
+    public void testMark() {
+        ByteBuffer bb1 = ByteBuffer.allocate(4);
+        bb1.put("0123".getBytes());
+        bb1.flip();
+
+        ByteBuffer bb2 = ByteBuffer.allocate(4);
+        bb2.put("4567".getBytes());
+        bb2.flip();
+
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
+
+        ioBuffer.position(3);
+        ioBuffer.mark();
+
+        ioBuffer.position(6);
+        ioBuffer.reset();
+
+        ioBuffer.position(6);
+        ioBuffer.mark();
+
+        // we go backward, the mark should be discarded
+        ioBuffer.position(3);
+
+        try {
+            ioBuffer.reset();
+            fail("An InvalidMarkException should have been thrown");
+        } catch (InvalidMarkException ime) {
+            // 
+        }
+    }
+
+    /**
      * Test the flip() method
      */
     @Test
@@ -653,6 +689,33 @@ public class IoBufferTest {
         }
     }
 
+    /**
+     * Test set position method over a buffer
+     */
+    @Test
+    public void testSetPositionBuffer() {
+        ByteBuffer bb1 = ByteBuffer.allocate(4);
+        bb1.put("012".getBytes());
+        bb1.flip();
+
+        ByteBuffer bb2 = ByteBuffer.allocate(4);
+        bb2.put("3456".getBytes());
+        bb2.flip();
+
+        ByteBuffer bb3 = ByteBuffer.allocate(4);
+        bb3.put("789".getBytes());
+        bb3.flip();
+
+        // The resulting buffer will be seen as "0123456789"
+        IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2, bb3);
+
+        // Check with random positions
+        for (int i : new int[] { 4, 6, 7, 8, 3, 9, 1, 5, 0, 2 }) {
+            ioBuffer.position(i);
+            assertEquals('0' + i, ioBuffer.get());
+        }
+    }
+
     //-------------------------------------------------------------------------
     // Test the position(int) method
     // We will test many different cases
@@ -824,7 +887,7 @@ public class IoBufferTest {
             assertEquals(12345, ioBuffer.getShort());
             assertEquals(-23456, ioBuffer.getShort());
             ioBuffer.rewind();
-            
+
             ioBuffer.putShort(1, (short) 12345);
             assertEquals((short) 12345, ioBuffer.getShort(1));
 
@@ -858,7 +921,7 @@ public class IoBufferTest {
             assertEquals(123456, ioBuffer.getInt());
             assertEquals(-23456789, ioBuffer.getInt());
             ioBuffer.rewind();
-            
+
             ioBuffer.putInt(2, 1234567890);
             assertEquals(1234567890, ioBuffer.getInt(2));
 
@@ -893,7 +956,7 @@ public class IoBufferTest {
             assertEquals(123456789012l, ioBuffer.getLong());
             assertEquals(-23456789023l, ioBuffer.getLong());
 
-            ioBuffer.rewind();            
+            ioBuffer.rewind();
             ioBuffer.putLong(4, 1234567890);
             assertEquals(1234567890, ioBuffer.getLong(4));
 
@@ -927,7 +990,7 @@ public class IoBufferTest {
             assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f);
             assertEquals(3.14f, ioBuffer.getFloat(), 0.001f);
             ioBuffer.rewind();
-            
+
             ioBuffer.putFloat(2, -12.34f);
             assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f);
         }
@@ -948,7 +1011,7 @@ public class IoBufferTest {
             assertEquals(Math.PI, ioBuffer.getDouble(), 1E-10);
             assertEquals(-Math.E, ioBuffer.getDouble(), 1E-10);
 
-            ioBuffer.rewind();            
+            ioBuffer.rewind();
             ioBuffer.putDouble(4, 12.34);
             assertEquals(12.34, ioBuffer.getDouble(4), 1E-10);
         }
@@ -972,10 +1035,10 @@ public class IoBufferTest {
             assertEquals('ë', ioBuffer.getChar());
             assertEquals('ü', ioBuffer.getChar());
             ioBuffer.rewind();
-            
+
             ioBuffer.putChar(1, 'ç');
             assertEquals('ç', ioBuffer.getChar(1));
-            
+
             try {
                 ioBuffer.putChar(3, 'ñ');
                 fail("Not enough place on the buffer");