directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r234315 [1/2] - in /directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new: ber/containers/ ber/grammar/ ber/tlv/ primitives/ util/
Date Sun, 21 Aug 2005 18:51:13 GMT
Author: elecharny
Date: Sun Aug 21 11:51:02 2005
New Revision: 234315

URL: http://svn.apache.org/viewcvs?rev=234315&view=rev
Log:
more classes added...

Added:
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/IAsn1Container.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/AbstractGrammar.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarAction.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarTransition.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IAction.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IGrammar.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IStates.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/ITLVBerDecoderMBean.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/primitives/
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/primitives/BitString.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/primitives/OID.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/primitives/OctetString.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/util/
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/util/IntegerDecoder.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/util/MutableString.java
    directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/IAsn1Container.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/IAsn1Container.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/IAsn1Container.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/containers/IAsn1Container.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,136 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.containers;
+
+import org.apache.asn1new.ber.grammar.IGrammar;
+import org.apache.asn1new.ber.grammar.IStates;
+import org.apache.asn1new.ber.tlv.TLV;
+
+
+/**
+ * Every ASN1 container must implement this interface. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IAsn1Container
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    // State accessors
+    /**
+     * Get the current grammar state
+     *
+     * @return Returns the current grammar state
+     */
+    int getState();
+
+    /**
+     * Set the new current state
+     *
+     * @param state The new state
+     */
+    void setState( int state );
+
+    /**
+     * Set the current TLV
+     *
+     * @param tlv The current TLV
+     */
+    public void setCurrentTLV( TLV tlv );
+
+    /**
+     * Get the currentTLV
+     *
+     * @return Returns the current TLV being decoded
+     */
+    public TLV getCurrentTLV();
+
+    /**
+     * Get the grammar
+     *
+     * @return Returns the grammar used to decode a LdapMessage.
+     */
+    public IGrammar getGrammar();
+
+    /**
+     * Add a new IGrammar to use
+     *
+     * @param grammar The grammar to add.
+     */
+    public void addGrammar( IGrammar grammar );
+
+    /**
+     * Switch to another grammar
+     *
+     * @param grammar The grammar to switch to.
+     */
+    public void switchGrammar( int currentState, int grammar );
+
+    /**
+     * restore the previous grammar (the one before a switch has occured)
+     * 
+     * @return Returns the previous state if any.
+     */
+    public int restoreGrammar();
+
+    /**
+     * @return Returns the currentGrammar.
+     */
+    public int getCurrentGrammar();
+
+    /**
+     * Set the first grammar to use
+     * @param The first grammar .
+     */
+    public void setInitGrammar(int grammar);
+
+    /**
+     * Get the transition
+     *
+     * @return Returns the transition from the previous state to the new 
+     * state
+     */
+    public int getTransition();
+
+    /**
+     * Update the transition from a state to another
+     *
+     * @param transition The transition to set
+     */
+    public void setTransition( int transition );
+    
+    /**
+     * @return Returns the current Grammar type, or -1 if not found.
+     */
+    public int getCurrentGrammarType();
+    
+    /**
+     * @return Returns the states.
+     */
+    public IStates getStates();
+    
+    /**
+     * @return get the parent TLV.
+     */
+    public TLV getParentTLV();
+
+    /**
+     * Set the parent TLV
+     * @param The new parent TLV 
+     */
+    public void setParentTLV(TLV parentTLV);
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/AbstractGrammar.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/AbstractGrammar.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/AbstractGrammar.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/AbstractGrammar.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,243 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+import org.apache.asn1new.Asn1Object;
+import org.apache.asn1new.DecoderException;
+import org.apache.asn1new.ber.containers.IAsn1Container;
+import org.apache.asn1new.ber.tlv.TLV;
+import org.apache.asn1new.ber.tlv.Tag;
+import org.apache.asn1new.util.StringUtils;
+import org.slf4j.LoggerFactory;
+
+import org.slf4j.Logger;
+
+
+/**
+ * The abstract IGrammar which is the Mother of all the grammars. It
+ * contains the transitions table.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public abstract class AbstractGrammar implements IGrammar
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** The logger */
+    private static final Logger log = LoggerFactory.getLogger( AbstractGrammar.class );
+
+    /** Logging speed up  */
+    private static final boolean DEBUG = log.isDebugEnabled();
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** Table of transitions. It's a two dimension array, the first dimension
+     * indice the states, the second dimension indices the Tag value, so it is 256 wide. */
+    protected GrammarTransition[][] transitions;
+
+    /** The grammar name */
+    protected String name;
+
+    /** The grammar's states */
+    protected IStates statesEnum;
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Return the grammar's name
+     * @return The grammar name
+    */
+    public String getName()
+    {
+        return name;
+    }
+
+    /**
+     * Set the grammar's name
+     * @param name DOCUMENT ME!
+    */
+    public void setName( String name )
+    {
+        this.name = name;
+    }
+
+    /**
+     * Checks the Length. If the current TLV length is above the expected length of the 
+     * PDU, an exception is thrown.
+     * 
+     * The current Object contains the sum of all included Objects and element, which is
+     * compared with the PDU's expected length (the Length part of the PDU containing the Object).
+     * 
+     * @param object The Object that is being decoded.
+     * @param tlv The current TLV
+     * @throws DecoderException Thrown if the expected length is lower than the sum
+     * of all the included elements.
+     */
+    protected void checkLength( Asn1Object object, TLV tlv ) throws DecoderException
+    {
+
+        // Create a new expected Length
+        int expectedLength = tlv.getLength().getLength();
+
+        int tlvLength      = tlv.getSize();
+
+        if ( DEBUG )
+        {
+            log.debug(
+                "Expected Length = " + ( ( Asn1Object ) object ).getExpectedLength() +
+                ", current length = " + ( ( Asn1Object ) object ).getCurrentLength() +
+                ", added length = " + expectedLength +
+                ", tlv length = " + tlvLength );
+        }
+
+        // We already are at the top level.
+        // An exception will be thrown if the current length exceed the expected length
+        ( ( Asn1Object ) object ).addLength( tlvLength );
+    }
+
+    /**
+     * Get the transition associated with the state and tag
+     * @param state The current state
+     * @param tag The current tag
+     * @return A valid transition if any, or null.
+     */
+    public GrammarTransition getTransition( int state, int tag )
+    {
+        return transitions[state][tag & IStates.STATES_SWITCH_MASK];
+    }
+
+    /**
+     * The main function. This is where an action is executed. If the 
+     * action is null, nothing is done.
+     *
+     * @param container The Asn1Container 
+     *
+     * @throws DecoderException Thrown if anything went wrong
+     */
+    public void executeAction( IAsn1Container container ) throws DecoderException
+    {
+
+        int      currentState   = container.getTransition();
+        IGrammar currentGrammar = container.getGrammar();
+
+        // We have to deal with the special case of a GRAMMAR_END state
+        if ( currentState == IStates.END_STATE )
+        {
+            currentState = container.restoreGrammar();
+
+            if ( currentState == IStates.END_STATE )
+            {
+                return;
+            }
+        }
+
+        Tag  tag     = container.getCurrentTLV().getTag();
+        byte tagByte = tag.getTagByte();
+
+        // We will loop until no more actions are to be executed
+        while ( true )
+        {
+
+            GrammarTransition transition = ( ( AbstractGrammar ) container.getGrammar() )
+                .getTransition( currentState, tagByte & IStates.STATES_SWITCH_MASK );
+
+            if ( transition == null )
+            {
+
+                if ( container.getCurrentGrammar() == 0 )
+                {
+                	String errorMessage = "Bad transition from state " +
+                        currentGrammar.getStatesEnum().getState( container.getCurrentGrammarType(),
+                            currentState ) + ", tag " + StringUtils.dumpByte( tag.getTagByte() );
+                	
+                	log.error( errorMessage );
+                	
+                    // If we have no more grammar on the stack, then this is an error
+                    throw new DecoderException( "Bad transition !" );
+                }
+                else
+                {
+
+                    // We have finished with the current grammar, so we have to continue with the
+                    // previous one.
+                    currentState = container.restoreGrammar();
+                    continue;
+                }
+            }
+
+            if ( DEBUG )
+            {
+                log.debug( transition.toString( container.getCurrentGrammarType(),
+                        currentGrammar.getStatesEnum() ) );
+            }
+
+            int nextState = transition.getNextState();
+
+            if ( ( ( nextState & IStates.GRAMMAR_SWITCH_MASK ) != 0 ) &&
+                    ( nextState != IStates.END_STATE ) )
+            {
+
+                if ( DEBUG )
+                {
+                    log.debug(
+                        "Switching from grammar " +
+                        container.getStates().getGrammarName( currentGrammar ) +
+                        " to grammar " + container.getStates().getGrammarName( ( nextState >> 8 ) - 1 ) );
+                }
+
+                // We have a grammar switch, so we change the current state to the initial
+                // state in the new grammar and loop.
+                container.switchGrammar( currentState, nextState & IStates.GRAMMAR_SWITCH_MASK );
+                currentState = IStates.INIT_GRAMMAR_STATE;
+            }
+            else
+            {
+
+                // This is not a grammar switch, so we execute the
+                // action if we have one, and we quit the loop.
+                container.setTransition( nextState );
+
+                if ( transition.hasAction() )
+                {
+                    transition.getAction().action( container );
+                }
+
+                break;
+            }
+        }
+    }
+
+    /**
+     * Get the states of the current grammar
+     *
+     * @return Returns the statesEnum.
+     */
+    public IStates getStatesEnum()
+    {
+        return statesEnum;
+    }
+
+    /**
+     * Set the states for this grammar
+     *
+     * @param statesEnum The statesEnum to set.
+     */
+    public void setStatesEnum( IStates statesEnum )
+    {
+        this.statesEnum = statesEnum;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarAction.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarAction.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarAction.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarAction.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,56 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+/**
+ * A top level grammar class that store meta informations about the actions.
+ * 
+ * Those informations are not mandatory, but they can be usefull for debugging. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public abstract class GrammarAction implements IAction
+{
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The action's name */
+    protected String name;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new GrammarAction object.
+     *
+     * @param name The name of the create daction
+     */
+    public GrammarAction(  String name )
+    {
+        this.name = name;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Print the action's name
+     *
+     * @return The action's name
+     */
+    public String toString()
+    {
+        return name;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarTransition.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarTransition.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarTransition.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/GrammarTransition.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,98 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+/**
+ * Define a transition between two states of a grammar. It stores the 
+ * next state, and the action to execute while transiting.  
+ * 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GrammarTransition
+{
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The next state in the grammar */
+    private int           nextState;
+
+    /** The action associated to the transition */
+    private GrammarAction action;
+
+    /** The current state */
+    private int        currentState;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new GrammarTransition object.
+     *
+     * @param currentState The current transition 
+     * @param nextState The target state
+     * @param action The action to execute. It could be null.
+     */
+    public GrammarTransition(  int currentState, int nextState, GrammarAction action )
+    {
+        this.currentState      = currentState;
+        this.nextState = nextState;
+        this.action    = action;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * @return Returns the target state.
+     */
+    public int getNextState()
+    {
+        return nextState;
+    }
+
+    /**
+     * Tells if the transition has an associated action.
+     *
+     * @return <code>true</code> if an action has been asociated to the transition
+     */
+    public boolean hasAction()
+    {
+        return action != null;
+    }
+
+    /**
+     * @return Returns the action associated with the transition
+     */
+    public GrammarAction getAction()
+    {
+        return action;
+    }
+
+    /**
+     * @param grammar The grammar which state we want a String from
+     * @return A representation of the transition as a string.
+     */
+    public String toString(int grammar, IStates statesEnum)
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        sb.append( "Transition from <" ).append( statesEnum.getState( grammar, currentState ) ).append( "> to <" )
+          .append( statesEnum.getState( grammar, nextState ) ).append( ">, action : " )
+          .append( ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
+
+        return sb.toString();
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IAction.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IAction.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IAction.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IAction.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,41 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+import org.apache.asn1new.DecoderException;
+import org.apache.asn1new.ber.containers.IAsn1Container;
+
+
+/**
+ * IAction interface just contains the method 'action' which must be implemented
+ * in all the implementong classes.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IAction
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * The action to be executed.
+     *
+     * @param container The container which stores the current data
+     *
+     * @throws DecoderException Thrown if something went wrong.
+     */
+    public void action( IAsn1Container container ) throws DecoderException;
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IGrammar.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IGrammar.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IGrammar.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IGrammar.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,60 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+import org.apache.asn1new.DecoderException;
+import org.apache.asn1new.ber.containers.IAsn1Container;
+
+
+/**
+ * The interface which expose common behavior of a Gramar implementer.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IGrammar
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * This method, when called, execute an action on the current data stored 
+     * in the container.
+     *
+     * @param asn1Container Store the data being processed.
+     *
+     * @throws DecoderException Thrown when an unrecoverable error occurs.
+     */
+    void executeAction( IAsn1Container asn1Container ) throws DecoderException;
+
+    /**
+     * Get the grammar name
+     *
+     * @return Return the grammar's name
+     */
+    String getName();
+
+    /**
+     * Get the statesEnum for the current grammar 
+     * @return The specific States Enum for the current grammar
+     */
+    IStates getStatesEnum();
+
+    /**
+     * Set the grammar's name
+     * @param name The grammar name
+    */
+    void setName( String name );
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IStates.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IStates.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IStates.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/grammar/IStates.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,50 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.grammar;
+
+/**
+ * This interface is used to store the different states of a grammar. While
+ * tracing debugging information, the methods to dump the current state
+ * as a string are called.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IStates {
+    /** The initial state of every grammar */
+    static int INIT_GRAMMAR_STATE = 0;
+
+    /** The ending state for every grammars */
+    static int GRAMMAR_END = -1;
+    
+    /** The END_STATE */
+    static int END_STATE = -1;
+
+    /** The mask to filter grammar switch */
+    final static int GRAMMAR_SWITCH_MASK = 0xFF00;
+    
+    /** The mask to filter states transition */
+    final static int STATES_SWITCH_MASK = 0x00FF;
+    
+    /** Get the current state for a specified grammar */
+    String getState( int grammar, int state );
+    
+    /** Return the grammar name from a grammar */ 
+    String getGrammarName(IGrammar grammar);
+
+    /** Return the grammar name from a grammar number */ 
+    String getGrammarName(int grammar);
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/ITLVBerDecoderMBean.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/ITLVBerDecoderMBean.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/ITLVBerDecoderMBean.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/ITLVBerDecoderMBean.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+import org.apache.asn1new.DecoderException;
+
+
+/**
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface ITLVBerDecoderMBean
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /** Set the number of bytes that can be used to encode the Value length,
+     * including the first byte. 
+     * Max is 127 if the Length use a definite form, default is 1
+     *  
+     * @param length The number of byte to use 
+    */
+    void setMaxLengthLength( int length ) throws DecoderException;
+
+    /** Set the maximum number of bytes that should be used to encode a Tag label,
+     * including the first byte. 
+     * Default is 1, no maximum
+     *   
+     * @param length The length to use 
+    */
+    void setMaxTagLength( int length );
+
+    /** Allow indefinite length. */
+    void allowIndefiniteLength();
+
+    /** Disallow indefinite length. */
+    void disallowIndefiniteLength();
+
+    /** Get the actual maximum number of bytes that can be used to encode the Length
+     *  
+     * @return The maximum bytes of the Length
+    */
+    int getMaxLengthLength();
+
+    /** Get the actual maximum number of bytes that can be used to encode the Tag 
+     * @return The maximum length of the Tag
+    */
+    int getMaxTagLength();
+
+    /** Tell if indefinite length form could be used for Length 
+     * @return <code>true</code> if the Indefinite form is allowed
+    */
+    boolean isIndefiniteLengthAllowed();
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,329 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+import java.io.Serializable;
+
+
+/**
+ * The Length part of a TLV. We are not dealing with indefinite length.
+ * 
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class Length implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+	
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** A mask to get the Length form */
+    public static final transient int LENGTH_LONG_FORM = 0x0080;
+
+    /** Value of the reserved extension */
+    public static final transient int LENGTH_EXTENSION_RESERVED = 0x7F;
+
+    /** A mask to get the long form value */
+    public static final transient int SHORT_MASK = 0x007F;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The length of the following value */
+    private int length;
+
+    /** The size of the Length part. */
+    private int size;
+
+    /** If the Length is in a long form, this variable store the expected
+     * number of bytes to be read to obtain the length */
+    private transient int expectedLength;
+
+    /** Stores the number of bytes already read for a long Length form */
+    private int currentLength;
+
+    /** A flag used with definite forms length. */
+    private boolean definiteForm;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new object.
+    */
+    public Length()
+    {
+        length         = 0;
+        expectedLength = 1;
+        currentLength  = 0;
+        size           = 0;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the Length object
+     */
+    public void reset()
+    {
+        length         = 0;
+        expectedLength = 0;
+        currentLength  = 0;
+        size           = 0;
+    }
+
+    /**
+     * Get the Value length
+     *
+     * @return Returns the length of the value part.
+     */
+    public int getLength()
+    {
+
+        return length;
+    }
+
+    /**
+     * Set the length of the Value part 
+     *
+     * @param length The length of the Value part.
+     */
+    public void setLength( int length )
+    {
+        this.length = length;
+    }
+
+    /**
+     * Get the current number of bytes read
+     *
+     * @return Returns the currentLength.
+     */
+    public int getCurrentLength()
+    {
+
+        return currentLength;
+    }
+
+    /**
+     * Set the current length of the Length
+     *
+     * @param currentLength The currentLength to set.
+     */
+    public void setCurrentLength( int currentLength )
+    {
+        this.currentLength = currentLength;
+    }
+
+    /**
+     * Increment the Length being read
+     */
+    public void incCurrentLength()
+    {
+        this.currentLength++;
+    }
+
+    /**
+     * Get the expected length
+     *
+     * @return Returns the expected Length of the long form Length.
+     */
+    public int getExpectedLength()
+    {
+        return expectedLength;
+    }
+
+    /**
+     * Set the expected long form length
+     *
+     * @param expectedLength The long form expected length to set.
+     */
+    public void setExpectedLength( int expectedLength )
+    {
+        this.expectedLength = expectedLength;
+    }
+
+    /**
+     * Clone the object
+     *
+     * @return A deep copy of the Length
+     *
+     * @throws CloneNotSupportedException Thrown if any problem occurs.
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+
+        return super.clone();
+    }
+
+    /**
+     * Get the size of the Length element
+     *
+     * @return Returns the size of the Length element.
+     */
+    public int getSize()
+    {
+
+        return size;
+    }
+
+    /**
+     * Increment the size of the Length element.
+     */
+    public void incSize()
+    {
+        this.size++;
+    }
+
+    /**
+     * Return a String representing the Length
+     *
+     * @return The length
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+        sb.append( "LENGTH[" ).append( length ).append( "](" )
+          .append( definiteForm ? "definite)" : "indefinite)" ).append( "size=" ).append( size )
+          .append(
+            ")" );
+
+        return sb.toString();
+    }
+
+    /**
+     * Set the Length's size
+     *
+     * @param size The lengthSize to set.
+     */
+    public void setSize( int size )
+    {
+        this.size = size;
+    }
+
+    /**
+     * Utility function that return the number of bytes necessary to store 
+     * the length
+     * @param length The length to store in a byte array
+     * @return The number of bytes necessary to store the length.
+     */
+    public static int getNbBytes( int length )
+    {
+
+        if ( length >= 0 )
+        {
+
+            if ( length < 128 )
+            {
+                return 1;
+            }
+            else if ( length < 256 )
+            {
+                return 2;
+            }
+            else if ( length < 65536 )
+            {
+                return 3;
+            }
+            else if ( length < 16777216 )
+            {
+                return 4;
+            }
+            else
+            {
+                return 5;
+            }
+        }
+        else
+        {
+            return 5;
+        }
+    }
+
+    /**
+     * Utility function that return a byte array representing the length
+     * @param length The length to store in a byte array
+     * @return The byte array representing the length.
+     */
+    public static byte[] getBytes( int length )
+    {
+
+        byte[] bytes = new byte[getNbBytes( length )];
+
+        if ( length >= 0 )
+        {
+
+            if ( length < 128 )
+            {
+                bytes[0] = ( byte ) length;
+            }
+            else if ( length < 256 )
+            {
+                bytes[0] = ( byte ) 0x81;
+                bytes[1] = ( byte ) length;
+            }
+            else if ( length < 65536 )
+            {
+                bytes[0] = ( byte ) 0x82;
+                bytes[1] = ( byte ) ( length >> 8 );
+                bytes[2] = ( byte ) ( length & 0x00FF );
+            }
+            else if ( length < 16777216 )
+            {
+                bytes[0] = ( byte ) 0x83;
+                bytes[1] = ( byte ) ( length >> 16 );
+                bytes[2] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+                bytes[3] = ( byte ) ( length & 0x00FF );
+            }
+            else
+            {
+                bytes[0] = ( byte ) 0x84;
+                bytes[1] = ( byte ) ( length >> 24 );
+                bytes[2] = ( byte ) ( ( length >> 16 ) & 0x00FF );
+                bytes[3] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+                bytes[4] = ( byte ) ( length & 0x00FF );
+            }
+        }
+        else
+        {
+            bytes[0] = ( byte ) 0x84;
+            bytes[1] = ( byte ) ( length >> 24 );
+            bytes[2] = ( byte ) ( ( length >> 16 ) & 0x00FF );
+            bytes[3] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+            bytes[4] = ( byte ) ( length & 0x00FF );
+        }
+
+        return bytes;
+    }
+
+    /**
+     * Get the length's type 
+     * @return Returns the definiteForm flag.
+     */
+    public boolean isDefiniteForm()
+    {
+        return definiteForm;
+    }
+
+    /**
+     * Set the length's form
+     *
+     * @param definiteForm The definiteForm flag to set.
+     */
+    public void setDefiniteForm( boolean definiteForm )
+    {
+        this.definiteForm = definiteForm;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,177 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+/**
+ * This class is used to store Tag, Length and Value decoded from a PDU.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TLV
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The current Tag being processed */
+    private Tag tag;
+
+    /** The current Length being processed */
+    private Length length;
+
+    /** The current Value being processed */
+    private Value value;
+
+    /** Reference the TLV which contains the current TLV, if any.
+     * As the enclosing TLV of a PDU does not have parent, it can be 
+     * null in this case. Otherwise, it must point to a constructed TLV */
+    private TLV parent;
+    
+    /** The expected length of the TLV's elements, if the current TLV is
+     * a constructed TLV.
+     */
+    private int expectedLength;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new TLV object.
+     */
+    public TLV()
+    {
+        tag      = new Tag();
+        length   = new Length();
+        value    = new Value();
+        
+        expectedLength = 0;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the TLV, so it can be reused for the next PDU decoding.
+     */
+    public void reset()
+    {
+        tag.reset();
+        length.reset();
+        value.reset();
+
+        expectedLength = 0;
+    }
+
+    /**
+     * @return Returns the length.
+     */
+    public Length getLength()
+    {
+        return length;
+    }
+
+    /**
+     * Add the TLV Length part
+     *
+     * @param length The length to set.
+     */
+    public void setLength( Length length )
+    {
+        this.length = length;
+        
+        expectedLength = length.getLength();
+    }
+
+    /**
+     *  @return Returns the tag.
+     */
+    public Tag getTag()
+    {
+        return tag;
+    }
+
+    /**
+     * @return Returns the value.
+     */
+    public Value getValue()
+    {
+        return value;
+    }
+
+    /**
+     * Get a String representation of the TLV
+     *
+     * @return A String
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        sb.append( "TLV[ " );
+        sb.append( tag.toString() ).append( ", " );
+        sb.append( length.toString() ).append( ", " );
+        sb.append( value.toString() );
+        sb.append( "]" );
+
+        return sb.toString();
+    }
+
+	/**
+     * The TLV size is calculated by adding the Tag's size, the Length's
+     * size and the Value's length, if any.
+     * 
+	 * @return Returns the size of the TLV.
+	 */
+	public int getSize() {
+		return tag.getSize() + length.getSize() + length.getLength();
+	}
+	
+    /**
+     * @return Returns the parent.
+     */
+    public TLV getParent() 
+    {
+        return parent;
+    }
+    
+    /**
+     * @param parent The parent to set.
+     */
+    public void setParent(TLV parent) 
+    {
+        this.parent = parent;
+    }
+    
+    /**
+     * Get the TLV expected length.
+     * 
+     * @return Returns the expectedLength.
+     */
+    public int getExpectedLength() 
+    {
+        return expectedLength;
+    }
+    
+    /**
+     * Set the new expected length of the current TLV.
+     * @param expectedLength The expectedLength to set.
+     */
+    public void setExpectedLength(int expectedLength) 
+    {
+        this.expectedLength = expectedLength;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,67 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+/**
+ * Stores the different states of a PDU parsing.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TLVStateEnum
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** Start means that the deconding hasn't read the first byte */
+    public static final int TAG_STATE_START = 0x00;
+
+    /** Pending means that the Type Tag is contained in more that one byte */
+    public static final int TAG_STATE_PENDING = 0x01;
+
+    /** End means that the Type is totally read */
+    public static final int TAG_STATE_END = 0x02;
+
+    /**
+     * Overflow could have two meaning : either there are more than 5 bytes to
+     * encode the value (5 bytes = 5bits + 4*7 bits = 33 bits) or the value that
+     * is represented by those bytes is over MAX_INTEGER
+     */
+    public static final int TAG_STATE_OVERFLOW = 0x04;
+
+    /** Start means that the decoding hasn't read the first byte */
+    public static final int LENGTH_STATE_START = 0x08;
+
+    /** Pending means that the Type length is contained in more that one byte */
+    public static final int LENGTH_STATE_PENDING = 0x10;
+
+    /** End means that the Length is totally read */
+    public static final int LENGTH_STATE_END    = 0x20;
+
+    /** Start means that the decoding hasn't read the first byte */
+    public static final int VALUE_STATE_START   = 0x40;
+
+    /** Pending means that the Type Value is contained in more that one byte */
+    public static final int VALUE_STATE_PENDING = 0x80;
+
+    /** End means that the Value is totally read */
+    public static final int VALUE_STATE_END = 0x100;
+
+    /** The decoding of a TLV is done */
+    public static final int TLV_STATE_DONE = 0x200;
+
+    /** The decoding of a PDU is done */
+    public static final int PDU_DECODED = 0x400;
+}

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,378 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+import java.io.Serializable;
+
+
+/**
+ * The Tag component of a BER TLV Tuple.
+ *
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class Tag implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+
+	//~ Static fields/initializers -----------------------------------------------------------------
+
+    /** tag flag for the primitive/constructed bit - 0010 0000 - 0x20 */
+    public static final transient int CONSTRUCTED_FLAG = 0x20;
+
+    /** tag mask for the short tag format - 0001 1111 - 0x1F */
+    public static final transient int SHORT_MASK = 0x1F;
+
+    /** tag mask for the long tag format - 0111 1111 - 0x7F */
+    public static final transient int LONG_MASK = 0x7F;
+
+    /** tag flag indicating the use of the long tag encoding form */
+    public static final transient int LONG_FLAG = 0x80;
+
+    /** the max id size with one tag octet */
+    public static final transient int ONE_OCTET_IDMAX = 30;
+
+    /** the max id size with two tag octets */
+    public static final transient int TWO_OCTET_IDMAX = ( 1 << 7 ) - 1;
+
+    /** the max id size with three tag octets */
+    public static final transient int THREE_OCTET_IDMAX = ( 1 << 14 ) - 1;
+
+    /** the max id size with four tag octets */
+    public static final transient int FOUR_OCTET_IDMAX = ( 1 << 21 ) - 1;
+
+    /** the bit that signal that the value will overflow */
+    public static final transient int TAG_MAX_FLAG = ( 1 << 26 );
+
+    /** value for the universal type class */
+    public static final transient int TYPE_CLASS_UNIVERSAL = 0;
+
+    /** value for the application type class */
+    public static final transient int TYPE_CLASS_APPLICATION = 1;
+
+    /** value for the context specific type class */
+    public static final transient int TYPE_CLASS_CONTEXT_SPECIFIC = 2;
+
+    /** value for the private type class */
+    public static final transient int TYPE_CLASS_PRIVATE = 3;
+
+    /** mask to get the type class value */
+    public static final transient int TYPE_CLASS_MASK = 0xC0;
+
+    /**
+     * The maximum bytes number that could be used to hold the value. Actually,
+     * it's five : - 7 bits x 4 bytes = 28 bits, which is not enough to
+     * represent an int. - 7 bits x 5 bytes = 35 bits, which is just above int's
+     * number of bits Note : the higher bit is not used.
+     */
+    public static final transient int MAX_TAG_BYTES = 5;
+
+    /** array of the different Type classes */
+    public static final int[] TYPE_CLASS =
+    {
+        Tag.TYPE_CLASS_UNIVERSAL, Tag.TYPE_CLASS_APPLICATION, Tag.TYPE_CLASS_CONTEXT_SPECIFIC,
+        Tag.TYPE_CLASS_PRIVATE
+    };
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** the int used to store the tag octets */
+    private int id;
+
+    /** the number of octets currently read */
+    private int size;
+
+    /** whether or not this tag represents a primitive type */
+    private boolean isPrimitive;
+
+    /** the type class of this tag */
+    private int typeClass;
+
+    /** The bytes read from the PDU. We store only 5 bytes, so we can't have tag that are
+     * above 2^28 */
+    private byte[] tagBytes = new byte[] { 0, 0, 0, 0, 0 };
+
+    /** Current position in the tagBytes */
+    private int bytePos = 0;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new Tag object.
+     */
+    public Tag()
+    {
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the tag so that it can be reused. 
+     */
+    public void reset()
+    {
+        id          = 0;
+        size        = 0;
+        isPrimitive = false;
+        typeClass   = Tag.TYPE_CLASS_APPLICATION;
+        tagBytes[0] = 0;
+        tagBytes[1] = 0;
+        tagBytes[2] = 0;
+        tagBytes[3] = 0;
+        tagBytes[4] = 0;
+        bytePos     = 0;
+    }
+
+    /**
+     * Gets the id which represent the tag.
+     *
+     * @return  the id
+     */
+    public int getId()
+    {
+        return id;
+    }
+
+    /**
+     * Set the id.
+     *
+     * @param id The id to be set
+    */
+    public void setId( int id )
+    {
+        this.id = id;
+    }
+
+    /**
+     * Gets the number of octets of this Tag.
+     *
+     * @return  the number of octets of this Tag
+     */
+    public int getSize()
+    {
+        return size;
+    }
+
+    /**
+     * Gets the number of octets in this Tag.
+     *
+     * @param size The size of the tag
+    */
+    public void setSize( int size )
+    {
+        this.size = size;
+    }
+
+    /**
+     * Gets the number of octets in this Tag.
+     *
+     * */
+    public void incTagSize()
+    {
+        this.size++;
+    }
+
+    /**
+     * Gets the type class for this Tag.
+     *
+     * @return  The typeClass for this Tag
+     */
+    public int getTypeClass()
+    {
+        return typeClass;
+    }
+
+    /**
+     * Gets the type class for this Tag.
+     *
+     * @param typeClass The TypeClass to set
+    */
+    public void setTypeClass( int typeClass )
+    {
+        this.typeClass = typeClass;
+    }
+
+    /**
+     * Checks to see if the tag is constructed.
+     *
+     * @return  true if constructed, false if primitive
+     */
+    public boolean isConstructed()
+    {
+        return ! isPrimitive;
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
+     * Tells if the tag is Universal or not
+     * @return  true if it is primitive, false if it is constructed
+     */
+    public boolean isUniversal()
+    {
+        return typeClass == TYPE_CLASS_UNIVERSAL;
+    }
+
+    /**
+     * Tells if the tag class is Application or not
+     *
+     * @return  true if it is Application, false otherwise.
+     */
+    public boolean isApplication()
+    {
+        return typeClass == TYPE_CLASS_APPLICATION;
+    }
+
+    /**
+     * Tells if the tag class is Private or not
+     *
+     * @return  true if it is Private, false otherwise.
+     */
+    public boolean isPrivate()
+    {
+        return typeClass == TYPE_CLASS_PRIVATE;
+    }
+
+    /**
+     * Tells if the tag class is Contextual or not
+     *
+     * @return  true if it is Contextual, false otherwise.
+     */
+    public boolean isContextual()
+    {
+        return typeClass == TYPE_CLASS_CONTEXT_SPECIFIC;
+    }
+
+    /**
+     * Set the tag type to Primitive or Constructed
+     *
+     * @param isPrimitive The type to set
+    */
+    public void setPrimitive( boolean isPrimitive )
+    {
+        this.isPrimitive = isPrimitive;
+    }
+
+    /**
+     * Add a byte to the inner representation of the tag.
+     *
+     * @param octet The byte to add.
+     */
+    public void addByte( byte octet )
+    {
+        tagBytes[bytePos++] = octet;
+    }
+
+    /**
+     * Get the first byte of the tag.
+     *
+     * @return The first byte of the tag.
+     */
+    public byte getTagByte()
+    {
+        return tagBytes[0];
+    }
+
+    /**
+     * @return Get all the bytes of the tag
+     */
+    public byte[] getTagBytes()
+    {
+        return tagBytes;
+    }
+
+    /**
+     * Get the byte at a specific position of the tag's bytes
+     *
+     * @param pos The position
+     *
+     * @return The byte found
+     */
+    public byte getTagBytes( int pos )
+    {
+        return tagBytes[pos];
+    }
+
+    /**
+     * Clone the Tag
+     *
+     * @return A copy of the tag
+     *
+     * @throws CloneNotSupportedException Thrown if we have a cloning problem 
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+        return super.clone();
+    }
+
+    /**
+     * A string representation of a Tag
+     *
+     * @return A string representation of a Tag
+     */
+    public String toString()
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( "TAG[" );
+
+        if ( isPrimitive )
+        {
+            sb.append( "PRIMITIVE, " );
+        }
+        else
+        {
+            sb.append( "CONSTRUCTED, " );
+        }
+
+        switch ( typeClass )
+        {
+
+            case TYPE_CLASS_APPLICATION :
+                sb.append( "APPLICATION, " );
+
+                break;
+
+            case TYPE_CLASS_UNIVERSAL :
+                sb.append( "UNIVERSAL, " ).append( UniversalTag.toString( id ) );
+
+                break;
+
+            case TYPE_CLASS_PRIVATE :
+                sb.append( "PRIVATE, " ).append( id );
+
+                break;
+
+            case TYPE_CLASS_CONTEXT_SPECIFIC :
+                sb.append( "CONTEXTUAL, " ).append( id );
+
+                break;
+        }
+
+        sb.append( "](size=" ).append( size ).append( ")" );
+
+        return sb.toString();
+    }
+} // end interface ITag

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,364 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+/**
+ * Enum for ASN.1 UNIVERSAL class tags.  The tags values are
+ * constructed using the SNACC representation for tags without the
+ * primitive/constructed bit.  This is done because several bit, octet and
+ * character string types can be encoded as primitives or as constructed types
+ * to chunk the value out.
+ *
+ * <p>These tags can have one of the following values:</p>
+ *
+ * <p></p>
+ *
+ * <table border="1" cellspacing="1" width="60%">
+ *   <tr>
+ *     <th>Id</th>
+ *     <th>Usage</th>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 0]</td>
+ *     <td>reserved for BER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 1]</td>
+ *     <td>BOOLEAN</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 2]</td>
+ *     <td>INTEGER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 3]</td>
+ *     <td>BIT STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 4]</td>
+ *     <td>OCTET STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 5]</td>
+ *     <td>NULL</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 6]</td>
+ *     <td>OBJECT IDENTIFIER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 7]</td>
+ *     <td>ObjectDescriptor</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 8]</td>
+ *     <td>EXTERNAL, INSTANCE OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 9]</td>
+ *     <td>REAL</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 10]</td>
+ *     <td>ENUMERATED</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 11]</td>
+ *     <td>EMBEDDED PDV</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 12]</td>
+ *     <td>UTF8String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 13]</td>
+ *     <td>RELATIVE-OID</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 14]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 15]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 16]</td>
+ *     <td>SEQUENCE, SEQUENCE OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 17]</td>
+ *     <td>SET, SET OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 18]</td>
+ *     <td>NumericString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 19]</td>
+ *     <td>PrintableString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 20]</td>
+ *     <td>TeletexString, T61String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 21]</td>
+ *     <td>VideotexString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 22]</td>
+ *     <td>IA5String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 23]</td>
+ *     <td>UTCTime</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 24]</td>
+ *     <td>GeneralizedTime</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 25]</td>
+ *     <td>GraphicString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 26]</td>
+ *     <td>VisibleString, ISO646String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 27]</td>
+ *     <td>GeneralString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 28]</td>
+ *     <td>UniversalString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 29]</td>
+ *     <td>CHARACTER STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 30]</td>
+ *     <td>BMPString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 31]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ * </table>
+ *
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class UniversalTag
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** value for the tag */
+    public static final int RESERVED_0 = 0;
+
+    /** value for the tag */
+    public static final int BOOLEAN = 1;
+
+    /** value for the tag */
+    public static final int INTEGER = 2;
+
+    /** value for the tag */
+    public static final int BIT_STRING = 3;
+
+    /** value for the tag */
+    public static final int OCTET_STRING = 4;
+
+    /** value for the tag */
+    public static final int NULL = 5;
+
+    /** value for the tag */
+    public static final int OBJECT_IDENTIFIER = 6;
+
+    /** value for the tag */
+    public static final int OBJECT_DESCRIPTOR = 7;
+
+    /** value for the tag */
+    public static final int EXTERNAL_INSTANCE_OF = 8;
+
+    /** value for the tag */
+    public static final int REAL = 9;
+
+    /** value for the tag */
+    public static final int ENUMERATED = 10;
+
+    /** value for the tag */
+    public static final int EMBEDDED_PDV = 11;
+
+    /** value for the tag */
+    public static final int UTF8_STRING = 12;
+
+    /** value for the tag */
+    public static final int RELATIVE_OID = 13;
+
+    /** value for the tag */
+    public static final int RESERVED_14 = 14;
+
+    /** value for the tag */
+    public static final int RESERVED_15 = 15;
+
+    /** value for the tag */
+    public static final int SEQUENCE_SEQUENCE_OF = 16;
+
+    /** value for the tag */
+    public static final int SET_SET_OF = 17;
+
+    /** value for the tag */
+    public static final int NUMERIC_STRING = 18;
+
+    /** value for the tag */
+    public static final int PRINTABLE_STRING = 19;
+
+    /** value for the tag */
+    public static final int TELETEX_STRING = 20;
+
+    /** value for the tag */
+    public static final int VIDEOTEX_STRING = 21;
+
+    /** value for the tag */
+    public static final int IA5_STRING = 22;
+
+    /** value for the tag */
+    public static final int UTC_TIME = 23;
+
+    /** value for the tag */
+    public static final int GENERALIZED_TIME = 24;
+
+    /** value for the tag */
+    public static final int GRAPHIC_STRING = 25;
+
+    /** value for the tag */
+    public static final int VISIBLE_STRING = 26;
+
+    /** value for the tag */
+    public static final int GENERAL_STRING = 27;
+
+    /** value for the tag */
+    public static final int UNIVERSAL_STRING = 28;
+
+    /** value for the tag */
+    public static final int CHARACTER_STRING = 29;
+
+    /** value for the tag */
+    public static final int BMP_STRING = 30;
+
+    /** value for the tag */
+    public static final int       RESERVED_31          = 31;
+
+    /** String representation of the tags */
+    private static final String[] UNIVERSAL_TAG_STRING =
+    {
+        "RESERVED_0", 
+		"BOOLEAN", 
+		"INTEGER", 
+		"BIT_STRING", 
+		"OCTET_STRING", 
+		"NULL",
+        "OBJECT_IDENTIFIER", 
+		"OBJECT_DESCRIPTOR", 
+		"EXTERNAL_INSTANCE_OF", 
+		"REAL", 
+		"ENUMERATED",
+        "EMBEDDED_PDV", 
+		"UTF8_STRING", 
+		"RELATIVE_OID", 
+		"RESERVED_14", 
+		"RESERVED_15",
+        "SEQUENCE_SEQUENCE_OF", 
+		"SET_SET_OF", 
+		"NUMERIC_STRING", 
+		"PRINTABLE_STRING",
+        "TELETEX_STRING", 
+		"VIDEOTEX_STRING", 
+		"IA5_STRING", 
+		"UTC_TIME", 
+		"GENERALIZED_TIME",
+        "GRAPHIC_STRING", 
+		"VISIBLE_STRING", 
+		"GENERAL_STRING", 
+		"UNIVERSAL_STRING",
+        "CHARACTER_STRING", 
+		"BMP_STRING", 
+		"RESERVED_31"
+    };
+
+    /** ASN.1 primitive tag values */
+    public static final byte BOOLEAN_TAG      = 0x01;
+    public static final byte INTEGER_TAG      = 0x02;
+
+    public static final byte OCTET_STRING_TAG = 0x04;
+    
+    public static final byte ENUMERATED_TAG   = 0x0A;
+    
+    public static final byte SEQUENCE_TAG     = 0x30;
+    public static final byte SET_TAG          = 0x31;
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    // -----------------------------------------------------------------------
+    // Members
+    // -----------------------------------------------------------------------
+    /**
+     * Gets the ASN.1 UNIVERSAL type tag's enum using a tag value.
+     *
+     * @param   tag  the first octet of the TLV
+     *
+     * @return  the valued enum for the ASN.1 UNIVERSAL type tag
+     */
+    public static String toString( int tag )
+    {
+        return UNIVERSAL_TAG_STRING[tag & 0x1F];
+    }
+} // end class UniversalTag

Added: directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java?rev=234315&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java Sun Aug 21 11:51:02 2005
@@ -0,0 +1,483 @@
+/*
+ *   Copyright 2005 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.asn1new.ber.tlv;
+
+import org.apache.asn1new.EncoderException;
+import org.apache.asn1new.primitives.OID;
+import org.apache.asn1new.primitives.OctetString;
+import org.apache.asn1new.util.MutableString;
+import org.apache.asn1new.util.StringUtils;
+
+import java.io.Serializable;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+/**
+ * This class stores the data decoded from a TLV.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class Value implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+
+	//~ Instance fields ----------------------------------------------------------------------------
+
+    /** The data buffer.  
+     * TODO Create a streamed data to store large data */
+    private byte[] data;
+
+    /** The current position of the last byte in the data buffer */
+    private int    currentPos;
+    
+    /** The encoded byte for a TRUE value */ 
+    public static final byte TRUE_VALUE = (byte)0xFF;
+    
+    /** The encoded byte for a FALSE value */ 
+    public static final byte FALSE_VALUE = (byte)0x00;
+    
+    /** Pre-encoded PDUs for a TRUE and FALSE TLV */
+    private static final byte[] ENCODED_TRUE = new byte[] { 0x01, 0x01, TRUE_VALUE};
+    private static final byte[] ENCODED_FALSE = new byte[] { 0x01, 0x01, FALSE_VALUE};
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Initialize the Value
+     *
+     * @param size The data size to allocate.
+     */
+    public void init( int size )
+    {
+        data = new byte[size];
+        currentPos = 0;
+    }
+
+    /**
+     * Reset the Value so that it can be reused
+     */
+    public void reset()
+    {
+        data = null;
+        currentPos = 0;
+    }
+
+    /**
+     * Clone the Value
+     *
+     * @return An object that is a copy of this Value
+     *
+     * @throws CloneNotSupportedException Thrown when the cloning failed
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+
+        return super.clone();
+    }
+
+    /**
+     * Get the Values'data
+     *
+     * @return Returns the data.
+     */
+    public byte[] getData()
+    {
+        return data;
+    }
+
+    /**
+     * Set a block of bytes in the Value
+     *
+     * @param data The data to set.
+     */
+    public void setData( ByteBuffer data )
+    {
+        int length = data.remaining();
+        data.get( this.data, 0, length );
+        currentPos = length;
+    }
+
+    /**
+     * Append some bytes to the data buffer. 
+     *
+     * @param data The data to append.
+     */
+    public void addData( ByteBuffer data )
+    {
+        int length = data.remaining();
+        data.get( this.data, currentPos, length );
+        currentPos += length;
+    }
+
+    /**
+     * Set a block of bytes in the Value
+     *
+     * @param data The data to set.
+     */
+    public void setData( byte[] data )
+    {
+        System.arraycopy( data, 0, this.data, 0, data.length );
+        currentPos = data.length;
+    }
+
+    /**
+     * Append some bytes to the data buffer. 
+     *
+     * @param data The data to append.
+     */
+    public void addData( byte[] data )
+    {
+        System.arraycopy( data, 0, this.data, currentPos, data.length );
+        currentPos = data.length;
+    }
+
+    /**
+     * @return The number of bytes actually stored
+     */
+    public int getCurrentLength()
+    {
+        return currentPos;
+    }
+
+    /**
+     * Utility function that return the number of bytes necessary to store 
+     * the value
+     * @param value The value to store in a byte array
+     * @return The number of bytes necessary to store the value.
+     */
+    public static int getNbBytes( int value )
+    {
+
+        if ( value >= 0 )
+        {
+
+            if ( value < 256 )
+            {
+                return 1;
+            }
+            else if ( value < 65536 )
+            {
+                return 2;
+            }
+            else if ( value < 16777216 )
+            {
+                return 3;
+            }
+            else
+            {
+                return 4;
+            }
+        }
+        else
+        {
+            return 4;
+        }
+    }
+
+    /**
+     * Utility function that return a byte array representing the Value
+     * @param value The value to store in a byte array
+     * @return The byte array representing the value.
+     */
+    public static byte[] getBytes( int value )
+    {
+
+        byte[] bytes = new byte[getNbBytes( value )];
+
+        if ( value >= 0 )
+        {
+
+            if ( value < 256 )
+            {
+                bytes[0] = ( byte ) value;
+            }
+            else if ( value < 65536 )
+            {
+                bytes[0] = ( byte ) ( value >> 8 );
+                bytes[1] = ( byte ) ( value & 0x00FF );
+            }
+            else if ( value < 16777216 )
+            {
+                bytes[0] = ( byte ) ( value >> 16 );
+                bytes[1] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+                bytes[2] = ( byte ) ( value & 0x00FF );
+            }
+            else
+            {
+                bytes[0] = ( byte ) ( value >> 24 );
+                bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
+                bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+                bytes[3] = ( byte ) ( value & 0x00FF );
+            }
+        }
+        else
+        {
+            bytes[0] = ( byte ) ( value >> 24 );
+            bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
+            bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+            bytes[3] = ( byte ) ( value & 0x00FF );
+        }
+
+        return bytes;
+    }
+
+    /**
+     * Encode an String value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The String to be encoded
+     */
+    public static void encode( ByteBuffer buffer, String string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            byte[] value = string.getBytes();
+            
+            buffer.put( Length.getBytes( value.length ) );
+    
+            if ( value.length != 0 )
+            {
+                buffer.put( value );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an OctetString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The OctetString to be encoded
+     */
+    public static void encode( ByteBuffer buffer, OctetString string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( string.getLength() ) );
+    
+            if ( string.getLength() != 0 )
+            {
+                buffer.put( string.getValue() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an OID value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The OID to be encoded
+     */
+    public static void encode( ByteBuffer buffer, OID oid ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( oid.getOIDLength() ) );
+    
+            if ( oid.getOIDLength() != 0 )
+            {
+                buffer.put( oid.getOID() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode a MutableString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The MutableString to be encoded
+     */
+    public static void encode( ByteBuffer buffer, MutableString string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( string.getLength() ) );
+    
+            if ( string.getLength() != 0 )
+            {
+                buffer.put( string.getData() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an integer value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param value The integer to be encoded
+     */
+    public static void encode( ByteBuffer buffer, int value ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( UniversalTag.INTEGER_TAG );
+            buffer.put( Length.getBytes( getNbBytes( value ) ) );
+            buffer.put( getBytes( value ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an enumerated value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param value The integer to be encoded
+     */
+    public static void encodeEnumerated( ByteBuffer buffer, int value ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( UniversalTag.ENUMERATED_TAG );
+            buffer.put( Length.getBytes( getNbBytes( value ) ) );
+            buffer.put( getBytes( value ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode a boolean value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param bool The boolean to be encoded
+     */
+    public static void encode( ByteBuffer buffer, boolean bool ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( bool ? ENCODED_TRUE : ENCODED_FALSE ) ;
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Return a string representing the Value
+     *
+     * @return A string representing the value
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+        sb.append( "DATA" );
+
+        if ( data != null )
+        {
+            sb.append( '[' );
+
+            for ( int i = 0; i < data.length; i++ )
+            {
+
+                int b = ( data[i] & 0x00FF );
+
+                if ( ( b < 32 ) || ( b > 127 ) )
+                {
+                    sb.append( "0x" ).append( StringUtils.dumpByte( ( byte ) b ) );
+                }
+                else
+                {
+                    sb.append( ' ' ).append( ( char ) b );
+                }
+            }
+
+            sb.append( ']' );
+        }
+        else
+        {
+
+            return "[]";
+        }
+
+        return sb.toString();
+    }
+}



Mime
View raw message