com.sun.slamd.asn1
Class ASN1Element

java.lang.Object
  extended bycom.sun.slamd.asn1.ASN1Element
Direct Known Subclasses:
ASN1Boolean, ASN1Enumerated, ASN1Integer, ASN1Null, ASN1OctetString, ASN1Sequence, ASN1Set

public class ASN1Element
extends java.lang.Object

This class defines a generic ASN.1 element and a set of core methods for dealing with them. Subclasses may deal with more specific kinds of ASN.1 elements.


Field Summary
static byte ASN1_BOOLEAN_TYPE
          The standard ASN.1 type for boolean values.
static byte ASN1_ENUMERATED_TYPE
          The standard ASN.1 type for enumerated values.
static byte ASN1_INTEGER_TYPE
          The standard ASN.1 type for integer values.
static byte ASN1_NULL_TYPE
          The standard ASN.1 type for null values.
static byte ASN1_OCTET_STRING_TYPE
          The standard ASN.1 type for octet string values.
static byte ASN1_SEQUENCE_TYPE
          The standard ASN.1 type for sequence values.
static byte ASN1_SET_TYPE
          The standard ASN.1 type for set values.
static byte[] EMPTY_BYTES
          An empty byte array, used to prevent multiple allocations for empty arrays.
 
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.
ASN1Element(byte type, byte[] value, byte[] encodedElement)
          Creates a new ASN.1 element with the specified information.
 
Method Summary
protected static int byteArrayToInt(byte[] byteArray)
          Converts the provided byte array into a Java int value.
static java.lang.String byteArrayToString(byte[] byteArray)
          Retrieves a string containing the hexadecimal digits contained in the provided byte array.
static java.lang.String byteArrayToString(byte[] byteArray, int indent)
          Retrieves a string containing the hexadecimal digits contained in the provided byte array.
static ASN1Element decode(byte[] encodedValue)
          Decodes the provided byte array as a generic ASN.1 element.
 ASN1Boolean decodeAsBoolean()
          Decodes this element as an ASN.1 Boolean element.
static ASN1Boolean decodeAsBoolean(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 Boolean element.
 ASN1Enumerated decodeAsEnumerated()
          Decodes this element as an ASN.1 enumerated element.
static ASN1Enumerated decodeAsEnumerated(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 enumerated element.
 ASN1Integer decodeAsInteger()
          Decodes this element as an ASN.1 integer element.
static ASN1Integer decodeAsInteger(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 integer element.
 ASN1Null decodeAsNull()
          Decodes this element as an ASN.1 null element.
static ASN1Null decodeAsNull(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 null element.
 ASN1OctetString decodeAsOctetString()
          Decodes this element as an ASN.1 octet string element.
static ASN1OctetString decodeAsOctetString(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 octet string element.
 ASN1Sequence decodeAsSequence()
          Decodes this element as an ASN.1 sequence element.
static ASN1Sequence decodeAsSequence(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 sequence element.
 ASN1Set decodeAsSet()
          Decodes this element as an ASN.1 set element.
static ASN1Set decodeAsSet(byte[] encodedValue)
          Decodes the provided byte array as an ASN.1 set element.
static int decodeLength(byte[] encodedLength)
          Decodes the provided byte array as a length.
 byte[] encode()
          Encodes this ASN.1 element into a byte array.
 int encode(byte[] byteArray, int startPos)
          Encodes this ASN.1 element into the provided byte array at the indicated position.
protected static byte[] encodeLength(int length)
          Encodes the specified length as a byte array as it should appear in an ASN.1 element.
static byte[] getBytes(java.lang.String stringValue)
          Retrieves a byte array containing the binary representation of the provided string.
 byte getType()
          Gets the type of this ASN.1 element.
 byte getTypeWithoutFlags()
          Gets the type of this ASN.1 element without either of the class bits or the primitive/constructed bit set (i.e., the least significant five bits).
 byte[] getValue()
          Gets the encoded value for this ASN.1 element.
 void setType(byte type)
          Specifies the type for this ASN.1 element.
 void setValue(byte[] value)
          Specifies the value for this ASN.1 element.
 java.lang.String toString()
          Retrieves a string representation of this ASN.1 element.
 java.lang.String toString(int indent)
          Retrieves a string representation of this ASN.1 element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ASN1_BOOLEAN_TYPE

public static final byte ASN1_BOOLEAN_TYPE
The standard ASN.1 type for boolean values.

See Also:
Constant Field Values

ASN1_INTEGER_TYPE

public static final byte ASN1_INTEGER_TYPE
The standard ASN.1 type for integer values.

See Also:
Constant Field Values

ASN1_OCTET_STRING_TYPE

public static final byte ASN1_OCTET_STRING_TYPE
The standard ASN.1 type for octet string values.

See Also:
Constant Field Values

ASN1_NULL_TYPE

public static final byte ASN1_NULL_TYPE
The standard ASN.1 type for null values.

See Also:
Constant Field Values

ASN1_ENUMERATED_TYPE

public static final byte ASN1_ENUMERATED_TYPE
The standard ASN.1 type for enumerated values.

See Also:
Constant Field Values

ASN1_SEQUENCE_TYPE

public static final byte ASN1_SEQUENCE_TYPE
The standard ASN.1 type for sequence values.

See Also:
Constant Field Values

ASN1_SET_TYPE

public static final byte ASN1_SET_TYPE
The standard ASN.1 type for set values.

See Also:
Constant Field Values

EMPTY_BYTES

public static final byte[] EMPTY_BYTES
An empty byte array, used to prevent multiple allocations for empty arrays.

Constructor Detail

ASN1Element

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

Parameters:
type - The type of 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 type of this ASN.1 element.
value - The encoded value for this ASN.1 element.

ASN1Element

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

Parameters:
type - The type of this ASN.1 element.
value - The value for this ASN.1 element.
encodedElement - The ASN.1 element encoded as a byte array.
Method Detail

getType

public byte getType()
Gets the type of this ASN.1 element.

Returns:
The type of this ASN.1 element.

getTypeWithoutFlags

public byte getTypeWithoutFlags()
Gets the type of this ASN.1 element without either of the class bits or the primitive/constructed bit set (i.e., the least significant five bits).

Returns:
The type of this ASN.1 element without any of the flag bits set.

setType

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

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

getValue

public byte[] getValue()
Gets the encoded value for this ASN.1 element.

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

setValue

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

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

encodeLength

protected static byte[] encodeLength(int length)
Encodes the specified length as a byte array as it should appear in an ASN.1 element.

Parameters:
length - The length value to be encoded.
Returns:
The encoded length as a byte array.

decodeLength

public static int decodeLength(byte[] encodedLength)
                        throws ASN1Exception
Decodes the provided byte array as a length.

Parameters:
encodedLength - The encoded value to decode as a length.
Returns:
The length decoded from the provided byte array.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 length.

encode

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

Returns:
This ASN.1 element encoded as a byte array.

encode

public int encode(byte[] byteArray,
                  int startPos)
Encodes this ASN.1 element into the provided byte array at the indicated position.

Parameters:
byteArray - The byte array into which the value is to be encoded.
startPos - The position in the byte array at which to start writing the encoded value.
Returns:
The number of bytes written into the array, or -1 if the array was not big enough to hold the encoded value.

decode

public static ASN1Element decode(byte[] encodedValue)
                          throws ASN1Exception
Decodes the provided byte array as a generic ASN.1 element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 element.

decodeAsBoolean

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

Returns:
The decoded ASN.1 Boolean element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 Boolean element.

decodeAsBoolean

public static ASN1Boolean decodeAsBoolean(byte[] encodedValue)
                                   throws ASN1Exception
Decodes the provided byte array as an ASN.1 Boolean element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 Boolean element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 Boolean element.

decodeAsInteger

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

Returns:
The decoded ASN.1 integer element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 integer element.

decodeAsInteger

public static ASN1Integer decodeAsInteger(byte[] encodedValue)
                                   throws ASN1Exception
Decodes the provided byte array as an ASN.1 integer element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 integer element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 integer element.

decodeAsOctetString

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

Returns:
The decoded ASN.1 octet string element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 octet string element.

decodeAsOctetString

public static ASN1OctetString decodeAsOctetString(byte[] encodedValue)
                                           throws ASN1Exception
Decodes the provided byte array as an ASN.1 octet string element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 octet string element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 octet string element.

decodeAsNull

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

Returns:
The decoded ASN.1 null element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 null element.

decodeAsNull

public static ASN1Null decodeAsNull(byte[] encodedValue)
                             throws ASN1Exception
Decodes the provided byte array as an ASN.1 null element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 null element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 null element.

decodeAsEnumerated

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

Returns:
The decoded ASN.1 enumerated element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 enumerated element.

decodeAsEnumerated

public static ASN1Enumerated decodeAsEnumerated(byte[] encodedValue)
                                         throws ASN1Exception
Decodes the provided byte array as an ASN.1 enumerated element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 enumerated element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 enumerated element.

decodeAsSequence

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

Returns:
The decoded ASN.1 sequence element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 sequence element.

decodeAsSequence

public static ASN1Sequence decodeAsSequence(byte[] encodedValue)
                                     throws ASN1Exception
Decodes the provided byte array as an ASN.1 sequence element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 sequence element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 sequence element.

decodeAsSet

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

Returns:
The decoded ASN.1 set element.
Throws:
ASN1Exception - If this element cannot be decoded as an ASN.1 set element.

decodeAsSet

public static ASN1Set decodeAsSet(byte[] encodedValue)
                           throws ASN1Exception
Decodes the provided byte array as an ASN.1 set element.

Parameters:
encodedValue - The encoded ASN.1 element.
Returns:
The decoded ASN.1 set element.
Throws:
ASN1Exception - If the provided byte array cannot be decoded as an ASN.1 set element.

byteArrayToInt

protected static int byteArrayToInt(byte[] byteArray)
Converts the provided byte array into a Java int value. Note that this method assumes that the provided array contains no more than four bytes.

Parameters:
byteArray - The byte array containing the encoded integer value.
Returns:
The Java int value decoded from the byte array.

toString

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

Returns:
A string representation of this ASN.1 element.

toString

public java.lang.String toString(int indent)
Retrieves a string representation of this ASN.1 element. The information will be indented the specified number of spaces.

Parameters:
indent - The number of spaces to indent each line of the output.
Returns:
A string representation of this ASN.1 element.

byteArrayToString

public static java.lang.String byteArrayToString(byte[] byteArray)
Retrieves a string containing the hexadecimal digits contained in the provided byte array.

Parameters:
byteArray - The byte array containing the information to be output.
Returns:
A string containing the hexadecimal digits contained in the provided byte array.

byteArrayToString

public static java.lang.String byteArrayToString(byte[] byteArray,
                                                 int indent)
Retrieves a string containing the hexadecimal digits contained in the provided byte array. Each line will be indented the specified number of spaces.

Parameters:
byteArray - The byte array containing the information to be output.
indent - The number of spaces to indent each line of the output.
Returns:
A string containing the hexadecimal digits contained in the provided byte array.

getBytes

public static byte[] getBytes(java.lang.String stringValue)
Retrieves a byte array containing the binary representation of the provided string. If the provided string is 7-bit clean, then this method is about 5 times faster than the standard Java String.getBytes() method.

Parameters:
stringValue - The string for which to retrieve the binary representation.
Returns:
A byte array containing the binary representation of the provided string.