You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by sj...@apache.org on 2008/07/22 23:23:01 UTC

svn commit: r678912 - /mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java

Author: sjlee
Date: Tue Jul 22 14:23:01 2008
New Revision: 678912

URL: http://svn.apache.org/viewvc?rev=678912&view=rev
Log:
DIRMINA-610

Backported changes to optimize ByteBuffer.getString().

Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.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=678912&r1=678911&r2=678912&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 Tue Jul 22 14:23:01 2008
@@ -158,7 +158,7 @@
 public abstract class ByteBuffer implements Comparable {
     private static ByteBufferAllocator allocator = new PooledByteBufferAllocator();
 
-    private static boolean useDirectBuffers = true;
+    private static boolean useDirectBuffers = false;
 
     /**
      * Returns the current allocator which manages the allocated buffers.
@@ -900,44 +900,58 @@
 
         int oldPos = position();
         int oldLimit = limit();
-        int end;
+        int end = -1;
+        int newPos;
 
         if (!utf16) {
-            while (hasRemaining()) {
-                if (get() == 0) {
-                    break;
-                }
-            }
-
-            end = position();
-            if (end == oldLimit && get(end - 1) != 0) {
-                limit(end);
+            end = indexOf((byte)0x00);
+            if (end < 0) {
+                newPos = end = oldLimit;
             } else {
-                limit(end - 1);
+                newPos = end + 1;
             }
         } else {
-            while (remaining() >= 2) {
-                boolean highZero = (get() == 0);
-                boolean lowZero = (get() == 0);
-                if (highZero && lowZero) {
+            int i = oldPos;
+            for (;;) {
+                boolean wasZero = get(i) == 0;
+                i++;
+                
+                if (i >= oldLimit) {
+                    break;
+                }
+            
+                if (get(i) != 0) {
+                    i++;
+                    if (i >= oldLimit) {
+                        break;
+                    } else {
+                        continue;
+                    }
+                }
+            
+                if (wasZero) {
+                    end = i - 1;
                     break;
                 }
             }
 
-            end = position();
-            if (end == oldLimit || end == oldLimit - 1) {
-                limit(end);
+            if (end < 0) {
+                newPos = end = oldPos + (oldLimit - oldPos & 0xFFFFFFFE);
             } else {
-                limit(end - 2);
+                if (end + 2 <= oldLimit) {
+                    newPos = end + 2;
+                } else {
+                    newPos = end;
+                }
             }
         }
 
-        position(oldPos);
-        if (!hasRemaining()) {
-            limit(oldLimit);
-            position(end);
+        if (oldPos == end) {
+            position(newPos);
             return "";
         }
+        
+        limit(end);
         decoder.reset();
 
         int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
@@ -972,7 +986,7 @@
         }
 
         limit(oldLimit);
-        position(end);
+        position(newPos);
         return out.flip().toString();
     }
 
@@ -1002,7 +1016,7 @@
 
         int oldPos = position();
         int oldLimit = limit();
-        int end = position() + fieldSize;
+        int end = oldPos + fieldSize;
 
         if (oldLimit < end) {
             throw new BufferUnderflowException();
@@ -1011,34 +1025,31 @@
         int i;
 
         if (!utf16) {
-            for (i = 0; i < fieldSize; i++) {
-                if (get() == 0) {
+            for (i = oldPos; i < end; i++) {
+                if (get(i) == 0) {
                     break;
                 }
             }
 
-            if (i == fieldSize) {
+            if (i == end) {
                 limit(end);
             } else {
-                limit(position() - 1);
+                limit(i);
             }
         } else {
-            for (i = 0; i < fieldSize; i += 2) {
-                boolean highZero = (get() == 0);
-                boolean lowZero = (get() == 0);
-                if (highZero && lowZero) {
+            for (i = oldPos; i < end; i += 2) {
+                if (get(i) == 0 && get(i + 1) == 0) {
                     break;
                 }
             }
 
-            if (i == fieldSize) {
+            if (i == end) {
                 limit(end);
             } else {
-                limit(position() - 2);
+                limit(i);
             }
         }
 
-        position(oldPos);
         if (!hasRemaining()) {
             limit(oldLimit);
             position(end);
@@ -1081,6 +1092,32 @@
         position(end);
         return out.flip().toString();
     }
+    
+    private int indexOf(byte b) {
+        if (buf().hasArray()) {
+            int arrayOffset = arrayOffset();
+            int beginPos = arrayOffset + position();
+            int limit = arrayOffset + limit();
+            byte[] array = array();
+            
+            for (int i = beginPos; i < limit; i++) {
+                if (array[i] == b) {
+                    return i - arrayOffset;
+                }
+            }
+        } else {
+            int beginPos = position();
+            int limit = limit();
+            
+            for (int i = beginPos; i < limit; i++) {
+                if (get(i) == b) {
+                    return i;
+                }
+            }
+        }
+        
+        return -1;
+    }
 
     /**
      * Writes the content of <code>in</code> into this buffer using the