You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ni...@apache.org on 2005/12/18 19:20:26 UTC
svn commit: r357489 - in /directory/network/trunk/src:
java/org/apache/mina/common/ByteBuffer.java
test/org/apache/mina/common/ByteBufferTest.java
Author: niklas
Date: Sun Dec 18 10:20:18 2005
New Revision: 357489
URL: http://svn.apache.org/viewcvs?rev=357489&view=rev
Log:
Fixed bug in ByteBuffer when wrapping sub arrays (DIRMINA-146)
Modified:
directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java?rev=357489&r1=357488&r2=357489&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Sun Dec 18 10:20:18 2005
@@ -183,7 +183,7 @@
{
java.nio.ByteBuffer nioBuffer = allocate0( capacity, direct );
DefaultByteBuffer buf = allocateContainer();
- buf.init( nioBuffer );
+ buf.init( nioBuffer, true );
return buf;
}
@@ -239,8 +239,13 @@
*/
public static ByteBuffer wrap( java.nio.ByteBuffer nioBuffer )
{
+ return wrap( nioBuffer, true );
+ }
+
+ private static ByteBuffer wrap( java.nio.ByteBuffer nioBuffer, boolean clear )
+ {
DefaultByteBuffer buf = allocateContainer();
- buf.init( nioBuffer );
+ buf.init( nioBuffer, clear );
buf.setPooled( false );
return buf;
}
@@ -250,7 +255,7 @@
*/
public static ByteBuffer wrap( byte[] byteArray )
{
- return wrap( java.nio.ByteBuffer.wrap( byteArray ) );
+ return wrap( java.nio.ByteBuffer.wrap( byteArray ), false );
}
/**
@@ -261,7 +266,7 @@
*/
public static ByteBuffer wrap( byte[] byteArray, int offset, int length )
{
- return wrap( java.nio.ByteBuffer.wrap( byteArray, offset, length ) );
+ return wrap( java.nio.ByteBuffer.wrap( byteArray, offset, length ), false );
}
private static int getBufferStackIndex( Stack[] bufferStacks, int size )
@@ -1865,10 +1870,13 @@
{
}
- private synchronized void init( java.nio.ByteBuffer buf )
+ private synchronized void init( java.nio.ByteBuffer buf, boolean clear )
{
this.buf = buf;
- buf.clear();
+ if( clear )
+ {
+ buf.clear();
+ }
buf.order( ByteOrder.BIG_ENDIAN );
autoExpand = false;
pooled = true;
Modified: directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?rev=357489&r1=357488&r2=357489&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Sun Dec 18 10:20:18 2005
@@ -484,4 +484,19 @@
Assert.assertEquals( 4, buf.limit() );
Assert.assertEquals( 0x45454545, buf.getInt() );
}
+
+ public void testWrapSubArray() throws Exception
+ {
+ byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ ByteBuffer buf = ByteBuffer.wrap( array, 3, 4 );
+ Assert.assertEquals( 3, buf.position() );
+ Assert.assertEquals( 7, buf.limit() );
+ Assert.assertEquals( 10, buf.capacity() );
+
+ buf.clear();
+ Assert.assertEquals( 0, buf.position() );
+ Assert.assertEquals( 10, buf.limit() );
+ Assert.assertEquals( 10, buf.capacity() );
+ }
}