You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ad...@apache.org on 2004/06/11 05:26:52 UTC
svn commit: rev 21055 - in incubator/directory/snickers/branches/ber-decoder/ber-codec/src: java/org/apache/snickers java/org/apache/snickers/ber test/org/apache/snickers test/org/apache/snickers/ber
Author: adc
Date: Thu Jun 10 20:26:51 2004
New Revision: 21055
Added:
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagId.java
- copied, changed from rev 20765, incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagOctetCollector.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TupleStack.java
Removed:
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/SnickersDecoder.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderState.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/LengthDecoder.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagDecoder.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagEnum.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagOctetCollector.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TupleTreeDecoder.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/SnickersDecoderTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderStateTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/EncodeDecodeTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/LdapMessageTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
Modified:
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallback.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Length.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tag.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tuple.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TypeClass.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCaseTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderCallbackAdapterTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/ConstructedTLVTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteLengthTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteTagTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SimplePrimitiveTLVTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteLengthTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteTagTests.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TagTest.java
incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TupleTest.java
Log:
Removed Tuple buffers, pushed states into BERDecoder, removed rules.
I'll probably end up copying stuff back in this branch from main.
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallback.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallback.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallback.java Thu Jun 10 20:26:51 2004
@@ -14,52 +14,45 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
+import java.nio.ByteBuffer;
-import org.apache.commons.codec.stateful.DecoderCallback ;
+import org.apache.commons.codec.stateful.DecoderCallback;
/**
* A specialized decoder callback that handles specific BER events.
*
- * @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
* @version $Rev$
*/
public interface BERDecoderCallback extends DecoderCallback
{
/**
- * Method used to receive notification that a tag was decoded. The
- * following tag properties of the TLV tuple are valid at this point:
+ * Method used to receive notification that a TLV tuple has begun to be
+ * read. The following tag properties of the TLV tuple are valid at this
+ * point:
* <ul>
* <li>id</li>
* <li>isPrimitive</li>
* <li>typeClass</li>
+ * <li>length</li>
* </ul>
*
* @param tlv the TLV tuple
*/
- void tagDecoded( Tuple tlv ) ;
-
+ void start( Tuple tlv );
+
/**
- * Method used to receive notification that a length was decoded. The
- * following properties of the TLV tuple are valid at this point:
- * <ul>
- * <li>id</li>
- * <li>isPrimitive</li>
- * <li>typeClass</li>
- * <li>length</li>
- * </ul>
- *
- * @param tlv the TLV tuple
+ * Method used to provide the possibly partial contents of a TLV value.
+ *
+ * @param buffer the possibly partial contents of a TLV value
*/
- void lengthDecoded( Tuple tlv ) ;
-
+ void contents( ByteBuffer buffer );
+
/**
- * Method used to recieve notification that a part of the value was decoded.
- *
- * @param tlv the TLV tuple
+ * Method used to provide notification that a TLV tuple has been read.
*/
- void partialValueDecoded( Tuple tlv ) ;
+ void end();
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java Thu Jun 10 20:26:51 2004
@@ -14,51 +14,61 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
+import java.nio.ByteBuffer;
-import org.apache.commons.codec.stateful.StatefulDecoder ;
+import org.apache.commons.codec.stateful.StatefulDecoder;
/**
* A do nothing callback adapter.
*
- * @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
* @version $Rev$
*/
public class BERDecoderCallbackAdapter implements BERDecoderCallback
{
- /* (non-Javadoc)
- * @see org.apache.snickers.ber.BERDecoderCallback#tagDecoded(
- * org.apache.snickers.ber.Tuple)
+ /**
+ * Method used to receive notification that a TLV tuple has begun to be
+ * read. The following tag properties of the TLV tuple are valid at this
+ * point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * <li>length</li>
+ * </ul>
+ *
+ * @param tlv the TLV tuple
*/
- public void tagDecoded( Tuple tlv )
+ public void start( Tuple tlv )
{
}
-
- /* (non-Javadoc)
- * @see org.apache.snickers.ber.BERDecoderCallback#lengthDecoded(
- * org.apache.snickers.ber.Tuple)
+
+ /**
+ * Method used to provide the possibly partial contents of a TLV value.
+ *
+ * @param buffer the possibly partial contents of a TLV value
*/
- public void lengthDecoded( Tuple tlv )
+ public void contents( ByteBuffer buffer )
{
}
-
- /* (non-Javadoc)
- * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
- * org.apache.snickers.ber.Tuple)
+
+ /**
+ * Method used to provide notification that a TLV tuple has been read.
*/
- public void partialValueDecoded(Tuple tlv)
+ public void end()
{
}
-
- /* (non-Javadoc)
- * @see org.apache.commons.codec.stateful.DecoderCallback#decodeOccurred(
- * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Object)
+ /**
+ * Callback to deliver a fully decoded object.
+ *
+ * @param decoder the stateful decoder driving the callback
+ * @param decoded the object that was decoded
*/
public void decodeOccurred( StatefulDecoder decoder, Object decoded )
{
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Length.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Length.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Length.java Thu Jun 10 20:26:51 2004
@@ -31,9 +31,9 @@
*/
public class Length
{
- /** used to mark length as indefinate */
- public static final int INDEFINATE = -2 ;
- /** used to mark length as undefined */
+ /** used to mark length as indefinite */
+ public static final int INDEFINITE = -2 ;
+ /** used to mark length as undefined */
public static final int UNDEFINED = -1 ;
/** the end long form terminate bit flag mask */
public static final int END_MASK = 0x80 ;
@@ -44,7 +44,7 @@
private int numOctets = UNDEFINED ;
/** whether or not this length has been fixated */
private boolean isFixated = false ;
- /** a byte buffer used to collect the arriving length octets */
+ /** a byte buffer used to collect the arriving length octets */
private final ByteBuffer buf = ByteBuffer.allocate( 5 ) ;
@@ -57,8 +57,8 @@
{
return isFixated ;
}
-
-
+
+
/**
* Clears this tag's data of all bytes and values calculated so all is as it
* was when this instance was created.
@@ -70,13 +70,13 @@
numOctets = 1 ;
buf.clear() ;
}
-
-
+
+
/**
- * Fixates the data within this Length calculating all the derived
+ * Fixates the data within this Length calculating all the derived
* properties from the existing set of octets. While fixated octets
* cannot be added.
- *
+ *
* @throws DecoderException if this Length is invalid
*/
void fixate() throws DecoderException
@@ -85,8 +85,8 @@
value = getLength( buf ) ;
isFixated = true ;
}
-
-
+
+
/**
* Adds an octet to this Length component and as a side effect fixates the
* Length component if all the required length data has arrived.
@@ -96,12 +96,12 @@
void add( byte octet ) throws DecoderException
{
if ( isFixated )
- {
+ {
throw new IllegalStateException( "data added to fixated length" ) ;
}
-
+
buf.put( octet ) ;
-
+
if ( buf.position() == 1 )
{
// if its the long form
@@ -110,8 +110,8 @@
// capture number of octets we need to compute length
numOctets = octet & 0x7F ;
}
- else
- {
+ else
+ {
fixate() ;
}
}
@@ -125,8 +125,8 @@
fixate() ;
}
}
-
-
+
+
/**
* Gets the length of the value.
*
@@ -136,8 +136,8 @@
{
return value ;
}
-
-
+
+
/**
* Gets the number of octets currently in this Length component.
*
@@ -148,7 +148,7 @@
return buf.position() ;
}
-
+
/**
* Decodes the length of a value for a tlv using the Length field bytes.
*
@@ -167,7 +167,7 @@
*/
throw new DecoderException( "Length number is too large." ) ;
}
-
+
byte octet = octets.get() ;
// if we are using the short form then just return the first octet
@@ -178,7 +178,7 @@
// using the indefinate form
else if ( ( octet & 0x7F ) == 0 )
{
- return INDEFINATE ;
+ return INDEFINITE ;
}
// using the long form so we calculate the length from all octets
@@ -193,7 +193,7 @@
shift += 8 ;
}
while ( octets.hasRemaining() ) ;
-
+
return length ;
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tag.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tag.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tag.java Thu Jun 10 20:26:51 2004
@@ -14,52 +14,78 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
-import org.apache.commons.codec.DecoderException ;
+import org.apache.commons.codec.DecoderException;
/**
* The Tag component of a BER TLV Tuple.
*
* @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * Apache Directory Project</a>
* @version $Rev$
*/
public class Tag
{
- /** tag flag for the primitive/constructed bit - 0010 0000 - 0x20 */
- private static final int CONSTRUCTED_FLAG = 0x20 ;
+ /**
+ * tag flag for the primitive/constructed bit - 0010 0000 - 0x20
+ */
+ private static final int CONSTRUCTED_FLAG = 0x20;
/** tag mask for the primitive/constructed bit - 1101 1111 - 0xDF */
// private static final int CONSTRUCTED_MASK = ~CONSTRUCTED_FLAG ;
- /** tag mask for the short tag format - 0001 1111 - 0x1F */
- static final int SHORT_MASK = 0x1F ;
- /** tag mask for the long tag format - 0111 1111 - 0x7F */
- static final int LONG_MASK = 0x7F ;
- /** tag flag indicating the use of the long tag encoding form */
- private static final int LONG_FLAG = 0x80 ;
-
- /** the max id size with one tag octet */
- private static final int ONE_OCTET_IDMAX = 30 ;
- /** the max id size with two tag octets */
- private static final int TWO_OCTET_IDMAX = (1<<7)-1 ;
- /** the max id size with three tag octets */
- private static final int THREE_OCTET_IDMAX = (1<<14)-1 ;
- /** the max id size with four tag octets */
- private static final int FOUR_OCTET_IDMAX = (1<<21)-1 ;
-
- /** tag id */
- private int id = 0 ;
- /** whether or not this tag represents a primitive type */
- private boolean isPrimitive = true ;
- /** whether or not this tag has been fixated */
- private boolean isFixated = false ;
- /** the type class of this tag */
- private TypeClass typeClass = TypeClass.APPLICATION ;
- /** buffer backed by a Java int to collect the arriving tag octets */
- private final TagOctetCollector buf = new TagOctetCollector() ;
+ /**
+ * tag mask for the short tag format - 0001 1111 - 0x1F
+ */
+ static final int SHORT_MASK = 0x1F;
+ /**
+ * tag mask for the long tag format - 0111 1111 - 0x7F
+ */
+ static final int LONG_MASK = 0x7F;
+ /**
+ * tag flag indicating the use of the long tag encoding form
+ */
+ private static final int LONG_FLAG = 0x80;
+
+ /**
+ * the max id size with one tag octet
+ */
+ private static final int ONE_OCTET_IDMAX = 30;
+ /**
+ * the max id size with two tag octets
+ */
+ private static final int TWO_OCTET_IDMAX = ( 1 << 7 ) - 1;
+ /**
+ * the max id size with three tag octets
+ */
+ private static final int THREE_OCTET_IDMAX = ( 1 << 14 ) - 1;
+ /**
+ * the max id size with four tag octets
+ */
+ private static final int FOUR_OCTET_IDMAX = ( 1 << 21 ) - 1;
+
+ /**
+ * tag id
+ */
+ private int id = 0;
+ /**
+ * whether or not this tag represents a primitive type
+ */
+ private boolean isPrimitive = true;
+ /**
+ * whether or not this tag has been fixated
+ */
+ private boolean isFixated = false;
+ /**
+ * the type class of this tag
+ */
+ private TypeClass typeClass = TypeClass.APPLICATION;
+ /**
+ * buffer backed by a Java int to collect the arriving tag octets
+ */
+ private final TagId buf = new TagId();
/**
@@ -68,30 +94,32 @@
*/
void clear()
{
- id = 0 ;
- isFixated = false ;
- isPrimitive = true ;
- typeClass = TypeClass.APPLICATION ;
- buf.clear() ;
+ id = 0;
+ isFixated = false;
+ isPrimitive = true;
+ typeClass = TypeClass.APPLICATION;
+ buf.clear();
}
-
-
+
+
/**
- * Fixates the data within this Tag calculating all the derived
+ * Fixates the data within this Tag calculating all the derived
* properties from the existing set of octets. While fixated octets
* cannot be added.
- *
+ *
* @throws DecoderException if this Tag is invalid
*/
void fixate() throws DecoderException
{
- isFixated = true ;
- id = getTagId( buf ) ;
- isPrimitive = isPrimitive( buf.get( 0 ) ) ;
- typeClass = TypeClass.getTypeClass( buf.get( 0 ) ) ;
+ byte[] rawBytes = buf.toArray();
+
+ isFixated = true;
+ id = getTagId( rawBytes );
+ isPrimitive = isPrimitive( rawBytes[0] );
+ typeClass = TypeClass.getTypeClass( rawBytes[0] );
}
-
-
+
+
/**
* Adds an octet to this Tag and as a size effect may fixate the Tag if
* all the expected data has arrived.
@@ -101,34 +129,33 @@
void add( byte octet ) throws DecoderException
{
if ( isFixated )
- {
- throw new IllegalStateException( "data added to fixated tag" ) ;
+ {
+ throw new IllegalStateException( "data added to fixated tag" );
}
-
- buf.put( octet ) ;
-
+
+ buf.put( octet );
+
if ( buf.size() == 1 )
{
// if its the short form so we just fixate now!
if ( ( SHORT_MASK & octet ) != SHORT_MASK )
{
- fixate() ;
+ fixate();
}
}
-
- /*
- * From here on we're dealing with the long form of the tag. The
- * terminating octet for the long form uses a 0 for the most
- * significant bit to flag the end of the train of octets for the
- * tag id.
- */
else if ( ( octet & LONG_FLAG ) == 0 )
{
- fixate() ;
+ /*
+ * From here on we're dealing with the long form of the tag. The
+ * terminating octet for the long form uses a 0 for the most
+ * significant bit to flag the end of the train of octets for the
+ * tag id.
+ */
+ fixate();
}
}
-
-
+
+
/**
* Gets a copy of the octets composing this Tag.
*
@@ -136,10 +163,10 @@
*/
public byte[] getOctets()
{
- return buf.toArray() ;
+ return buf.toArray();
}
-
-
+
+
/**
* Gets the number of octets in this Tag.
*
@@ -147,10 +174,10 @@
*/
public int size()
{
- return buf.size() ;
+ return buf.size();
}
-
+
/**
* Gets the id.
*
@@ -158,10 +185,10 @@
*/
public int getId()
{
- return id ;
+ return id;
}
-
-
+
+
/**
* Gets the raw tag as it is stuffed into a primitive int.
*
@@ -169,22 +196,22 @@
*/
public int getRawTag()
{
- return buf.getIntValue() ;
+ return buf.getIntValue();
}
-
-
+
+
/**
- * Checks to see if the tag represented by this Tag is primitive or
+ * Checks to see if the tag represented by this Tag is primitive or
* constructed.
- *
+ *
* @return true if it is primitive, false if it is constructed
*/
public boolean isPrimitive()
{
- return isPrimitive ;
+ return isPrimitive;
}
-
-
+
+
/**
* Checks to see if the tag has been fixated.
*
@@ -192,10 +219,10 @@
*/
public boolean isFixated()
{
- return isFixated ;
+ return isFixated;
}
-
-
+
+
/**
* Gets the type class for this Tag.
*
@@ -203,7 +230,7 @@
*/
public TypeClass getTypeClass()
{
- return typeClass ;
+ return typeClass;
}
@@ -213,71 +240,66 @@
/**
- *
- *
* @param type
* @param id
* @param isConstructed
* @return
*/
- public final static int
- getIntEncodedTag( TypeClass type, int id, boolean isConstructed )
+ public final static int getIntEncodedTag( TypeClass type, int id, boolean isConstructed )
{
- int value = type.getValue() << 24 ;
+ int value = type.getValue() << 24;
if ( isConstructed )
{
- value |= ( CONSTRUCTED_FLAG << 24 ) ;
+ value |= ( CONSTRUCTED_FLAG << 24 );
}
if ( id <= ONE_OCTET_IDMAX )
{
- value |= ( id << 24 ) ;
+ value |= ( id << 24 );
}
else if ( id <= TWO_OCTET_IDMAX )
{
- value |= ( SHORT_MASK << 24 ) ;
- value |= ( id & 0x0000007F ) << 16 ;
+ value |= ( SHORT_MASK << 24 );
+ value |= ( id & 0x0000007F ) << 16;
}
else if ( id <= THREE_OCTET_IDMAX )
{
- value |= ( SHORT_MASK << 24 ) ;
- value |= ( id & 0x00003F80 ) << 9 ;
- value |= ( id & 0x0000007F ) << 8 ;
- value |= 0x00800000 ;
+ value |= ( SHORT_MASK << 24 );
+ value |= ( id & 0x00003F80 ) << 9;
+ value |= ( id & 0x0000007F ) << 8;
+ value |= 0x00800000;
}
else if ( id <= FOUR_OCTET_IDMAX )
{
- value |= ( SHORT_MASK << 24 ) ;
- value |= ( id & 0x001FC000 ) << 2 ;
- value |= ( id & 0x00003F80 ) << 1 ;
- value |= ( id & 0x0000007F ) ;
- value |= 0x00808000 ;
+ value |= ( SHORT_MASK << 24 );
+ value |= ( id & 0x001FC000 ) << 2;
+ value |= ( id & 0x00003F80 ) << 1;
+ value |= ( id & 0x0000007F );
+ value |= 0x00808000;
}
else
{
String msg = "Id argument value of " + id
+ " was greater than the maximum supported id of "
- + FOUR_OCTET_IDMAX ;
- throw new IllegalArgumentException( msg ) ;
+ + FOUR_OCTET_IDMAX;
+ throw new IllegalArgumentException( msg );
}
- return value ;
+ return value;
}
-
/**
* Gets the tag id of a TLV from tag octets.
- *
- * @param octets the set of octets needed to determine the tag value
- * (a.k.a identifier octets)
+ *
+ * @param octets the set of octets needed to determine the tag value
+ * (a.k.a identifier octets)
* @return the tag id
* @throws DecoderException if the id cannot be determined due to
- * type limitations of this method's return type.
+ * type limitations of this method's return type.
*/
- public final static int getTagId( byte[] octets )
- throws DecoderException
+ public final static int getTagId( byte[] octets ) throws DecoderException
{
if ( octets.length > 4 )
{
@@ -286,59 +308,28 @@
* we need to switch to another data type to return because after
* 4 bytes the int can no longer hold the number.
*/
- throw new DecoderException( "Tag number is too large." ) ;
+ throw new DecoderException( "Tag number is too large." );
}
-
- int id = octets[0] & SHORT_MASK ;
+
+ int id = octets[0] & SHORT_MASK;
// if bits are not all 1's then return the value which is less than 31
if ( id != SHORT_MASK && octets.length == 1 )
{
- return id ;
+ return id;
}
// clear the id now
- id = 0 ;
+ id = 0;
// calculate tag value w/ long tag format
- for( int ii = 1 ; ii < octets.length; ii++ )
- {
- int shift = ( ii - 1 ) * 7 ;
- id |= ( octets[ii] & LONG_MASK ) << shift ;
- }
-
- return id ;
- }
-
-
- /**
- * Gets the tag id of a TLV from the tag octets.
- *
- * @param octets the set of octets needed to determine the tag value
- * (a.k.a identifier octets)
- * @return the tag id
- */
- public final static int getTagId( TagOctetCollector octets )
- {
- int id = octets.get( 0 ) & SHORT_MASK ;
-
- // if bits are not all 1's then return the value which is less than 31
- if ( id != SHORT_MASK && octets.size() == 1 )
+ for ( int i = 1; i < octets.length; i++ )
{
- return id ;
- }
-
- // clear the id now
- id = 0 ;
-
- // calculate tag value w/ long tag format
- for( int ii = 1 ; ii < octets.size(); ii++ )
- {
- int shift = ( ii - 1 ) * 7 ;
- id |= ( octets.get( ii ) & LONG_MASK ) << shift ;
+ int shift = ( octets.length - i - 1 ) * 7;
+ id |= ( octets[i] & LONG_MASK ) << shift;
}
-
- return id ;
+
+ return id;
}
@@ -350,7 +341,7 @@
*/
public final static boolean isPrimitive( int octet )
{
- return ( octet & CONSTRUCTED_FLAG ) == 0 ;
+ return ( octet & CONSTRUCTED_FLAG ) == 0;
}
@@ -362,6 +353,6 @@
*/
public final static boolean isConstructed( int octet )
{
- return ( octet & CONSTRUCTED_FLAG ) == CONSTRUCTED_FLAG ;
+ return ( octet & CONSTRUCTED_FLAG ) == CONSTRUCTED_FLAG;
}
}
Copied: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagId.java (from rev 20765, incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagOctetCollector.java)
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagOctetCollector.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TagId.java Thu Jun 10 20:26:51 2004
@@ -14,29 +14,33 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
import java.nio.BufferOverflowException;
-import org.apache.commons.lang.ArrayUtils ;
+import org.apache.commons.codec.DecoderException;
/**
* Collects up to 4 tag octets.
*
* @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * Apache Directory Project</a>
* @version $Rev$
*/
-public class TagOctetCollector
+public class TagId
{
- /** the int used to store the tag octets */
- private int intValue = 0 ;
- /** the number of octets currently stored */
- private int _size = 0 ;
-
-
+ /**
+ * the store the tag octets
+ */
+ private byte[] buffer = new byte[4];
+ /**
+ * the number of octets currently stored
+ */
+ private int _size = 0;
+
+
/**
* Puts an octet into this collector.
*
@@ -44,51 +48,33 @@
*/
public void put( byte octet )
{
- switch( _size )
- {
- case(0):
- intValue = octet << 24 ;
- _size = 1 ;
- break ;
- case(1):
- intValue |= ( octet << 16 ) & 0x00FF0000 ;
- _size = 2 ;
- break ;
- case(2):
- intValue |= ( octet << 8 ) & 0x0000FF00 ;
- _size = 3 ;
- break ;
- case(3):
- intValue |= octet ;
- _size = 4 ;
- break ;
- default:
- throw new BufferOverflowException() ;
- }
+ if ( _size == 4 ) throw new BufferOverflowException();
+
+ buffer[_size++] = octet;
}
-
-
+
+
/**
* Clears all the tag octets resetting the tag and size to zero.
*/
public void clear()
{
- intValue = 0 ;
- _size = 0 ;
+ buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
+ _size = 0;
}
-
-
+
+
/**
- * Gets the number of octets stored by this TagOctetCollector
- *
+ * Gets the number of octets stored by this TagId
+ *
* @return
*/
public int size()
{
- return _size ;
+ return _size;
}
-
-
+
+
/**
* Gets a unique integer value representing the tag octets.
*
@@ -96,87 +82,40 @@
*/
public int getIntValue()
{
- return intValue ;
+ return (int) ( ( buffer[0] << 24 ) | ( buffer[1] << 16 ) | ( buffer[2] << 8 ) | buffer[3] );
}
-
-
+
+
/**
* Gets the 4 octets for the tag.
- *
+ *
* @return
*/
public byte[] toArray()
{
- byte[] octets = new byte[_size] ;
-
- switch( _size )
- {
- case(0):
- octets = ArrayUtils.EMPTY_BYTE_ARRAY ;
- break ;
- case(1):
- octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
- break ;
- case(2):
- octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
- octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
- break ;
- case(3):
- octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
- octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
- octets[2] = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
- break ;
- case(4):
- octets[0] = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
- octets[1] = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
- octets[2] = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
- octets[3] = ( byte ) ( intValue & 0x000000ff ) ;
- break ;
- default:
- throw new IllegalArgumentException(
- "Cannot support more than 4 octets" ) ;
- }
-
- return octets ;
+ byte[] octets = new byte[_size];
+
+ for ( int i = 0; i < _size; i++ ) octets[i] = buffer[i];
+
+ return octets;
}
-
-
+
+
/**
* Gets the byte at a specific index.
- *
+ *
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public byte get( int index )
{
- byte octet ;
-
if ( index >= _size )
{
- throw new IndexOutOfBoundsException( "accesing index " + index
- + " with a size of " + _size ) ;
+ throw new IndexOutOfBoundsException( "accesing index " + index + " with a size of " + _size );
}
- switch( index )
- {
- case(0):
- octet = ( byte ) ( ( intValue & 0xff000000 ) >> 24 ) ;
- break ;
- case(1):
- octet = ( byte ) ( ( intValue & 0x00ff0000 ) >> 16 ) ;
- break ;
- case(2):
- octet = ( byte ) ( ( intValue & 0x0000ff00 ) >> 8 ) ;
- break ;
- case(3):
- octet = ( byte ) ( intValue & 0x000000ff ) ;
- break ;
- default:
- throw new IllegalArgumentException(
- "Cannot support more than 4 octets" ) ;
- }
-
- return octet ;
+ return buffer[index];
}
+
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tuple.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tuple.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/Tuple.java Thu Jun 10 20:26:51 2004
@@ -14,67 +14,83 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
-import java.util.List ;
-
-import java.nio.ByteBuffer ;
-
-import org.apache.commons.lang.ArrayUtils ;
+import java.nio.ByteBuffer;
+import java.util.List;
/**
- * TLV Tuple used by the value chunking decoder. Because the length field is
- * a primitive int it's maximum value is 2,147,483,647 a single TLV's tuple
+ * TLV Tuple used by the value chunking decoder. Because the length field is
+ * a primitive int it's maximum value is 2,147,483,647 a single TLV's tuple
* cannot have a length over this amount or a value size over 2 GB.
- *
- * @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory Project</a>
* @version $Rev$
*/
-public class Tuple
+public class Tuple
{
- /** empty buffer reused for handling null */
- private static final ByteBuffer EMPTY_BUFFER =
- ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
-
- /** mask for bit 5 with 0-based index */
- private static final int BIT_5 = 0x20 ;
- /** mask for bit 6 with 0-based index */
- private static final int BIT_6 = 0x40 ;
- /** mask for bit 7 with 0-based index */
- private static final int BIT_7 = 0x80 ;
-
- /** precalculated left shift of 1 by 14 places */
- private static final int BIT_13 = 1 << 14 ;
- /** precalculated left shift of 1 by 16 places */
- private static final int BIT_15 = 1 << 16 ;
- /** precalculated left shift of 1 by 21 places */
- private static final int BIT_20 = 1 << 21 ;
- /** precalculated left shift of 1 by 24 places */
- private static final int BIT_23 = 1 << 24 ;
- /** precalculated left shift of 1 by 28 places */
- private static final int BIT_27 = 1 << 28 ;
-
- /** the raw tag data */
- int rawTag = 0 ;
- /** the tag id for this TLV tuple */
- int id = 0 ;
- /** the flag for whether or not this TLV is constructed or primitive */
- boolean isPrimitive = true ;
- /** the type class for this TLV */
- TypeClass typeClass = TypeClass.APPLICATION ;
- /** the length for this TLV tuple */
- int length = 0 ;
- /** the present value chunk buffer read for this TLV tuple */
- ByteBuffer valueChunk = EMPTY_BUFFER ;
-
- /** tlv byte index */
- int index = Length.UNDEFINED ;
- /** tlv value index for how far into the value we have read */
- int valueIndex = Length.UNDEFINED ;
-
+ /**
+ * mask for bit 5 with 0-based index
+ */
+ private static final int BIT_5 = 0x20;
+ /**
+ * mask for bit 6 with 0-based index
+ */
+ private static final int BIT_6 = 0x40;
+ /**
+ * mask for bit 7 with 0-based index
+ */
+ private static final int BIT_7 = 0x80;
+
+ /**
+ * precalculated left shift of 1 by 14 places
+ */
+ private static final int BIT_13 = 1 << 14;
+ /**
+ * precalculated left shift of 1 by 16 places
+ */
+ private static final int BIT_15 = 1 << 16;
+ /**
+ * precalculated left shift of 1 by 21 places
+ */
+ private static final int BIT_20 = 1 << 21;
+ /**
+ * precalculated left shift of 1 by 24 places
+ */
+ private static final int BIT_23 = 1 << 24;
+ /**
+ * precalculated left shift of 1 by 28 places
+ */
+ private static final int BIT_27 = 1 << 28;
+
+ /**
+ * the raw tag data
+ */
+ int rawTag = 0;
+ /**
+ * the tag id for this TLV tuple
+ */
+ int id = 0;
+ /**
+ * the flag for whether or not this TLV is constructed or primitive
+ */
+ boolean isPrimitive = true;
+ /**
+ * the type class for this TLV
+ */
+ TypeClass typeClass = TypeClass.APPLICATION;
+ /**
+ * the length for this TLV tuple
+ */
+ int length = 0;
+
+ /**
+ * tlv byte index
+ */
+ int index = 0;
+
// ------------------------------------------------------------------------
// C O N S T R U C T O R S
@@ -84,90 +100,87 @@
/**
* Empty do nothing tuple.
*/
- Tuple()
+ Tuple()
{
}
-
-
+
+
/**
* Creates constructed application type tlv tuples. Constructed TLV's with
* a definate length will use this constructor predominantly. The TypeClass
* defualts to APPLICATION.
- *
- * @param id the tag id of the tlv
- * @param length the length of the value which is the length of all the
- * nested tuples.
+ *
+ * @param id the tag id of the tlv
+ * @param length the length of the value which is the length of all the
+ * nested tuples.
*/
public Tuple( int id, int length )
{
- this( id, length, TypeClass.APPLICATION ) ;
+ this( id, length, TypeClass.APPLICATION );
}
-
-
+
+
/**
* Creates constructed application type tlv tuples. Constructed TLV's with
* a definate length will use this constructor predominantly.
- *
- * @param id the tag id of the tlv
- * @param length the length of the value which is the length of all the
- * nested tuples.
+ *
+ * @param id the tag id of the tlv
+ * @param length the length of the value which is the length of all the
+ * nested tuples.
* @param typeClass the type class of this tlv tuple
*/
public Tuple( int id, int length, TypeClass typeClass )
{
- this.id = id ;
- this.length = length ;
- valueChunk = EMPTY_BUFFER ;
- isPrimitive = false ;
-
+ this.id = id;
+ this.length = length;
+ isPrimitive = false;
+
if ( typeClass != null )
{
- this.typeClass = typeClass ;
+ this.typeClass = typeClass;
}
}
-
-
+
+
/**
* Creates constructed application type tlv tuples. Constructed TLV's with
* a definate length will use this constructor predominantly.
- *
- * @param id the tag id of the tlv
- * @param length the length of the value which is the length of all the
- * nested tuples.
+ *
+ * @param id the tag id of the tlv
+ * @param length the length of the value which is the length of all the
+ * nested tuples.
* @param isPrimitive whether or not this Tuple is primitive or constructed
- * @param typeClass the type class of this tlv tuple
+ * @param typeClass the type class of this tlv tuple
*/
public Tuple( int id, int length, boolean isPrimitive, TypeClass typeClass )
{
- this.id = id ;
- this.length = length ;
- valueChunk = EMPTY_BUFFER ;
- isPrimitive = false ;
-
+ this.id = id;
+ this.length = length;
+ isPrimitive = false;
+
if ( typeClass != null )
{
- this.typeClass = typeClass ;
+ this.typeClass = typeClass;
}
}
-
-
+
+
/**
* Creates a tuple where the length is indefinate. The tuple according to
* the BER encoding must be of the constructed type.
- *
- * @param id the tag id of the tlv
+ *
+ * @param id the tag id of the tlv
* @param typeClass the type class for the tlv
*/
public Tuple( int id, TypeClass typeClass )
{
- this.id = id ;
- this.isPrimitive = false ;
- valueChunk = EMPTY_BUFFER ;
- length = Length.INDEFINATE ;
-
+ this.id = id;
+ this.isPrimitive = false;
+ length = Length.INDEFINITE;
+
if ( typeClass != null )
{
- this.typeClass = typeClass ;
+ this.typeClass = typeClass;
}
}
@@ -184,10 +197,10 @@
*/
public int getId()
{
- return id ;
+ return id;
}
-
-
+
+
/**
* Gets the raw tag as it is stuffed into a primitive int.
*
@@ -195,48 +208,48 @@
*/
public int getRawTag()
{
- return rawTag ;
+ return rawTag;
}
-
+
/**
- * Gets the raw tag with the primitive/constructed flag dubbed out.
+ * Gets the raw tag with the primitive/constructed flag dubbed out.
* Effectively this makes every tag appear primitive and is done
- * to remove encoding ambiguities that could interfere with pattern
+ * to remove encoding ambiguities that could interfere with pattern
* matching.
- *
+ *
* @return the raw tag with the primitive/constructed flag dubbed out
*/
public int getRawPrimitiveTag()
{
- return rawTag & 0xDFFFFFFF ;
+ return rawTag & 0xDFFFFFFF;
}
-
+
/**
- * Get's whether or not this tuples's length is indefinate.
+ * Get's whether or not this tuples's length is indefinite.
*
- * @return whether or not this tuple's length is indefinate
+ * @return whether or not this tuple's length is indefinite
*/
- public boolean isIndefinate()
+ public boolean isIndefinite()
{
- return length == Length.INDEFINATE ;
+ return length == Length.INDEFINITE;
}
-
+
/**
- * Get's whether or not this tuple terminates an indefinate constructed
+ * Get's whether or not this tuple terminates an indefinite constructed
* tuple. This means that length == 0 && isPrimitive = true && id == 0
* and the type class is universal.
*
* @return whether or not this node's length is indefinate
*/
- public boolean isIndefinateTerminator()
+ public boolean isIndefiniteTerminator()
{
- return isPrimitive && ( id + length ) == 0 &&
- typeClass.equals( TypeClass.UNIVERSAL ) ;
+ return isPrimitive && ( id + length ) == 0 &&
+ typeClass.equals( TypeClass.UNIVERSAL );
}
-
+
/**
* Gets whether or not this TLV tuple is primitive or constructed.
@@ -245,10 +258,10 @@
*/
public boolean isPrimitive()
{
- return isPrimitive ;
+ return isPrimitive;
}
-
+
/**
* Gets the value length for this TLV Tuple.
*
@@ -256,10 +269,10 @@
*/
public int getLength()
{
- return length ;
+ return length;
}
-
+
/**
* Gets the BER TLV TypeClass for this TLV Tuple.
*
@@ -267,21 +280,10 @@
*/
public TypeClass getTypeClass()
{
- return typeClass ;
+ return typeClass;
}
-
- /**
- * Gets the last chunk read for the value field (V-part) for this TLV Tuple.
- *
- * @return the last valueChunk field for this TLV Tuple
- */
- public ByteBuffer getLastValueChunk()
- {
- return valueChunk ;
- }
-
-
+
/**
* Gets the total size of this TLV tuple in bytes. This includes the
* length of the tag field, the length of the length field and the length
@@ -291,17 +293,26 @@
*/
public int size()
{
- if ( this.length == Length.INDEFINATE )
- {
- return getTagLength() + getLengthLength() ;
+ if ( this.length == Length.INDEFINITE )
+ {
+ return getTagLength() + getLengthLength();
}
else
{
- return getTagLength() + getLengthLength() + length ;
+ return getTagLength() + getLengthLength() + length;
}
}
-
-
+
+ public int remaining()
+ {
+ return size() - index;
+ }
+
+ public void update( int amount )
+ {
+ index += amount;
+ }
+
// ------------------------------------------------------------------------
// Utility methods and java.lang.Object overrides
// ------------------------------------------------------------------------
@@ -312,17 +323,15 @@
*/
void clear()
{
- this.id = 0 ;
- this.index = 0 ;
- this.rawTag = 0 ;
- this.isPrimitive = true ;
- this.length = Length.UNDEFINED ;
- this.typeClass = TypeClass.APPLICATION ;
- this.valueChunk = EMPTY_BUFFER ;
- this.valueIndex = Length.UNDEFINED ;
+ this.id = 0;
+ this.index = 0;
+ this.rawTag = 0;
+ this.isPrimitive = true;
+ this.length = Length.UNDEFINED;
+ this.typeClass = TypeClass.APPLICATION;
}
-
+
/**
* Does not take into account the value, index or the valueIndex values when
* checking for equality. Technically if both are being constructed by
@@ -337,251 +346,138 @@
{
if ( o == this )
{
- return true ;
+ return true;
}
-
+
if ( o instanceof Tuple )
{
- Tuple t = ( Tuple ) o ;
-
+ Tuple t = (Tuple) o;
+
if ( t.id != id )
{
- return false ;
+ return false;
}
-
+
if ( t.isPrimitive != isPrimitive )
{
- return false ;
+ return false;
}
-
+
if ( t.length != length )
{
- return false ;
+ return false;
}
-
+
if ( t.typeClass != typeClass )
{
- return false ;
+ return false;
}
-
- return true ;
+
+ return true;
}
-
- return false ;
- }
-
- /*
- * (non-Javadoc)
- * @see java.lang.Object#clone()
- */
- public Object clone()
- {
- Tuple t = new Tuple() ;
- t.id = id ;
- t.rawTag = rawTag ;
- t.isPrimitive = isPrimitive ;
- t.typeClass = typeClass ;
- t.length = length ;
-
- /* ------------------------------------------------------------------ +/
- * @todo figure out if we should remove this section
- *
- * Do think we need this anymore since the last valueChunk does not
- * factor into the result returned by the equals(Object) method?
- * ------------------------------------------------------------------ */
- ByteBuffer bb = ( ByteBuffer ) valueChunk ;
- ByteBuffer cloned = ByteBuffer.allocate( bb.capacity() ) ;
- int oldPos = bb.position() ;
- bb.rewind() ;
- cloned.put( bb ) ;
- cloned.limit( bb.limit() ) ;
- bb.position( oldPos ) ;
- cloned.rewind() ;
- t.valueChunk = cloned ;
- /* ------------------------------------------------------------------ */
-
- t.index = index ;
- t.valueIndex = valueIndex ;
-
- return t ;
+ return false;
}
-
-
+
+
// ------------------------------------------------------------------------
// Tuple encoding operations
// ------------------------------------------------------------------------
/**
+ * @return partial encoded image if constructed or complete TLV if primitive
* @todo this should produce chunking output and needs to be removed from
* here actually and made into a standalone encoder. You give it a buffer
* and it fills it as much as it can remembering where the encode stopped.
* Hence it is stateful as expected from the statemachine.
- *
- * If this is a primitive TLV then the valueBytes argument is used to
+ * <p/>
+ * If this is a primitive TLV then the valueBytes argument is used to
* produce an encoded image of this TLV. If it is constructed then
* only the TL part of the tuple is encoded leaving the value to be encoded
* by the set of child TLVs.
- *
- * @return partial encoded image if constructed or complete TLV if primitive
*/
public ByteBuffer toEncodedBuffer( List valueChunks )
{
- ByteBuffer octets = null ;
- int tagLength = getTagLength() ;
- int lengthLength = getLengthLength() ;
- int total = tagLength + lengthLength ;
-
+ ByteBuffer octets = null;
+ int tagLength = getTagLength();
+ int lengthLength = getLengthLength();
+ int total = tagLength + lengthLength;
+
if ( isPrimitive )
{
- total += length ;
+ total += length;
}
-
- octets = ByteBuffer.allocate( total ) ;
- setTag( octets, tagLength ) ;
- setLength( octets, lengthLength ) ;
-
+
+ octets = ByteBuffer.allocate( total );
+ setTag( octets, tagLength );
+ setLength( octets, lengthLength );
+
if ( isPrimitive )
{
for ( int ii = 0; ii < valueChunks.size(); ii++ )
- {
- octets.put( ( ByteBuffer ) valueChunks.get(ii) ) ;
+ {
+ octets.put( (ByteBuffer) valueChunks.get( ii ) );
}
}
-
- return ( ByteBuffer ) octets.flip() ;
+
+ return (ByteBuffer) octets.flip();
}
-
-
+
+
/**
* Sets the tag section within the buffer.
- *
- * @param octets the buffer to set the tag in
+ *
+ * @param octets the buffer to set the tag in
* @param tagLength the length of the tag section
*/
void setTag( ByteBuffer octets, int tagLength )
{
- octets.put( ( byte ) typeClass.getValue() ) ;
-
- if ( ! isPrimitive )
- {
- octets.put( 0, ( byte ) ( octets.get( 0 ) | BIT_5 ) ) ;
- }
-
- if ( tagLength == 1 )
- {
- octets.put( 0, ( byte ) ( octets.get( 0 ) | id ) ) ;
- return ;
- }
-
- octets.put( 0, ( byte ) ( octets.get( 0 ) | Tag.SHORT_MASK ) ) ;
-
- if ( tagLength >= 2 )
- {
- octets.put( ( byte ) ( ( int ) 0x7f & id ) ) ;
-
- if ( tagLength > 2 )
- {
- octets.put( 1, ( byte ) ( octets.get( 1 ) | BIT_7 ) ) ;
- }
- }
- else
+ if ( tagLength >= 6 )
{
- return ;
- }
-
- /*
- * Take bits [8-14] (1 based bit indexing) inclusive in id and set the
- * value for the second byte to this when shifted down 7 positions so
- * we need the following mask:
- *
- * 0011 1111 1000 0000 => 0x3f80
- *
- */
- if ( tagLength >= 3 )
- {
- octets.put( ( byte ) ( ( ( int ) 0x3f80 & id ) >> 7 ) ) ;
-
- if ( tagLength > 3 )
- {
- octets.put( 2, ( byte ) ( octets.get( 2 ) | BIT_7 ) ) ;
- }
+ throw new IllegalArgumentException( "cannot support id's as large as " + id + " unless we start using longs for the id" ) ;
}
- else
+
+ octets.put( (byte) typeClass.getValue() );
+
+ if ( !isPrimitive )
{
- return ;
- }
-
- /*
- * Take bits [15-21] (1 based bit indexing) inclusive in id and set the
- * value for the second byte to this when shifted down 14 positions so
- * we need the following mask:
- *
- * 0001 1111 1100 0000 0000 0000 => 0x1fc000
- *
- */
- if ( tagLength >= 4 )
- {
- octets.put( ( byte ) ( ( ( int ) 0x1fc000 & id ) >> 14 ) ) ;
-
- if ( tagLength > 4 )
- {
- octets.put( 3, ( byte ) ( octets.get( 3 ) | BIT_7 ) ) ;
- }
+ octets.put( 0, (byte) ( octets.get( 0 ) | BIT_5 ) );
}
- else
+
+ if ( tagLength == 1 )
{
- return ;
- }
-
- /*
- * Take bits [22-28] (1 based bit indexing) inclusive in id and set the
- * value for the second byte to this when shifted down 21 positions so
- * we need the following mask:
- *
- * 0000 1111 1110 0000 0000 0000 0000 0000 => 0x0fe00000
- *
- */
- if ( tagLength >= 5 )
- {
- octets.put( ( byte ) ( ( ( int ) 0x0fe00000 & id ) >> 21 ) ) ;
-
- if ( tagLength > 5 )
- {
- octets.put( 4, ( byte ) ( octets.get( 4 ) | BIT_7 ) ) ;
- }
+ octets.put( 0, (byte) ( octets.get( 0 ) | id ) );
+ return;
}
- else
+
+ octets.put( 0, (byte) ( octets.get( 0 ) | Tag.SHORT_MASK ) );
+
+ for ( int i = 0; i < tagLength-1; i++ )
{
- return ;
- }
-
- if ( tagLength >= 6 )
- {
- throw new IllegalArgumentException( "cannot support id's as large "
- + "as " + id + " unless we start using longs for the id" ) ;
+ int shift = ( tagLength - i - 2 ) * 7;
+ octets.put( (byte) ( id >> shift & Tag.LONG_MASK | ( shift == 0 ? 0 : BIT_7 ) ) );
}
}
/**
* Sets the length bytes.
- *
- * @param octets the byte [] to set length in
+ *
+ * @param octets the byte [] to set length in
* @param lengthBytes the number bytes for the length section
*/
void setLength( ByteBuffer octets, int lengthBytes )
{
- if ( length == Length.INDEFINATE )
+ if ( length == Length.INDEFINITE )
{
- octets.put( ( byte ) BIT_7 ) ;
- return ;
+ octets.put( (byte) BIT_7 );
+ return;
}
else if ( lengthBytes == 1 )
{
- octets.put( ( byte ) length ) ;
- return ;
+ octets.put( (byte) length );
+ return;
}
else
{
@@ -594,50 +490,50 @@
* N which is set as the last 7 bits of the first octet of the L
* field.
*/
- octets.put( ( byte ) ( BIT_7 | ( lengthBytes - 1 ) ) ) ;
+ octets.put( (byte) ( BIT_7 | ( lengthBytes - 1 ) ) );
}
-
+
if ( lengthBytes >= 2 )
{
- octets.put( ( byte ) ( 0xff & length ) ) ;
+ octets.put( (byte) ( 0xff & length ) );
}
else
{
- return ;
+ return;
}
-
+
if ( lengthBytes >= 3 )
{
- octets.put( ( byte ) ( ( 0xff00 & length ) >> 8 ) ) ;
+ octets.put( (byte) ( ( 0xff00 & length ) >> 8 ) );
}
else
{
- return ;
+ return;
}
-
+
if ( lengthBytes >= 4 )
{
- octets.put( ( byte ) ( ( 0xff0000 & length ) >> 16 ) ) ;
+ octets.put( (byte) ( ( 0xff0000 & length ) >> 16 ) );
}
else
{
- return ;
+ return;
}
-
+
if ( lengthBytes >= 5 )
{
- octets.put( ( byte ) ( ( 0xff000000 & length ) >> 24 ) ) ;
+ octets.put( (byte) ( ( 0xff000000 & length ) >> 24 ) );
}
else
{
- return ;
+ return;
}
-
+
if ( lengthBytes >= 6 )
- {
+ {
throw new IllegalArgumentException( "cannot support lengths as "
- + "large as " + length
- + " unless we start using longs for the length" ) ;
+ + "large as " + length
+ + " unless we start using longs for the length" );
}
}
@@ -651,31 +547,31 @@
{
if ( id < 31 )
{
- return 1 ;
+ return 1;
}
else if ( id < BIT_6 )
{
- return 2 ;
+ return 2;
}
-
+
else if ( id < BIT_13 )
{
- return 3 ;
+ return 3;
}
else if ( id < BIT_20 )
{
- return 4 ;
+ return 4;
}
else if ( id < BIT_27 )
{
- return 5 ;
+ return 5;
}
-
- throw new IllegalArgumentException( "cannot support id's larger than "
- + id + " unless we start using longs for the id" ) ;
+
+ throw new IllegalArgumentException( "cannot support id's larger than "
+ + id + " unless we start using longs for the id" );
}
-
-
+
+
/**
* Gets the length in bytes of the length section of this TLV Tuple.
*
@@ -683,37 +579,37 @@
*/
public int getLengthLength()
{
- if ( length == Length.INDEFINATE )
+ if ( length == Length.INDEFINITE )
{
- return 1 ;
+ return 1;
}
-
+
if ( length < 0 )
{
- throw new IllegalArgumentException( "integer overflow makes id "
- + "negative with a value of " + id
+ throw new IllegalArgumentException( "integer overflow makes id "
+ + "negative with a value of " + id
+ " - unless we start using longs for"
- + " the id there you've hit a limitation" ) ;
+ + " the id there you've hit a limitation" );
}
else if ( length < BIT_7 )
{
- return 1 ;
+ return 1;
}
else if ( length < 256 )
{
- return 2 ;
+ return 2;
}
else if ( length < BIT_15 )
{
- return 3 ;
+ return 3;
}
else if ( length < BIT_23 )
{
- return 4 ;
+ return 4;
}
- else
+ else
{
- return 5 ;
+ return 5;
}
}
}
Added: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TupleStack.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TupleStack.java Thu Jun 10 20:26:51 2004
@@ -0,0 +1,243 @@
+/**
+ *
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.snickers.ber;
+
+import java.nio.ByteBuffer;
+import java.util.Stack;
+
+import org.apache.commons.codec.stateful.DecoderCallback;
+import org.apache.commons.codec.stateful.DecoderMonitor;
+import org.apache.commons.codec.stateful.DecoderMonitorAdapter;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+class TupleStack
+{
+ /**
+ * this decoder's callback
+ */
+ private DecoderCallback callback = new BERDecoderCallbackAdapter();
+
+ /**
+ * this decoder's callback
+ */
+ private BERDecoderCallback BERcallback = new BERDecoderCallbackAdapter();
+
+ /**
+ * the monitor used by this decoder
+ */
+ private DecoderMonitor monitor = new DecoderMonitorAdapter();
+
+ /**
+ * a reference to the stack's decoder which is used used to report status
+ * with monitor
+ */
+ private final BERDecoder decoder;
+
+ /**
+ * internal stack used to track incomplete tuples
+ */
+ private final Stack stack = new Stack();
+
+
+ /**
+ * Constructs a tuple stack
+ *
+ * @param decoder the stack's decoder which is used used to report status
+ * with monitor
+ */
+ TupleStack( BERDecoder decoder )
+ {
+ this.decoder = decoder;
+ }
+
+ /**
+ * Obtain the stack's DecoderCallback
+ *
+ * @return the stack's DecoderCallback
+ */
+ DecoderCallback getCallback()
+ {
+ return callback;
+ }
+
+ /**
+ * Set the stack's DecoderCallback
+ *
+ * @param callback the stack's DecoderCallback
+ */
+ void setCallback( DecoderCallback callback )
+ {
+ this.callback = callback;
+
+ if ( callback instanceof BERDecoderCallback ) BERcallback = (BERDecoderCallback) callback;
+ }
+
+ /**
+ * Obtain the stack's DecoderMonitor
+ *
+ * @return the stack's DecoderMonitor
+ */
+ DecoderMonitor getMonitor()
+ {
+ return monitor;
+ }
+
+ /**
+ * Set the stack's DecoderMonitor
+ *
+ * @param monitor the stack's DecoderMonitor
+ */
+ void setMonitor( DecoderMonitor monitor )
+ {
+ this.monitor = monitor;
+ }
+
+ /**
+ * Take a peek at the incomplete TLV that is on the top of the stack
+ *
+ * @return the incomplete TLV that is on the top of the stack
+ */
+ Tuple peek()
+ {
+ return (Tuple) stack.peek();
+ }
+
+ /**
+ * Method used tp push an incomplete TLV that is on the top of the stack.
+ * This TLV will be managed by the stack and events about its condition
+ * will be sent by this stack. <p/>
+ * <p/>
+ * The following tag properties of the TLV tuple are valid at this
+ * point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * <li>length</li>
+ * </ul>
+ *
+ * @param tlv
+ */
+ void push( Tuple tlv )
+ {
+
+ if ( tlv.isIndefiniteTerminator() )
+ {
+ Tuple itlv = (Tuple) stack.pop();
+
+ if ( !itlv.isIndefinite() )
+ {
+ /**
+ * Since this TLV is the indefinite terminator, the top of the
+ * stack should be its indefinite pair.
+ */
+ IllegalStateException ise = new IllegalStateException( "No matching TAG for indefinite terminator" );
+ monitor.fatalError( decoder, ise );
+ throw ise;
+ }
+
+ /**
+ * Original indefinite events
+ */
+ if ( BERcallback != null ) BERcallback.end();
+ callback.decodeOccurred( decoder, itlv );
+ }
+ else
+ {
+ if ( tlv.isIndefinite() )
+ {
+ /**
+ * Since this TLV is indefinite, all remaing TLVs on the stack
+ * must be constructive and indefinite.
+ */
+ for ( int i = stack.size() - 1; i >= 0; i-- )
+ {
+ Tuple stlv = (Tuple) stack.get( i );
+
+ if ( !stlv.isIndefinite() )
+ {
+ IllegalStateException ise = new IllegalStateException( "Containing constructive TLV not indefinite" );
+ monitor.fatalError( decoder, ise );
+ throw ise;
+ }
+ }
+ }
+
+ if ( BERcallback != null ) BERcallback.start( tlv );
+
+ stack.push( tlv );
+
+ received( tlv.getTagLength() );
+ received( tlv.getLengthLength() );
+ }
+ }
+
+ /**
+ * Update the contents of the incomplete TLV that is on the top of the
+ * stack. One side effect of this method is that elements that are on
+ * the top of the stack and complete are popped off after the proper
+ * notifications are sent.
+ *
+ * @param buffer the NIO byte buffer
+ */
+ void contents( ByteBuffer buffer )
+ {
+ if ( BERcallback != null ) BERcallback.contents( buffer.slice() );
+
+ received( buffer.remaining() );
+ }
+
+ /**
+ * This protected method manages the stack and pops off completed TLVs
+ * after the proper notifications are sent.
+ *
+ * @param amount
+ */
+ protected void received( int amount )
+ {
+ for ( int i = stack.size() - 1; i >= 0; i-- )
+ {
+ Tuple tlv = (Tuple) stack.get( i );
+
+ tlv.update( amount );
+
+ if ( !tlv.isIndefinite() )
+ {
+ if ( tlv.remaining() == 0 )
+ {
+ if ( tlv != stack.pop() )
+ {
+ /**
+ * All prior TLVs on the stack should have been popped
+ * and so, this should be the top element.
+ */
+ IllegalStateException ise = new IllegalStateException( "Complete TLV is not on top of stack" );
+ monitor.fatalError( decoder, ise );
+ throw ise;
+ }
+
+ callback.decodeOccurred( decoder, tlv );
+ if ( BERcallback != null ) BERcallback.end();
+ }
+ }
+ }
+ }
+
+}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TypeClass.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TypeClass.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/TypeClass.java Thu Jun 10 20:26:51 2004
@@ -14,68 +14,81 @@
* limitations under the License.
*
*/
-package org.apache.snickers.ber ;
+package org.apache.snickers.ber;
-import java.util.Map ;
-import java.util.List ;
+import java.util.List;
+import java.util.Map;
-import org.apache.commons.lang.enum.EnumUtils ;
-import org.apache.commons.lang.enum.ValuedEnum ;
+import org.apache.commons.lang.enum.EnumUtils;
+import org.apache.commons.lang.enum.ValuedEnum;
/**
- * Type safe enum for an ASN.1 type class. This can be take one of the
- * following four values:
+ * Type safe enum for an ASN.1 type class. This can be take one of the
+ * following four values:
* <ul>
* <li>UNIVERSAL</li>
* <li>APPLICATION</li>
* <li>CONTEXT_SPECIFIC</li>
* <li>PRIVATE</li>
* </ul>
- *
+ *
* @author <a href="mailto:directory-dev@incubator.apache.org">
- * Apache Directory Project</a>
+ * Apache Directory Project</a>
* @version $Rev$
*/
public class TypeClass extends ValuedEnum
{
- /** value for the universal type class */
- public static final int UNIVERSAL_VAL = 0 ;
- /** value for the application type class */
- public static final int APPLICATION_VAL = 0x40 ;
- /** value for the context specific type class */
- public static final int CONTEXT_SPECIFIC_VAL = 0x80 ;
- /** value for the private type class */
- public static final int PRIVATE_VAL = 0xc0 ;
-
- /** enum for the universal type class */
- public static final TypeClass UNIVERSAL =
- new TypeClass( "UNIVERSAL", UNIVERSAL_VAL ) ;
- /** enum for the application type class */
- public static final TypeClass APPLICATION =
- new TypeClass( "APPLICATION", APPLICATION_VAL ) ;
- /** enum for the context specific type class */
- public static final TypeClass CONTEXT_SPECIFIC =
- new TypeClass( "CONTEXT_SPECIFIC", CONTEXT_SPECIFIC_VAL ) ;
- /** enum for the private type class */
- public static final TypeClass PRIVATE =
- new TypeClass( "PRIVATE", PRIVATE_VAL ) ;
+ /**
+ * value for the universal type class
+ */
+ public static final int UNIVERSAL_VAL = 0;
+ /**
+ * value for the application type class
+ */
+ public static final int APPLICATION_VAL = 0x40;
+ /**
+ * value for the context specific type class
+ */
+ public static final int CONTEXT_SPECIFIC_VAL = 0x80;
+ /**
+ * value for the private type class
+ */
+ public static final int PRIVATE_VAL = 0xc0;
+
+ /**
+ * enum for the universal type class
+ */
+ public static final TypeClass UNIVERSAL = new TypeClass( "UNIVERSAL", UNIVERSAL_VAL );
+ /**
+ * enum for the application type class
+ */
+ public static final TypeClass APPLICATION = new TypeClass( "APPLICATION", APPLICATION_VAL );
+ /**
+ * enum for the context specific type class
+ */
+ public static final TypeClass CONTEXT_SPECIFIC = new TypeClass( "CONTEXT_SPECIFIC", CONTEXT_SPECIFIC_VAL );
+ /**
+ * enum for the private type class
+ */
+ public static final TypeClass PRIVATE = new TypeClass( "PRIVATE", PRIVATE_VAL );
+
+ private static final TypeClass[] map = {UNIVERSAL, null, null, null, APPLICATION, null, null, null, CONTEXT_SPECIFIC, null, null, null, PRIVATE, null, null, null};
-
/**
* Private constructor so no other instances can be created other than the
* public static constants in this class.
*
- * @param name a string name for the enumeration value.
+ * @param name a string name for the enumeration value.
* @param value the integer value of the enumeration.
*/
private TypeClass( final String name, final int value )
{
- super( name, value ) ;
+ super( name, value );
}
-
-
+
+
/**
* Gets the enumeration type for the type class regardless of case.
*
@@ -87,47 +100,46 @@
// check first using == since it will be the predominate use case
if ( className == APPLICATION.getName() )
{
- return APPLICATION ;
+ return APPLICATION;
}
else if ( className == CONTEXT_SPECIFIC.getName() )
{
- return CONTEXT_SPECIFIC ;
+ return CONTEXT_SPECIFIC;
}
else if ( className == PRIVATE.getName() )
{
- return PRIVATE ;
+ return PRIVATE;
}
else if ( className == UNIVERSAL.getName() )
{
- return UNIVERSAL ;
+ return UNIVERSAL;
}
-
+
if ( className.equalsIgnoreCase( TypeClass.PRIVATE.getName() ) )
{
- return TypeClass.PRIVATE ;
+ return TypeClass.PRIVATE;
}
-
+
if ( className.equalsIgnoreCase( TypeClass.UNIVERSAL.getName() ) )
{
- return TypeClass.UNIVERSAL ;
+ return TypeClass.UNIVERSAL;
}
-
+
if ( className.equalsIgnoreCase( TypeClass.APPLICATION.getName() ) )
{
- return TypeClass.APPLICATION ;
+ return TypeClass.APPLICATION;
}
-
- if ( className.equalsIgnoreCase(
- TypeClass.CONTEXT_SPECIFIC.getName() ) )
+
+ if ( className.equalsIgnoreCase( TypeClass.CONTEXT_SPECIFIC.getName() ) )
{
- return TypeClass.CONTEXT_SPECIFIC ;
+ return TypeClass.CONTEXT_SPECIFIC;
}
-
+
throw new IllegalArgumentException( "Unknown type class name"
- + className ) ;
+ + className );
}
-
-
+
+
/**
* Gets a List of the enumerations for ASN.1 type classes.
*
@@ -135,10 +147,10 @@
*/
public static List list()
{
- return EnumUtils.getEnumList( TypeClass.class ) ;
+ return EnumUtils.getEnumList( TypeClass.class );
}
-
-
+
+
/**
* Gets the Map of TypeClass objects by name using the TypeClass class.
*
@@ -146,7 +158,7 @@
*/
public static Map map()
{
- return EnumUtils.getEnumMap( TypeClass.class ) ;
+ return EnumUtils.getEnumMap( TypeClass.class );
}
@@ -158,25 +170,6 @@
*/
public static TypeClass getTypeClass( int octet )
{
- TypeClass tc = null ;
- int l_value = octet & PRIVATE_VAL ;
-
- switch ( l_value )
- {
- case( UNIVERSAL_VAL ):
- tc = UNIVERSAL ;
- break ;
- case( APPLICATION_VAL ):
- tc = APPLICATION ;
- break ;
- case( CONTEXT_SPECIFIC_VAL ):
- tc = CONTEXT_SPECIFIC ;
- break ;
- case( PRIVATE_VAL ):
- tc = PRIVATE ;
- break ;
- }
-
- return tc ;
+ return map[( octet & PRIVATE_VAL ) >> 4];
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCaseTest.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCaseTest.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCaseTest.java Thu Jun 10 20:26:51 2004
@@ -59,6 +59,7 @@
{
decode( ( byte ) 1 ) ;
decode( ( byte ) 1 ) ;
+ setExpectedState( decoder.VALUE_STATE );
Tuple t = decode( ( byte ) 1 ) ;
assertEquals( 1, t.id ) ;
assertEquals( 1, t.length ) ;
@@ -76,7 +77,9 @@
ByteBuffer[] buffers = new ByteBuffer[2] ;
buffers[0] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
buffers[1] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+ setExpectedState( decoder.LENGTH_STATE );
decode( tuples, buffers ) ;
+ setExpectedState( decoder.LENGTH_STATE );
decode( tuples[0], ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ) ;
}
@@ -89,6 +92,7 @@
ByteBuffer[] buffers = new ByteBuffer[2] ;
buffers[0] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
buffers[1] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+ setExpectedState( decoder.LENGTH_STATE );
decode(tuples, buffers) ;
callbackOccured(decoder, this, tuples[1]) ;
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderCallbackAdapterTest.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderCallbackAdapterTest.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderCallbackAdapterTest.java Thu Jun 10 20:26:51 2004
@@ -64,19 +64,19 @@
super(arg0);
}
- public void testTagDecoded()
+ public void testStartDecoded()
{
- adapter.tagDecoded( null ) ;
+ adapter.start( null );
}
- public void testLengthDecoded()
+ public void testContentsDecoded()
{
- adapter.lengthDecoded( null ) ;
+ adapter.contents( null );
}
- public void testPartialValueDecoded()
+ public void testEndDecoded()
{
- adapter.partialValueDecoded( null ) ;
+ adapter.end() ;
}
public void testDecodeOccurred()
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderTest.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderTest.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERDecoderTest.java Thu Jun 10 20:26:51 2004
@@ -51,7 +51,16 @@
public void testBasisCases() throws Exception
{
decoder.setDecoderMonitor( new DecoderMonitorAdapter() ) ;
- decoder.decode( null ) ;
+
+ try
+ {
+ decoder.decode( null ) ;
+ fail("IllegalArgumentException should have been thrown");
+ }
+ catch (IllegalArgumentException iae)
+ {
+ }
+
decoder.decode( EMPTY_BUFFER ) ;
}
@@ -60,19 +69,23 @@
{
Tuple decoded = null ;
Tuple t = new Tuple( 45, 0, true, TypeClass.APPLICATION ) ;
- assertTrue( decode( t, EMPTY_BUFFER ).equals( t ) ) ;
+
+ setExpectedState( decoder.LENGTH_STATE );
+ assertTrue( decode( t, EMPTY_BUFFER ).equals( t ) ) ;
- t = new Tuple( 45, "Hello world!".length(), true,
- TypeClass.APPLICATION ) ;
+ t = new Tuple( 45, "Hello world!".length(), true, TypeClass.APPLICATION ) ;
+ setExpectedState( decoder.VALUE_STATE );
decoded = decode( t, ByteBuffer.wrap( "Hello world!".getBytes() ) ) ;
assertTrue( decoded.equals( t ) ) ;
- assertEquals( "Hello world!", toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( "Hello world!", toString( buffer ) ) ;
String mesg = RandomStringUtils.randomAlphanumeric(1000) ;
t = new Tuple( 1234233, mesg.length(), true, TypeClass.APPLICATION ) ;
+
+ setExpectedState( decoder.VALUE_STATE );
decoded = decode( t, ByteBuffer.wrap( mesg.getBytes() ) ) ;
assertTrue( decoded.equals( t ) ) ;
- assertEquals( mesg, toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( mesg, toString( buffer ) ) ;
}
@@ -93,16 +106,19 @@
Tuple t1 = new Tuple( 3, "World".length(),
true, TypeClass.APPLICATION ) ;
Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+
+ setExpectedState( decoder.VALUE_STATE );
assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
Tuple decoded = decode( t0, ByteBuffer.wrap( "Hello".getBytes() ) ) ;
assertTrue( decoded.equals( t0 ) ) ;
- assertEquals( "Hello", toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( "Hello", toString( buffer ) ) ;
decoded = decode( t1, ByteBuffer.wrap( "World".getBytes() ) ) ;
assertTrue( decoded.equals( t1 ) ) ;
- assertEquals( "World", toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( "World", toString( buffer ) ) ;
+ setExpectedState( decoder.LENGTH_STATE );
decoded = decode( terminator, EMPTY_BUFFER ) ;
assertTrue( decoded.equals( top ) ) ;
}
@@ -117,9 +133,10 @@
Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
- Tuple decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+ setExpectedState( decoder.VALUE_STATE );
+ Tuple decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
assertTrue( decoded.equals( t0 ) ) ;
- assertEquals( str0, toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( str0, toString( buffer ) ) ;
// automatically set to top because after t1 is delivered top is
decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
@@ -136,9 +153,10 @@
Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
- Tuple decoded = decode( t0, ByteBuffer.wrap( "Hello".getBytes() ) ) ;
+ setExpectedState( decoder.VALUE_STATE );
+ Tuple decoded = decode( t0, ByteBuffer.wrap( "Hello".getBytes() ) ) ;
assertTrue( decoded.equals( t0 ) ) ;
- assertEquals( "Hello", toString( decoded.getLastValueChunk() ) ) ;
+ assertEquals( "Hello", toString( buffer ) ) ;
// automatically set to top because after t1 is delivered top is
decoded = decode( t1, ByteBuffer.wrap( "World".getBytes() ) ) ;
@@ -161,31 +179,18 @@
ByteBuffer[] fragments = fragment( all, 10 ) ;
Tuple decoded = null ;
+ setExpectedState( decoder.VALUE_STATE );
for ( int ii = 0; ii < fragments.length; ii++ )
{
decoded = decode( fragments[ii] ) ;
}
assertTrue( decoded.equals( t0 ) ) ;
- assertEquals( str0, toString( buf ) ) ;
+ assertEquals( str0, toString( buffer ) ) ;
// automatically set to top because after t1 is delivered top is
decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
assertTrue( decoded.equals( top ) ) ;
- }
-
-
- public void testDecodeOccurred()
- {
- try
- {
- decoder.decodeOccurred( null, null ) ;
- fail( "should never get here due to exception being thrown" ) ;
- }
- catch ( IllegalArgumentException e )
- {
- assertNotNull( e ) ;
- }
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/ConstructedTLVTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/ConstructedTLVTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/ConstructedTLVTests.java Thu Jun 10 20:26:51 2004
@@ -44,11 +44,11 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( false, outter.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
outter = decode( "00000011" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
// decode tag
Tuple tlv = decode( "01000001" ) ;
@@ -56,16 +56,17 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
+ setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 2, tlvList.size() ) ;
}
@@ -80,11 +81,11 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( false, outter.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
outter = decode( "10000000" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
// --------------------------------------------------------------------
@@ -96,20 +97,20 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
assertEquals( 0, tlvList.size() ) ;
// decode value
+ setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 1, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get( 0 ) ) ;
// --------------------------------------------------------------------
@@ -121,19 +122,18 @@
assertEquals( 1, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 2, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get( 0 ) ) ;
// --------------------------------------------------------------------
@@ -145,25 +145,25 @@
assertEquals( 2, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 3, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get( 0 ) ) ;
decode( "00000000" ) ;
+ setExpectedState( decoder.LENGTH_STATE );
decode( "00000000" ) ;
assertEquals( 4, tlvList.size() ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteLengthTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteLengthTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteLengthTests.java Thu Jun 10 20:26:51 2004
@@ -44,15 +44,15 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000001" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000000" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 128, tlv.length ) ;
}
@@ -64,15 +64,15 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000001" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 129, tlv.length ) ;
}
@@ -84,15 +84,15 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000001" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "11111111" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 255, tlv.length ) ;
}
@@ -104,19 +104,19 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000010" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "00000000" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000000" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 32768, tlv.length ) ;
}
@@ -128,19 +128,19 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "10000010" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "11111111" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "11111111" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 65535, tlv.length ) ;
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteTagTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteTagTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/MultiByteTagTests.java Thu Jun 10 20:26:51 2004
@@ -50,84 +50,61 @@
public void testId31() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "00011111" + "01011111") ;
+
+ assertEquals( 31, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "00011111" ) ;
- assertEquals( 31, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testId100() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "01100100" + "01011111" ) ;
+
+ assertEquals( 100, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "01100100" ) ;
- assertEquals( 100, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testId127() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "01111111" + "01011111" ) ;
+
+ assertEquals( 127, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "01111111" ) ;
- assertEquals( 127, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testId128() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "00000000" + "10000001" + "01011111" ) ;
+
+ assertEquals( 128, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000000" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "00000001" ) ;
- assertEquals( 128, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testId129() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "00000001" + "10000001" + "01011111" ) ;
+
+ assertEquals( 129, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000001" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "00000001" ) ;
- assertEquals( 129, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -135,88 +112,48 @@
public void testIdShift14() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "00000000" + "10000000" + "10000001" + "01011111" ) ;
+
+ assertEquals( BIT_13, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000000" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000000" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "00000001" ) ;
- assertEquals( BIT_13, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testIdShift14Minus1() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "01111111" + "11111111" + "01011111" ) ;
+
+ assertEquals( BIT_13 - 1, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "11111111" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "01111111" ) ;
- assertEquals( BIT_13 - 1, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testIdShift14Plus1() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "00000001" + "10000000" + "10000001" + "01011111" ) ;
+
+ assertEquals( BIT_13 + 1, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000001" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "10000000" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "00000001" ) ;
- assertEquals( BIT_13 + 1, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
public void testIdShift21Minus1() throws Exception
{
- Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ Tuple tlv = decode( "01111111" + "11111111" + "11111111" + "01011111" ) ;
+
+ assertEquals( BIT_20 - 1, tlv.id ) ;
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "11111111" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "11111111" ) ;
- assertEquals( 0, tlv.id ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
-
- tlv = decode( "01111111" ) ;
- assertEquals( BIT_20 - 1, tlv.id ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SimplePrimitiveTLVTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SimplePrimitiveTLVTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SimplePrimitiveTLVTests.java Thu Jun 10 20:26:51 2004
@@ -46,19 +46,19 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
+ this.setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 1, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get() ) ;
}
@@ -70,19 +70,19 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
+ this.setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 1, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get() ) ;
// decode tag
@@ -91,19 +91,19 @@
assertEquals( 1, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
+ this.setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 2, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get() ) ;
// decode tag
@@ -112,19 +112,19 @@
assertEquals( 2, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "00000001" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
// decode value
+ this.setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 3, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+ assertEquals( 0x0055, 0x00ff & buffer.get() ) ;
}
@@ -136,26 +136,26 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
// decode length
tlv = decode( "10000001" ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
tlv = decode( "00000111" ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 7, tlv.length ) ;
// decode value
+ this.setExpectedState( decoder.VALUE_STATE );
tlv = decode( "01010101" + "01010101" + "01010101" + "01010101"
+ "01010101" + "01010101" + "01010101" ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 1, tlvList.size() ) ;
- assertNotNull( tlv.valueChunk ) ;
- assertEquals( 7, tlv.valueChunk.capacity() ) ;
+ assertEquals( 7, buffer.limit() ) ;
- ByteBuffer value = tlv.valueChunk.duplicate() ;
+ ByteBuffer value = buffer.duplicate() ;
for ( int ii = 0 ; ii < 7; ii++ )
{
assertEquals( 0x0055, 0x00ff & value.get( ii ) ) ;
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteLengthTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteLengthTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteLengthTests.java Thu Jun 10 20:26:51 2004
@@ -36,15 +36,20 @@
super( name ) ;
}
-
+
+ /**
+ * This encoding's tuple should complete
+ * @throws Exception
+ */
public void testLength0() throws Exception
{
+ this.setExpectedState( decoder.LENGTH_STATE );
Tuple tlv = decode( "00000000" + "01000001" ) ;
assertEquals( 1, tlv.id ) ;
assertEquals( 1, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
assertEquals( 0, tlv.length ) ;
}
@@ -56,7 +61,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 1, tlv.length ) ;
}
@@ -68,7 +73,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 3, tlv.length ) ;
}
@@ -80,7 +85,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+ assertEquals( decoder.VALUE_STATE, decoder.getState() ) ;
assertEquals( 127, tlv.length ) ;
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteTagTests.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteTagTests.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/SingleByteTagTests.java Thu Jun 10 20:26:51 2004
@@ -44,7 +44,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -55,7 +55,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.PRIVATE, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -66,7 +66,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.CONTEXT_SPECIFIC, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -77,7 +77,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -100,7 +100,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -111,7 +111,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -122,7 +122,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -133,7 +133,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -144,7 +144,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -155,7 +155,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -166,7 +166,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -177,7 +177,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -188,7 +188,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -199,7 +199,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -210,7 +210,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -221,7 +221,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -232,7 +232,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -243,7 +243,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -254,7 +254,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -265,7 +265,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -276,7 +276,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -287,7 +287,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -298,7 +298,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -309,7 +309,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -320,7 +320,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -331,7 +331,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -342,7 +342,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -353,7 +353,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -364,7 +364,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -375,7 +375,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -386,7 +386,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -397,7 +397,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -408,7 +408,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -419,7 +419,7 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( true, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
@@ -428,13 +428,11 @@
// this is the long form
Tuple tlv = decode( "01011111" ) ;
- assertEquals( 0, tlv.id ) ;
+ assertEquals( null, tlv ) ;
assertEquals( 0, tlvList.size() ) ;
- assertEquals( true, tlv.isPrimitive ) ;
- assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
// state did not switch since we are still reading the long tag
- assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+ assertEquals( decoder.TAG_STATE, decoder.getState() ) ;
}
@@ -445,6 +443,6 @@
assertEquals( 0, tlvList.size() ) ;
assertEquals( false, tlv.isPrimitive ) ;
assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
- assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+ assertEquals( decoder.LENGTH_STATE, decoder.getState() ) ;
}
}
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TagTest.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TagTest.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TagTest.java Thu Jun 10 20:26:51 2004
@@ -18,7 +18,6 @@
import org.apache.commons.lang.ArrayUtils ;
-import org.apache.snickers.ber.primitives.UniversalTag;
import junit.framework.TestCase ;
@@ -163,9 +162,9 @@
for ( int ii = 128 ; ii < 20000; ii++ )
{
- octets[1] = ( byte ) ( ii & Tag.LONG_MASK ) ;
- octets[2] = ( byte ) ( ( ii >> 7 ) & Tag.LONG_MASK ) ;
-
+ octets[1] = ( byte ) ( ( ii >> 7 ) & Tag.LONG_MASK ) ;
+ octets[2] = ( byte ) ( ii & Tag.LONG_MASK ) ;
+
if ( ii < 16384 )
{
assertEquals( Tag.getTagId( octets ), ii ) ;
@@ -184,10 +183,10 @@
for ( int ii = 16384 ; ii < 2100000 ; ii++ )
{
- octets[1] = ( byte ) ( ii & Tag.LONG_MASK ) ;
+ octets[1] = ( byte ) ( ( ii >> 14 ) & Tag.LONG_MASK ) ;
octets[2] = ( byte ) ( ( ii >> 7 ) & Tag.LONG_MASK ) ;
- octets[3] = ( byte ) ( ( ii >> 14 ) & Tag.LONG_MASK ) ;
-
+ octets[3] = ( byte ) ( ii & Tag.LONG_MASK ) ;
+
if ( ii < 2097152 )
{
assertEquals( Tag.getTagId( octets ), ii ) ;
@@ -288,10 +287,6 @@
public void testGetIntEncodedTag()
{
- assertEquals( UniversalTag.INTEGER.getPrimitiveTag(),
- Tag.getIntEncodedTag( TypeClass.UNIVERSAL,
- UniversalTag.INTEGER.getTagId(), false ) ) ;
-
assertEquals( 0x01000000,
Tag.getIntEncodedTag( TypeClass.UNIVERSAL, 1, false ) ) ;
Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TupleTest.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TupleTest.java (original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/TupleTest.java Thu Jun 10 20:26:51 2004
@@ -24,8 +24,6 @@
import org.apache.commons.lang.ArrayUtils ;
import org.apache.commons.codec.binary.Binary ;
-import junit.framework.TestCase ;
-
/**
* Tests Tuple class.
@@ -34,7 +32,7 @@
* Apache Directory Project</a>
* @version $Rev$
*/
-public class TupleTest extends TestCase
+public class TupleTest extends AbstractDecoderTestCase
{
/** precalculated left shift of 1 by 14 places */
private static final int BIT_13 = 1 << 14 ;
@@ -47,10 +45,6 @@
/** precalculated left shift of 1 by 28 places */
private static final int BIT_27 = 1 << 28 ;
- /** for convenience in handling nulls */
- private static final ByteBuffer EMPTY_BUFFER =
- ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
-
public static void main(String[] args)
{
junit.textui.TestRunner.run(TupleTest.class);
@@ -140,15 +134,13 @@
assertEquals( 2, t.getId() ) ;
assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
assertEquals( false, t.isPrimitive() ) ;
- assertEquals( Length.INDEFINATE, t.getLength() ) ;
- assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+ assertEquals( Length.INDEFINITE, t.getLength() ) ;
t = new Tuple( 2, (TypeClass) null ) ;
assertEquals( 2, t.getId() ) ;
assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
assertEquals( false, t.isPrimitive() ) ;
- assertEquals( Length.INDEFINATE, t.getLength() ) ;
- assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
+ assertEquals( Length.INDEFINITE, t.getLength() ) ;
}
public void testGetId()
@@ -191,21 +183,21 @@
public void testIsIndefinate()
{
Tuple t = new Tuple() ;
- assertFalse( t.isIndefinate() ) ;
- t.length = Length.INDEFINATE ;
- assertTrue( t.isIndefinate() ) ;
+ assertFalse( t.isIndefinite() ) ;
+ t.length = Length.INDEFINITE ;
+ assertTrue( t.isIndefinite() ) ;
}
public void testIsIndefinateTerminator()
{
Tuple t = new Tuple() ;
- assertFalse( t.isIndefinateTerminator() ) ;
+ assertFalse( t.isIndefiniteTerminator() ) ;
t.id = 0 ;
t.length = 0 ;
t.isPrimitive = true ;
t.typeClass = TypeClass.UNIVERSAL ;
- assertTrue( t.isIndefinateTerminator() ) ;
+ assertTrue( t.isIndefiniteTerminator() ) ;
}
@@ -237,18 +229,6 @@
assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
}
- public void testGetValue()
- {
- Tuple t = new Tuple() ;
- assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
- byte[] bites = {1, 2, 3, 45} ;
- t.valueChunk = ByteBuffer.wrap( bites ) ;
- assertEquals( ByteBuffer.wrap( bites ), t.getLastValueChunk() ) ;
- byte[] bites2 = {1, 2, 3, 45} ;
- t.clear() ;
- assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
- }
-
public void testClear()
{
Tuple t = new Tuple() ;
@@ -277,16 +257,6 @@
t.clear() ;
assertEquals( TypeClass.APPLICATION, t.typeClass ) ;
- t.valueChunk = ByteBuffer.allocate( 3 ) ;
- assertNotNull( t.valueChunk ) ;
- t.clear() ;
- assertEquals( EMPTY_BUFFER, t.valueChunk ) ;
-
- t.valueIndex = 12 ;
- assertEquals( 12, t.valueIndex ) ;
- t.clear() ;
- assertEquals( Length.UNDEFINED, t.valueIndex ) ;
-
}
/*
@@ -295,73 +265,11 @@
public void testEqualsObject()
{
Tuple tnull0 = new Tuple() ;
- tnull0.valueChunk = null ;
Tuple tnull1 = new Tuple() ;
- tnull1.valueChunk = null ;
tnull0.equals( tnull1 ) ;
tnull1.equals( tnull1 ) ;
tnull0.equals( tnull0 ) ;
-
- Tuple t0 = new Tuple() ;
- Tuple t1 = ( Tuple ) t0.clone() ;
-
- assertTrue( t0.equals( t1 ) ) ;
- t0.id = 23 ;
- assertFalse( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- // indices are not taken into account in Tuple.equals(Object)
- t0.index = 23 ;
- t1.index = 33 ;
- assertTrue( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- t0.isPrimitive = false ;
- t1.isPrimitive = true ;
- assertFalse( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- t0.length = 23 ;
- assertFalse( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- t0.typeClass = TypeClass.PRIVATE ;
- t1.typeClass = TypeClass.UNIVERSAL ;
- assertFalse( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- // indices are not taken into account in Tuple.equals(Object)
- t0.valueIndex = 23 ;
- t1.valueIndex = 3 ;
- assertTrue( t0.equals( t1 ) ) ;
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- t0.valueChunk = ByteBuffer.allocate( 4 ) ;
- t1.valueChunk = null ;
-
- // The buffer does not factor into equality
- assertTrue( t0.equals( t1 ) ) ;
-
- t1 = ( Tuple ) t0.clone() ;
- assertTrue( t0.equals( t1 ) ) ;
-
- assertFalse( t0.equals( new Object() )) ;
- }
-
- /*
- * Class to test for Object clone()
- */
- public void testClone()
- {
- Tuple t = new Tuple() ;
- assertTrue( t.equals( t.clone() ) ) ;
}
public void testToEncodedBufferConstructed()
@@ -369,7 +277,7 @@
Tuple t = null ;
ByteBuffer encoded ;
String binary ;
-
+
t = new Tuple( 0, 0 ) ;
encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
binary = toAsciiString( encoded ) ;
@@ -408,8 +316,8 @@
binary = toAsciiString( encoded ) ;
assertEquals(
"00000000" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01111111"
, toAsciiString( encoded ) ) ;
@@ -420,8 +328,8 @@
binary = toAsciiString( encoded ) ;
assertEquals(
"01111111" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01111111"
, toAsciiString( encoded ) ) ;
@@ -433,8 +341,8 @@
assertEquals(
"10000000" +
"10000001" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01111111"
, toAsciiString( encoded ) ) ;
@@ -446,8 +354,8 @@
assertEquals(
"11111111" +
"10000001" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01111111"
, toAsciiString( encoded ) ) ;
@@ -460,8 +368,8 @@
"00000001" +
"00000000" +
"10000010" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01111111"
, toAsciiString( encoded ) ) ;
}
@@ -511,8 +419,8 @@
binary = toAsciiString( encoded ) ;
assertEquals(
"00000000" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
@@ -525,8 +433,8 @@
assertEquals(
"00000000" +
"00000001" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
@@ -538,8 +446,8 @@
binary = toAsciiString( encoded ) ;
assertEquals( Binary.toAsciiString( data ) +
"01111111" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
@@ -552,8 +460,8 @@
assertEquals( Binary.toAsciiString( data ) +
"10000000" +
"10000001" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
@@ -566,8 +474,8 @@
assertEquals( Binary.toAsciiString( data ) +
"11111111" +
"10000001" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
@@ -581,8 +489,8 @@
"00000001" +
"00000000" +
"10000010" +
- "00000001" +
- "10000000" +
+ "00000000" +
+ "10000001" +
"01011111"
, toAsciiString( encoded ) ) ;
}
@@ -631,7 +539,7 @@
t = new Tuple( 128, 0 ) ;
t.setTag( bites, 3 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" + "10000000" + "01111111", binary ) ;
+ assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
bites = ByteBuffer.allocate( 3 ) ;
t = new Tuple( BIT_13 - 1, 0 ) ;
@@ -645,9 +553,9 @@
t = new Tuple( BIT_13, 0 ) ;
t.setTag( bites, 4 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" +
+ assertEquals( "00000000" +
"10000000" +
- "10000000" +
+ "10000001" +
"01111111", binary ) ;
bites = ByteBuffer.allocate( 4 ) ;
@@ -672,10 +580,10 @@
t = new Tuple( BIT_20, 0 ) ;
t.setTag( bites, 5 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" +
+ assertEquals( "00000000" +
"10000000" +
"10000000" +
- "10000000" +
+ "10000001" +
"01111111", binary ) ;
bites = ByteBuffer.allocate( 5 ) ;
@@ -749,7 +657,7 @@
t = new Tuple( 128, 0 ) ;
t.setTag( bites, 3 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" + "10000000" + "01111111", binary ) ;
+ assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
bites = ByteBuffer.allocate( 3 ) ;
t = new Tuple( BIT_13 - 1, 0 ) ;
@@ -763,9 +671,9 @@
t = new Tuple( BIT_13, 0 ) ;
t.setTag( bites, 4 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" +
+ assertEquals( "00000000" +
"10000000" +
- "10000000" +
+ "10000001" +
"01111111", binary ) ;
bites = ByteBuffer.allocate( 4 ) ;
@@ -790,10 +698,10 @@
t = new Tuple( BIT_20, 0 ) ;
t.setTag( bites, 5 ) ;
binary = toAsciiString( bites ) ;
- assertEquals( "00000001" +
+ assertEquals( "00000000" +
"10000000" +
"10000000" +
- "10000000" +
+ "10000001" +
"01111111", binary ) ;
bites = ByteBuffer.allocate( 5 ) ;