You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/03/26 06:24:30 UTC
svn commit: rev 9758 - incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber
Author: akarasulu
Date: Thu Mar 25 21:24:28 2004
New Revision: 9758
Modified:
incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java
Log:
changes for chunking
Modified: incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java
==============================================================================
--- incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java (original)
+++ incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java Thu Mar 25 21:24:28 2004
@@ -49,6 +49,8 @@
protected ArrayList tlvList = new ArrayList() ;
/** the decoder that is constructed every time */
protected BERDecoder decoder = null ;
+ /** value accumulator */
+ ByteBuffer buf = ByteBuffer.allocate(11111) ;
/*
@@ -139,6 +141,60 @@
/**
+ * Fragments a byte buffer into multiple buffer 'attempting' to keep
+ * them the same size however the last fragment will be an array
+ * bites.length%size which may or may not be of the requested fragment size.
+ * However it will never be greater. Use this method to break appart TLV
+ * bytes to feed them into the decoder for testing.
+ *
+ * @param bites the bites to fragment
+ * @param size the maximum size of a fragment
+ * @return the buffer fragment
+ */
+ public ByteBuffer[] fragment( ByteBuffer bites, int size )
+ {
+ bites = bites.duplicate() ;
+ ByteBuffer[] fragments = null ;
+
+ if ( size <= 0 )
+ {
+ throw new IllegalArgumentException(
+ "fragment size should be 1 or more but was " + size ) ;
+ }
+
+ int wholeFrags = bites.remaining()/size ;
+ int partialFragSize = bites.remaining() % size ;
+
+ /*
+ * Allocate what we need depending on the size of our remainder
+ */
+ if ( partialFragSize == 0 )
+ {
+ fragments = new ByteBuffer[wholeFrags] ;
+ }
+ else
+ {
+ fragments = new ByteBuffer[wholeFrags+1] ;
+ fragments[wholeFrags] = ByteBuffer.allocate( partialFragSize ) ;
+ }
+
+ for ( int ii = 0; ii < wholeFrags; ii++ )
+ {
+ fragments[ii] = ( ByteBuffer ) bites.slice().limit( size ) ;
+ bites.position( bites.position() + size ) ;
+ }
+
+ if ( partialFragSize != 0 )
+ {
+ fragments[wholeFrags].put( bites ) ;
+ fragments[wholeFrags].flip() ;
+ }
+
+ return fragments ;
+ }
+
+
+ /**
* BER decodes a string of 0's and 1's.
*
* @param bitString a string of ascii 0's and 1's
@@ -207,15 +263,36 @@
/**
+ * BER decodes a byte buffer.
+ *
+ * @param bites a byte buffer to decode
+ * @return a copy of the decoded tuple or the partially decoded current tlv
+ * @throws DecoderException if there are errors while decoding.
+ */
+ public Tuple decode( ByteBuffer bites ) throws DecoderException
+ {
+ int lastSize = tlvList.size() ;
+ decoder.decode( bites ) ;
+
+ if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+ {
+ return decoder.getCurrentTuple() ;
+ }
+
+ return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+ }
+
+
+ /**
* First BER encodes then BER decodes a Tuple.
*
* @param tlv a tuple to encode then decode
* @return a copy of the decoded tuple or the partially decoded current tlv
* @throws DecoderException if there are errors while decoding
*/
- public Tuple decode( Tuple tlv ) throws DecoderException
+ public Tuple decode( Tuple tlv, ByteBuffer value ) throws DecoderException
{
- ByteBuffer buf = ByteBuffer.wrap( tlv.toEncodedArray() ) ;
+ ByteBuffer buf = tlv.toEncodedBuffer( value ) ;
int lastSize = tlvList.size() ;
decoder.decode( buf ) ;
@@ -244,13 +321,14 @@
* @return a copy of the decoded tuple or the partially decoded current tlv
* @throws DecoderException if there are errors while decoding
*/
- public Tuple decode( Tuple[] tlvs ) throws DecoderException
+ public Tuple decode( Tuple[] tlvs, ByteBuffer[] values )
+ throws DecoderException
{
int lastSize = tlvList.size() ;
for ( int ii = 0; ii < tlvs.length; ii++ )
{
- decode( tlvs[ii] ) ;
+ decode( tlvs[ii], values[ii] ) ;
}
if ( tlvList.isEmpty() || tlvList.size() == lastSize )
@@ -303,15 +381,6 @@
assertTrue( decoder.getCurrentTuple().equals( tlv ) ) ;
assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
}
-
-
- /* (non-Javadoc)
- * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
- * org.apache.snickers.ber.Tuple)
- */
- public void partialValueDecoded( Tuple tlv )
- {
- }
/* (non-Javadoc)
@@ -322,8 +391,19 @@
{
assertTrue( decoder.getCurrentTuple().equals( tlv ) ) ;
assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ buf.clear() ;
}
+
+ /* (non-Javadoc)
+ * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
+ * org.apache.snickers.ber.Tuple)
+ */
+ public void partialValueDecoded( Tuple tlv )
+ {
+ buf.put( tlv.value ) ;
+ }
+
/* (non-Javadoc)
* @see org.apache.commons.codec.stateful.DecoderCallback#decodeOccurred(
@@ -334,6 +414,7 @@
Tuple t = ( Tuple ) decoded ;
tlvList.add( t.clone() ) ;
assertEquals( BERDecoderState.VALUE, this.decoder.getState() ) ;
+ buf.flip() ;
}
@@ -406,6 +487,6 @@
*/
public void warning( StatefulDecoder decoder, Exception exception )
{
- fail( ExceptionUtils.getFullStackTrace( exception ) ) ;
+ assertNotNull( exception ) ;
}
}