org.apache.asn1new.ber
Class Asn1Decoder

java.lang.Object
  extended byorg.apache.asn1new.ber.Asn1Decoder
All Implemented Interfaces:
ITLVBerDecoderMBean

public class Asn1Decoder
extends java.lang.Object
implements ITLVBerDecoderMBean

A BER TLV Tag component decoder. This decoder instanciate a Tag. The tag won't be implementations should not copy the handle to the Tag object delivered but should copy the data if they need it over the long term.

Author:
Apache Directory Project

Field Summary
private static boolean DEBUG
          A speedup for logs
private static boolean END
          This flag is used to indicate that there are no more bytes in the stream
private  boolean indefiniteLengthAllowed
          Flag that is used to allow/disallow the indefinite form of Length
private static org.apache.log4j.Logger log
          The logger
private  int maxLengthLength
          The maximum number of bytes that could be used to encode the Length
private  int maxTagLength
          The maximum number of bytes that could be used to encode the Tag
private static boolean MORE
          This flag is used to indicate that there are more bytes in the stream
 
Constructor Summary
Asn1Decoder()
          A public constructor of an Asn1 Decoder.
 
Method Summary
 void allowIndefiniteLength()
          Allow indefinite length.
 void decode(java.nio.ByteBuffer stream, IAsn1Container container)
          The decoder main function.
 void disallowIndefiniteLength()
          Disallow indefinite length.
private  void dumpTLVTree(IAsn1Container container)
          Dump the current TLV tree
 int getMaxLengthLength()
          Get the length's Length.
 int getMaxTagLength()
          Get the maximum Tag's length
private  java.lang.String getParentLength(TLV tlv)
          A debug function used to dump the expected length stack.
 boolean isIndefiniteLengthAllowed()
          Tells if indefinite length form could be used for Length
private  boolean isTLVDecoded(IAsn1Container container)
          Check if the TLV tree is fully decoded
 void setMaxLengthLength(int maxLengthLength)
          Set the maximul length for a Length
 void setMaxTagLength(int maxTagLength)
          Set the maximum Tag length
private  java.lang.String stateToString(int state)
          An helper function that return a string representing the current state for debuging purpose.
private  void treatLengthEndState(IAsn1Container container)
          The Length is fully decoded.
private  boolean treatLengthPendingState(java.nio.ByteBuffer stream, IAsn1Container container)
          This function is called when a Length is in the process of being decoded, but the lack of bytes in the buffer stopped the process.
private  boolean treatLengthStartState(java.nio.ByteBuffer stream, IAsn1Container container)
          Treat the Length start.
private  void treatTagEndState(IAsn1Container container)
          Action to be executed when the Tag has been decoded.
private  boolean treatTagPendingState(java.nio.ByteBuffer stream, IAsn1Container container)
          Treat a tag that is more than one byte long if the stream was cut in pieces.
private  boolean treatTagStartState(java.nio.ByteBuffer stream, IAsn1Container container)
          Treat the start of a TLV.
private  boolean treatTLVDoneState(java.nio.ByteBuffer stream, IAsn1Container container)
          When the TLV has been fully decoded, we have to execute the associated action and switch to the next TLV, which will start with a Tag.
private  boolean treatValuePendingState(java.nio.ByteBuffer stream, IAsn1Container container)
          Treat a pending Value when we get more bytes in the buffer.
private  boolean treatValueStartState(java.nio.ByteBuffer stream, IAsn1Container container)
          Treat the Value part.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.log4j.Logger log
The logger


DEBUG

private static boolean DEBUG
A speedup for logs


MORE

private static final boolean MORE
This flag is used to indicate that there are more bytes in the stream

See Also:
Constant Field Values

END

private static final boolean END
This flag is used to indicate that there are no more bytes in the stream

See Also:
Constant Field Values

indefiniteLengthAllowed

private boolean indefiniteLengthAllowed
Flag that is used to allow/disallow the indefinite form of Length


maxLengthLength

private int maxLengthLength
The maximum number of bytes that could be used to encode the Length


maxTagLength

private int maxTagLength
The maximum number of bytes that could be used to encode the Tag

Constructor Detail

Asn1Decoder

public Asn1Decoder()
A public constructor of an Asn1 Decoder.

Method Detail

treatTagStartState

private boolean treatTagStartState(java.nio.ByteBuffer stream,
                                   IAsn1Container container)
                            throws org.apache.asn1.codec.DecoderException
Treat the start of a TLV. It reads the tag and get its value.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - If something went wrong.

treatTagPendingState

private boolean treatTagPendingState(java.nio.ByteBuffer stream,
                                     IAsn1Container container)
                              throws org.apache.asn1.codec.DecoderException
Treat a tag that is more than one byte long if the stream was cut in pieces. This function is called when some new bytes where got from the stream.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

dumpTLVTree

private void dumpTLVTree(IAsn1Container container)
Dump the current TLV tree

Parameters:
container - The container

isTLVDecoded

private boolean isTLVDecoded(IAsn1Container container)
Check if the TLV tree is fully decoded

Parameters:
container - The container

treatTagEndState

private void treatTagEndState(IAsn1Container container)
                       throws org.apache.asn1.codec.DecoderException
Action to be executed when the Tag has been decoded. Basically, this is a debug action. We will log the information that the Tag has been decoded.

Parameters:
container - The container that stores the current state, the result and other informations.
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

treatLengthStartState

private boolean treatLengthStartState(java.nio.ByteBuffer stream,
                                      IAsn1Container container)
                               throws org.apache.asn1.codec.DecoderException
Treat the Length start. The tag has been decoded, so we have to deal with the LENGTH, which can be multi-bytes.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

treatLengthPendingState

private boolean treatLengthPendingState(java.nio.ByteBuffer stream,
                                        IAsn1Container container)
                                 throws org.apache.asn1.codec.DecoderException
This function is called when a Length is in the process of being decoded, but the lack of bytes in the buffer stopped the process.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

getParentLength

private java.lang.String getParentLength(TLV tlv)
A debug function used to dump the expected length stack.

Parameters:
tlv - The current TLV.
Returns:
A string which represent the expected length stack.

treatLengthEndState

private void treatLengthEndState(IAsn1Container container)
                          throws org.apache.asn1.codec.DecoderException
The Length is fully decoded. We have to call an action to check the size.

Parameters:
container - The container that stores the current state, the result and other informations.
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

treatValueStartState

private boolean treatValueStartState(java.nio.ByteBuffer stream,
                                     IAsn1Container container)
                              throws org.apache.asn1.codec.DecoderException
Treat the Value part. We will distinguish two cases : - if the Tag is a Primitive one, we will get the value. - if the Tag is a Constructed one, nothing will be done.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

treatValuePendingState

private boolean treatValuePendingState(java.nio.ByteBuffer stream,
                                       IAsn1Container container)
                                throws org.apache.asn1.codec.DecoderException
Treat a pending Value when we get more bytes in the buffer.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
MORE if some bytes remain in the buffer when the value has been decoded, END if whe still need to get some more bytes.
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

treatTLVDoneState

private boolean treatTLVDoneState(java.nio.ByteBuffer stream,
                                  IAsn1Container container)
                           throws org.apache.asn1.codec.DecoderException
When the TLV has been fully decoded, we have to execute the associated action and switch to the next TLV, which will start with a Tag.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that stores the current state, the result and other informations.
Returns:
true if there are more bytes to read, false otherwise
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong

stateToString

private java.lang.String stateToString(int state)
An helper function that return a string representing the current state for debuging purpose.

Parameters:
state - The state
Returns:
A string representation of the state

decode

public void decode(java.nio.ByteBuffer stream,
                   IAsn1Container container)
            throws org.apache.asn1.codec.DecoderException
The decoder main function. This is where we read bytes from the stream and go through the automaton. It's an inifnite loop which stop when no more bytes are to be read. It can occurs if the ByteBuffer is exhausted or if the PDU has been fully decoded.

Parameters:
stream - The ByteBuffer containing the PDU to decode
container - The container that store the state, the result and other elements.
Throws:
org.apache.asn1.codec.DecoderException - Thrown if anything went wrong!

getMaxLengthLength

public int getMaxLengthLength()
Get the length's Length.

Specified by:
getMaxLengthLength in interface ITLVBerDecoderMBean
Returns:
Returns the length's Length.

getMaxTagLength

public int getMaxTagLength()
Get the maximum Tag's length

Specified by:
getMaxTagLength in interface ITLVBerDecoderMBean
Returns:
Returns the maximum tag Length.

disallowIndefiniteLength

public void disallowIndefiniteLength()
Disallow indefinite length.

Specified by:
disallowIndefiniteLength in interface ITLVBerDecoderMBean

allowIndefiniteLength

public void allowIndefiniteLength()
Allow indefinite length.

Specified by:
allowIndefiniteLength in interface ITLVBerDecoderMBean

isIndefiniteLengthAllowed

public boolean isIndefiniteLengthAllowed()
Tells if indefinite length form could be used for Length

Specified by:
isIndefiniteLengthAllowed in interface ITLVBerDecoderMBean
Returns:
Returns true if the current decoder support indefinite length

setMaxLengthLength

public void setMaxLengthLength(int maxLengthLength)
                        throws org.apache.asn1.codec.DecoderException
Set the maximul length for a Length

Specified by:
setMaxLengthLength in interface ITLVBerDecoderMBean
Parameters:
maxLengthLength - The lengthLength to set.
Throws:
org.apache.asn1.codec.DecoderException - Thrown if the indefinite length is allowed or if the length's Length is above 126 bytes

setMaxTagLength

public void setMaxTagLength(int maxTagLength)
Set the maximum Tag length

Specified by:
setMaxTagLength in interface ITLVBerDecoderMBean
Parameters:
maxTagLength - The tagLength to set.


Copyright © 2005-2006 . All Rights Reserved.