cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r1072029 - /cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java
Date Fri, 18 Feb 2011 15:50:06 GMT
Author: jbellis
Date: Fri Feb 18 15:50:05 2011
New Revision: 1072029

URL: http://svn.apache.org/viewvc?rev=1072029&view=rev
Log:
use get, put bulk methods in ByteBufferUtil
patch by jbellis; reviewed by slebresne for CASSANDRA-2186

Modified:
    cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java

Modified: cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java?rev=1072029&r1=1072028&r2=1072029&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java
(original)
+++ cassandra/branches/cassandra-0.7/src/java/org/apache/cassandra/utils/ByteBufferUtil.java
Fri Feb 18 15:50:05 2011
@@ -129,25 +129,19 @@ public class ByteBufferUtil
      */
     public static byte[] getArray(ByteBuffer buffer)
     {
-        return getArray(buffer, buffer.position(), buffer.remaining());
-    }
+        int length = buffer.remaining();
 
-    public static byte[] getArray(ByteBuffer b, int start, int length)
-    {
-        if (b.hasArray())
+        if (buffer.hasArray())
         {
-            if (b.arrayOffset() == 0 && start == 0 && length == b.array().length)
-                return b.array();
+            int start = buffer.position();
+            if (buffer.arrayOffset() == 0 && start == 0 && length == buffer.array().length)
+                return buffer.array();
             else
-                return Arrays.copyOfRange(b.array(), start + b.arrayOffset(), start + length
+ b.arrayOffset());
+                return Arrays.copyOfRange(buffer.array(), start + buffer.arrayOffset(), start
+ length + buffer.arrayOffset());
         }
-
+        // else, DirectByteBuffer.get() is the fastest route
         byte[] bytes = new byte[length];
-
-        for (int i = 0; i < length; i++)
-        {
-            bytes[i] = b.get(start++);
-        }
+        buffer.duplicate().get(bytes);
 
         return bytes;
     }
@@ -210,8 +204,7 @@ public class ByteBufferUtil
         }
         else
         {
-            for (int i = o.position(); i < o.limit(); i++)
-                clone.put(o.get(i));
+            clone.put(o.duplicate());
             clone.flip();
         }
 
@@ -221,16 +214,9 @@ public class ByteBufferUtil
     public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset,
int length)
     {
         if (buffer.hasArray())
-        {
             System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset,
length);
-        }
         else
-        {
-            for (int i = 0; i < length; i++)
-            {
-                bytes[offset++] = buffer.get(position++);
-            }
-        }
+            ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, length);
     }
 
     public static void writeWithLength(ByteBuffer bytes, DataOutput out) throws IOException



Mime
View raw message