You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2008/05/23 04:39:42 UTC

svn commit: r659376 - /mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java

Author: trustin
Date: Thu May 22 19:39:42 2008
New Revision: 659376

URL: http://svn.apache.org/viewvc?rev=659376&view=rev
Log:
Fixed a bug that elementAs(Short|Int|Long) modifies the state of the queue

Modified:
    mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java

Modified: mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java
URL: http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java?rev=659376&r1=659375&r2=659376&view=diff
==============================================================================
--- mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java (original)
+++ mina/branches/buffer/core/src/main/java/org/apache/mina/queue/DefaultByteBufferQueue.java Thu May 22 19:39:42 2008
@@ -533,7 +533,7 @@
         }
 
         // Otherwise, read byte by byte. (inefficient!)
-        return applyByteOrder((short) readByteByByte(2));
+        return applyByteOrder((short) getByteByByte(2));
     }
 
     public int    elementAsInt   () {
@@ -545,7 +545,7 @@
         }
 
         // Otherwise, read byte by byte. (inefficient!)
-        return applyByteOrder((int) readByteByByte(4));
+        return applyByteOrder((int) getByteByByte(4));
     }
 
     public long   elementAsLong  () {
@@ -557,7 +557,7 @@
         }
 
         // Otherwise, read byte by byte. (inefficient!)
-        return applyByteOrder(readByteByByte(8));
+        return applyByteOrder(getByteByByte(8));
     }
 
     public float  elementAsFloat () {
@@ -760,6 +760,25 @@
         throw new ConcurrentModificationException();
     }
 
+    private long getByteByByte(int bytesToRead) {
+        long value = 0;
+        for (ByteBuffer b: this) {
+            int remaining = b.remaining();
+            if (remaining == 0) {
+                continue;
+            }
+
+            for (int i = 0; i < remaining; i ++) {
+                value = value << 8 | b.get(b.position() + i);
+                bytesToRead --;
+                if (bytesToRead == 0) {
+                    return value;
+                }
+            }
+        }
+        throw new ConcurrentModificationException();
+    }
+
     private long getByteByByte(int byteIndex, int bytesToRead) {
         long value = 0;
         for (ByteBuffer b: this) {