org.opends.server.protocols.asn1
Class ASN1Element

java.lang.Object
  extended by org.opends.server.protocols.asn1.ASN1Element
All Implemented Interfaces:
java.io.Serializable, ProtocolElement
Direct Known Subclasses:
ASN1Boolean, ASN1Enumerated, ASN1Integer, ASN1Long, ASN1Null, ASN1OctetString, ASN1Sequence, ASN1Set

@PublicAPI(stability=UNCOMMITTED,
           mayInstantiate=true,
           mayExtend=false,
           mayInvoke=true)
public class ASN1Element
extends java.lang.Object
implements ProtocolElement, java.io.Serializable

This class defines the data structures and methods to use when interacting with generic ASN.1 elements. Subclasses may provide more specific functionality for individual element types.

See Also:
Serialized Form

Constructor Summary
ASN1Element(byte type)
          Creates a new ASN.1 element with the specified type and no value.
ASN1Element(byte type, byte[] value)
          Creates a new ASN.1 element with the specified type and value.
 
Method Summary
static ASN1Element decode(byte[] encodedElement)
          Decodes the contents of the provided byte array as an ASN.1 element.
static ASN1Element decode(byte[] encodedElement, int startPos, int length)
          Decodes the specified portion of the provided byte array as an ASN.1 element.
 ASN1Boolean decodeAsBoolean()
          Decodes this ASN.1 element as an ASN.1 Boolean element.
 ASN1Enumerated decodeAsEnumerated()
          Decodes this ASN.1 element as an ASN.1 enumerated element.
 ASN1Integer decodeAsInteger()
          Decodes this ASN.1 element as an ASN.1 integer element.
 ASN1Long decodeAsLong()
          Decodes this ASN.1 element as an ASN.1 long element.
 ASN1Null decodeAsNull()
          Decodes this ASN.1 element as an ASN.1 null element.
 ASN1OctetString decodeAsOctetString()
          Decodes this ASN.1 element as an ASN.1 octet string element.
 ASN1Sequence decodeAsSequence()
          Decodes this ASN.1 element as an ASN.1 sequence element.
 ASN1Set decodeAsSet()
          Decodes this ASN.1 element as an ASN.1 set element.
static java.util.ArrayList<ASN1Element> decodeElements(byte[] encodedElements)
          Decodes the provided byte array as a collection of ASN.1 elements as would be found in the value of a sequence or set.
 byte[] encode()
          Encodes this ASN.1 element to a byte array.
static byte[] encodeLength(int length)
          Encodes the provided value for use as the length of an ASN.1 element.
static byte[] encodeLongValue(long longValue)
          Retrieves a byte array containing the encoded representation of the provided long value.
static byte[] encodeValue(java.util.ArrayList<ASN1Element> elements)
          Retrieves a byte array containing the encoded representation of the provided set of ASN.1 elements.
static byte[] encodeValue(boolean booleanValue)
          Retrieves a byte array containing the encoded representation of the provided boolean value.
static byte[] encodeValue(int intValue)
          Retrieves a byte array containing the encoded representation of the provided integer value.
 boolean equals(java.lang.Object o)
          Indicates whether the provided object is equal to this ASN.1 element.
 boolean equalsElement(ASN1Element e)
          Indicates whether the provided ASN.1 element is equal to this element.
 boolean equalsIgnoreType(ASN1Element element)
          Indicates whether the provided ASN.1 element has a value that is equal to the value of this ASN.1 element.
 boolean equalsIgnoreType(ByteString byteString)
          Indicates whether the provided byte string has a value that is equal to the value of this ASN.1 element.
 java.lang.String getProtocolElementName()
          Retrieves the name of the protocol associated with this protocol element.
 byte getType()
          Retrieves the BER type for this ASN.1 element.
 int hashCode()
          Retrieves the hash code for this ASN.1 element.
 boolean isApplicationSpecific()
          Indicates whether this ASN.1 element is in the application-specific class.
 boolean isConstructed()
          Indicates whether this ASN.1 element has a constructed value.
 boolean isContextSpecific()
          Indicates whether this ASN.1 element is in the context-specific class.
 boolean isPrimitive()
          Indicates whether this ASN.1 element has a primitive value.
 boolean isPrivate()
          Indicates whether this ASN.1 element is in the private class.
 boolean isUniversal()
          Indicates whether this ASN.1 element is in the universal class.
 void setType(byte type)
          Specifies the BER type for this ASN.1 element.
 void setValue(byte[] value)
          Specifies the encoded value for this ASN.1 element.
protected  void setValueInternal(byte[] value)
          Specifies the value to use for this ASN.1 element, but without performing any validity checks.
 java.lang.String toString()
          Retrieves a string representation of this ASN.1 element.
 void toString(java.lang.StringBuilder buffer)
          Appends a string representation of this ASN.1 element to the provided buffer.
 void toString(java.lang.StringBuilder buffer, int indent)
          Appends a string representation of this protocol element to the provided buffer.
 byte[] value()
          Retrieves the encoded value for this ASN.1 element.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ASN1Element

public ASN1Element(byte type)
Creates a new ASN.1 element with the specified type and no value.

Parameters:
type - The BER type for this ASN.1 element.

ASN1Element

public ASN1Element(byte type,
                   byte[] value)
Creates a new ASN.1 element with the specified type and value.

Parameters:
type - The BER type for this ASN.1 element.
value - The encoded value for this ASN.1 element.
Method Detail

getType

public final byte getType()
Retrieves the BER type for this ASN.1 element.

Returns:
The BER type for this ASN.1 element.

setType

public final void setType(byte type)
Specifies the BER type for this ASN.1 element.

Parameters:
type - The BER type for this ASN.1 element.

isUniversal

public final boolean isUniversal()
Indicates whether this ASN.1 element is in the universal class.

Returns:
true if this ASN.1 element is in the universal class, or false if not.

isApplicationSpecific

public final boolean isApplicationSpecific()
Indicates whether this ASN.1 element is in the application-specific class.

Returns:
true if this ASN.1 element is in the application-specific class, or false if not.

isContextSpecific

public final boolean isContextSpecific()
Indicates whether this ASN.1 element is in the context-specific class.

Returns:
true if this ASN.1 element is in the context-specific class, or false if not.

isPrivate

public final boolean isPrivate()
Indicates whether this ASN.1 element is in the private class.

Returns:
true if this ASN.1 element is in the private class, or false if not.

isPrimitive

public final boolean isPrimitive()
Indicates whether this ASN.1 element has a primitive value.

Returns:
true if this ASN.1 element has a primitive value, or false if it is constructed.

isConstructed

public final boolean isConstructed()
Indicates whether this ASN.1 element has a constructed value.

Returns:
true if this ASN.1 element has a constructed value, or false if it is primitive.

value

public final byte[] value()
Retrieves the encoded value for this ASN.1 element.

Returns:
The encoded value for this ASN.1 element.

setValue

public void setValue(byte[] value)
              throws ASN1Exception
Specifies the encoded value for this ASN.1 element.

Parameters:
value - The encoded value for this ASN.1 element.
Throws:
ASN1Exception - If the provided value is not appropriate for this type of ASN.1 element.

setValueInternal

protected final void setValueInternal(byte[] value)
Specifies the value to use for this ASN.1 element, but without performing any validity checks. This should only be used by subclasses and they must ensure that it is non-null and conforms to the appropriate requirements of the underlying type.

Parameters:
value - The encoded value for this ASN.1 element.

encodeLength

public static byte[] encodeLength(int length)
Encodes the provided value for use as the length of an ASN.1 element.

Parameters:
length - The length to encode for use in an ASN.1 element.
Returns:
The byte array containing the encoded length.

encode

public final byte[] encode()
Encodes this ASN.1 element to a byte array.

Returns:
The byte array containing the encoded ASN.1 element.

encodeValue

public static byte[] encodeValue(boolean booleanValue)
Retrieves a byte array containing the encoded representation of the provided boolean value.

Parameters:
booleanValue - The boolean value to encode.
Returns:
A byte array containing the encoded representation of the provided boolean value.

encodeValue

public static byte[] encodeValue(int intValue)
Retrieves a byte array containing the encoded representation of the provided integer value.

Parameters:
intValue - The integer value to encode.
Returns:
A byte array containing the encoded representation of the provided integer value.

encodeLongValue

public static byte[] encodeLongValue(long longValue)
Retrieves a byte array containing the encoded representation of the provided long value.

Parameters:
longValue - The long value to encode.
Returns:
A byte array containing the encoded representation of the provided long value.

encodeValue

public static byte[] encodeValue(java.util.ArrayList<ASN1Element> elements)
Retrieves a byte array containing the encoded representation of the provided set of ASN.1 elements.

Parameters:
elements - The set of ASN.1 elements to encode into the value.
Returns:
A byte array containing the encoded representation of the provided set of ASN.1 elements.

decode

public static ASN1Element decode(byte[] encodedElement)
                          throws ASN1Exception
Decodes the contents of the provided byte array as an ASN.1 element.

Parameters:
encodedElement - The byte array containing the ASN.1 element to decode.
Returns:
The decoded ASN.1 element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode the byte array as an ASN.1 element.

decode

public static ASN1Element decode(byte[] encodedElement,
                                 int startPos,
                                 int length)
                          throws ASN1Exception
Decodes the specified portion of the provided byte array as an ASN.1 element.

Parameters:
encodedElement - The byte array containing the ASN.1 element to decode.
startPos - The position in the provided array at which to start decoding.
length - The number of bytes in the set of data to decode as an ASN.1 element.
Returns:
The decoded ASN.1 element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode the byte array as an ASN.1 element.

decodeAsBoolean

public final ASN1Boolean decodeAsBoolean()
                                  throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 Boolean element.

Returns:
The ASN.1 Boolean element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 Boolean element.

decodeAsEnumerated

public final ASN1Enumerated decodeAsEnumerated()
                                        throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 enumerated element.

Returns:
The ASN.1 enumerated element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 enumerated element.

decodeAsInteger

public final ASN1Integer decodeAsInteger()
                                  throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 integer element.

Returns:
The ASN.1 integer element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 integer element.

decodeAsLong

public final ASN1Long decodeAsLong()
                            throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 long element.

Returns:
The ASN.1 long element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 long element.

decodeAsNull

public final ASN1Null decodeAsNull()
                            throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 null element.

Returns:
The ASN.1 null element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 null element.

decodeAsOctetString

public final ASN1OctetString decodeAsOctetString()
                                          throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 octet string element.

Returns:
The ASN.1 octet string element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 octet string element.

decodeAsSequence

public final ASN1Sequence decodeAsSequence()
                                    throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 sequence element.

Returns:
The ASN.1 sequence element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 sequence element.

decodeAsSet

public final ASN1Set decodeAsSet()
                          throws ASN1Exception
Decodes this ASN.1 element as an ASN.1 set element.

Returns:
The ASN.1 set element decoded from this element.
Throws:
ASN1Exception - If a problem occurs while attempting to decode this element as an ASN.1 set element.

decodeElements

public static java.util.ArrayList<ASN1Element> decodeElements(byte[] encodedElements)
                                                       throws ASN1Exception
Decodes the provided byte array as a collection of ASN.1 elements as would be found in the value of a sequence or set.

Parameters:
encodedElements - The byte array containing the data to decode.
Returns:
The set of decoded ASN.1 elements.
Throws:
ASN1Exception - If a problem occurs while attempting to decode the set of ASN.1 elements from the provided byte array.

getProtocolElementName

public final java.lang.String getProtocolElementName()
Retrieves the name of the protocol associated with this protocol element.

Specified by:
getProtocolElementName in interface ProtocolElement
Returns:
The name of the protocol associated with this protocol element.

equals

public final boolean equals(java.lang.Object o)
Indicates whether the provided object is equal to this ASN.1 element.

Overrides:
equals in class java.lang.Object
Parameters:
o - The object for which to make the determination.
Returns:
true if the provided object is an ASN.1 element that is equal to this element, or false if not. The object will be considered equal if it is an ASN.1 element (or a subclass) with the same type and encoded value.

equalsIgnoreType

public final boolean equalsIgnoreType(ASN1Element element)
Indicates whether the provided ASN.1 element has a value that is equal to the value of this ASN.1 element.

Parameters:
element - The ASN.1 element whose value should be compared against the value of this element.
Returns:
true if the values of the elements are equal, or false if not.

equalsIgnoreType

public final boolean equalsIgnoreType(ByteString byteString)
Indicates whether the provided byte string has a value that is equal to the value of this ASN.1 element.

Parameters:
byteString - The byte string whose value should be compared against the value of this element.
Returns:
true if the values are equal, or false if not.

equalsElement

public final boolean equalsElement(ASN1Element e)
Indicates whether the provided ASN.1 element is equal to this element.

Parameters:
e - The ASN.1 element for which to make the determination.
Returns:
true ASN.1 element is equal to this element, or false if not. The elements will be considered equal if they have the same type and encoded value.

hashCode

public final int hashCode()
Retrieves the hash code for this ASN.1 element. It will be constructed from the sum of the type and up to the first twenty bytes of the value.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code for this ASN.1 element.

toString

public final java.lang.String toString()
Retrieves a string representation of this ASN.1 element.

Specified by:
toString in interface ProtocolElement
Overrides:
toString in class java.lang.Object
Returns:
A string representation of this ASN.1 element.

toString

public void toString(java.lang.StringBuilder buffer)
Appends a string representation of this ASN.1 element to the provided buffer.

Specified by:
toString in interface ProtocolElement
Parameters:
buffer - The buffer to which the information should be appended.

toString

public void toString(java.lang.StringBuilder buffer,
                     int indent)
Appends a string representation of this protocol element to the provided buffer.

Specified by:
toString in interface ProtocolElement
Parameters:
buffer - The buffer into which the string representation should be written.
indent - The number of spaces that should be used to indent the resulting string representation.