directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9969 - in incubator/directory/snickers/trunk/ber/src: java/org/apache/snickers/ber/digester/rules test/org/apache/snickers/ber/digester/rules
Date Mon, 12 Apr 2004 06:33:30 GMT
Author: akarasulu
Date: Sun Apr 11 23:33:29 2004
New Revision: 9969

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java
  (contents, props changed)
Log:
o created accumulating OCTET STRING rule which only works for primitives
o added unit test case but we still need to add more tests


Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java
Sun Apr 11 23:33:29 2004
@@ -0,0 +1,214 @@
+/*
+ *   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.digester.rules ;
+
+
+import org.apache.snickers.ber.Length ;
+import org.apache.snickers.ber.TypeClass ;
+import org.apache.snickers.ber.digester.AbstractRule ;
+import org.apache.snickers.ber.primitives.UniversalTag ;
+
+import java.nio.ByteBuffer ;
+
+
+/**
+ * A rule that collects the value bytes of an ASN.1 OCTET STRING and pushes
+ * the byte[] onto the digester's Object stack.
+ * <p>
+ * This rule can only handle primitive octet strings.  Constructed OCTET STRING
+ * values are simply ignored by this rule rather than throwing exceptions.
+ * </p>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveOctetStringRule extends AbstractRule
+{
+    /** the default increment for the accumulator as is grows if it must */
+    private static final int DEFAULT_INCREMENT = 100 ;
+
+    /** the octets accumulator */
+    private byte[] octets ;
+    /** the growth increment for the accumulator */
+    private int increment = DEFAULT_INCREMENT ;
+    /** the current position that we're writing to */
+    private int pos = 0 ;
+    /** whether or not the length is known */
+    private boolean isLengthIndeterminate = false ;
+    /** used to determin if our type is constructed or primitive */
+    private boolean isConstructed = false ;
+
+
+    // -----------------------------------------------------------------------
+    // C O N S T R U C T O R S
+    // -----------------------------------------------------------------------
+
+
+    /**
+     * Creates a rule using defaults.
+     */
+    PrimitiveOctetStringRule()
+    {
+    }
+
+
+    /**
+     * Creates a rule instance which grows the backing store on indeterminate
+     * length OCTET STRINGS by the specified growth increment.
+     *
+     * @param increment the byte increment to grow the backing store by
+     */
+    PrimitiveOctetStringRule( int increment )
+    {
+        this.increment = increment ;
+    }
+
+
+    // -----------------------------------------------------------------------
+    // Rule event method overrides
+    // -----------------------------------------------------------------------
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#tag(int, boolean,
+     * org.apache.snickers.ber.TypeClass)
+     */
+    public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+    {
+        isConstructed = ! isPrimitive ;
+
+        if ( isConstructed )
+        {
+            return ;
+        }
+
+        if ( id != UniversalTag.OCTET_STRING.getTagId() )
+        {
+            throw new IllegalArgumentException(
+                    "Expecting " + UniversalTag.OCTET_STRING.getName()
+                    + " with an id of " + UniversalTag.OCTET_STRING.getTagId()
+                    + " but instead got a tag id of " + id ) ;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#length(int)
+     */
+    public void length( int length )
+    {
+        if ( isConstructed )
+        {
+            return ;
+        }
+
+        // @todo Length should not be visible outside of the digester
+        // package.  The digester or a contants interface should contain
+        // these constants.
+        if ( Length.INDEFINATE == length )
+        {
+            octets = new byte[ increment ] ;
+            isLengthIndeterminate = true ;
+        }
+        else
+        {
+            octets = new byte[ length ] ;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#value(java.nio.ByteBuffer)
+     */
+    public void value( ByteBuffer buf )
+    {
+        if ( isConstructed )
+        {
+            return ;
+        }
+
+        if ( buf == null )
+        {
+            return ;
+        }
+
+        while( buf.hasRemaining() )
+        {
+            /*
+             * Check if we ran out of space.  If so see why - is it because
+             * we're done with the value or out grew the byte[] storage space.
+             */
+            if ( pos >= octets.length )
+            {
+                // we out grew our space if length is the indeterminate form
+                if ( isLengthIndeterminate )
+                {
+                    byte[] dest = new byte[ octets.length + increment ] ;
+                    System.arraycopy( octets, 0, dest, 0, octets.length ) ;
+                    octets = dest ;
+                }
+                else
+                {
+                    return ;
+                }
+            }
+
+            /*
+             * Find out how much space we have left and if it can hold the
+             * remaining contents of the buffer.
+             */
+            int spaceLeft = octets.length - pos ;
+            if ( buf.remaining() <= spaceLeft )
+            {
+                int remaining = buf.remaining() ;
+                buf.get( octets, pos, remaining ) ;
+                pos += remaining ;
+                return ;
+            }
+
+            /*
+             * Below we have a situation where there are more octets in the
+             * buffer than we have space.  So we read as much as we can into
+             * the empty space filling it up all the way until another cycle
+             * allocates more space.
+             */
+            buf.get( octets, pos, spaceLeft ) ;
+            pos += spaceLeft ;
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.Rule#finish()
+     */
+    public void finish()
+    {
+        if ( isConstructed )
+        {
+            return ;
+        }
+
+        // push the octet string onto the digester's object stack
+        getDigester().push( octets ) ;
+
+        // clean up
+        pos = 0 ;
+        octets = null ;
+        isConstructed = false ;
+        isLengthIndeterminate = false ;
+    }
+}

Added: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java
Sun Apr 11 23:33:29 2004
@@ -0,0 +1,97 @@
+/*
+ *   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.digester.rules ;
+
+
+import junit.framework.TestCase ;
+
+import org.apache.snickers.ber.digester.BERDigester ;
+import org.apache.snickers.ber.primitives.UniversalTag;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * Tests the operation of the PrimitiveOctetStringRule.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class PrimitiveOctetStringRuleTest extends TestCase
+{
+    private BERDigester digester ;
+    private PrimitiveOctetStringRule rule ;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp() ;
+
+        rule = new PrimitiveOctetStringRule() ;
+        digester = new BERDigester() ;
+        rule.setDigester( digester ) ;
+    }
+
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown() ;
+
+        rule = null ;
+        digester = null ;
+    }
+
+
+    /**
+     * Tests for correct behavior when the OCTET STRING is constructed.
+     */
+    public void testConstructedOctetString()
+    {
+        rule.tag( UniversalTag.OCTET_STRING.getTagId(), false, null ) ;
+        rule.length( 2 ) ;
+        byte[] bites = { 0x07, 0x1 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        rule.finish() ;
+
+        // we should have nothing in the object stack
+        assertEquals( 0, digester.getCount() ) ;
+    }
+
+
+    /**
+     * Tests for correct behavior when the OCTET STRING is constructed.
+     */
+    public void testPrimitiveOctetString()
+    {
+        rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
+        rule.length( 2 ) ;
+        byte[] bites = { 0x07, 0x1 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        rule.value( buf ) ;
+        rule.finish() ;
+
+        // we should have nothing in the object stack
+        assertEquals( 1, digester.getCount() ) ;
+        byte[] pushed = ( byte [] ) digester.pop() ;
+        assertEquals( bites[0], pushed[0] ) ;
+        assertEquals( bites[1], pushed[1] ) ;
+        assertEquals( 0, digester.getCount() ) ;
+    }
+
+
+}
\ No newline at end of file

Mime
View raw message