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 2006/11/21 03:51:59 UTC
svn commit: r477479 - in /mina/trunk/core/src:
main/java/org/apache/mina/common/ByteBuffer.java
test/java/org/apache/mina/common/ByteBufferTest.java
Author: trustin
Date: Mon Nov 20 18:51:58 2006
New Revision: 477479
URL: http://svn.apache.org/viewvc?view=rev&rev=477479
Log:
Resolved issue: DIRMINA-312 (ByteBuffer.indexOf(byte))
* Added ByteBuffer.indexOf(byte)
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=477479&r1=477478&r2=477479
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java Mon Nov 20 18:51:58 2006
@@ -1787,6 +1787,50 @@
return remaining() - prefixLength >= dataLength;
}
+ /////////////////////
+ // IndexOf methods //
+ /////////////////////
+
+ /**
+ * Returns the first occurence position of the specified byte from the current position to
+ * the current limit.
+ *
+ * @return <tt>-1</tt> if the specified byte is not found
+ */
+ public int indexOf( byte b )
+ {
+ if( 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;
+ }
+
//////////////////////////
// Skip or fill methods //
//////////////////////////
Modified: mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=477479&r1=477478&r2=477479
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java (original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java Mon Nov 20 18:51:58 2006
@@ -833,4 +833,28 @@
buf.reset();
Assert.assertEquals( 0xCDB3D0A4L, buf.getUnsignedInt() );
}
+
+ public void testIndexOf() throws Exception
+ {
+ boolean direct = false;
+ for( int i = 0; i < 2; i ++, direct = !direct )
+ {
+ ByteBuffer buf = ByteBuffer.allocate( 16, direct );
+ buf.put( ( byte ) 0x1 );
+ buf.put( ( byte ) 0x2 );
+ buf.put( ( byte ) 0x3 );
+ buf.put( ( byte ) 0x4 );
+ buf.put( ( byte ) 0x1 );
+ buf.put( ( byte ) 0x2 );
+ buf.put( ( byte ) 0x3 );
+ buf.put( ( byte ) 0x4 );
+ buf.position( 2 );
+ buf.limit( 5 );
+
+ Assert.assertEquals( 4, buf.indexOf( ( byte ) 0x1 ) );
+ Assert.assertEquals( -1, buf.indexOf( ( byte ) 0x2 ) );
+ Assert.assertEquals( 2, buf.indexOf( ( byte ) 0x3 ) );
+ Assert.assertEquals( 3, buf.indexOf( ( byte ) 0x4 ) );
+ }
+ }
}