Package com.unboundid.asn1
Class ASN1Element
java.lang.Object
com.unboundid.asn1.ASN1Element
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ASN1BigInteger,ASN1BitString,ASN1Boolean,ASN1Enumerated,ASN1GeneralizedTime,ASN1IA5String,ASN1Integer,ASN1Long,ASN1Null,ASN1NumericString,ASN1ObjectIdentifier,ASN1OctetString,ASN1PrintableString,ASN1Sequence,ASN1Set,ASN1UTCTime,ASN1UTF8String
@NotExtensible
@NotMutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public class ASN1Element
extends Object
implements Serializable
This class defines a generic ASN.1 BER element, which has a type and value.
It provides a framework for encoding and decoding BER elements, both as
generic elements and more specific subtypes.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionASN1Element(byte type) Creates a new ASN.1 BER element with the specified type and no value.ASN1Element(byte type, byte[] value) Creates a new ASN1 BER element with the specified type and value.ASN1Element(byte type, byte[] value, int offset, int length) Creates a new ASN1 BER element with the specified type and value. -
Method Summary
Modifier and TypeMethodDescriptionstatic ASN1Elementdecode(byte[] elementBytes) Decodes the content in the provided byte array as an ASN.1 element.final ASN1BigIntegerDecodes this ASN.1 element as a big integer element.final ASN1BitStringDecodes this ASN.1 element as a bit string element.final ASN1BooleanDecodes this ASN.1 element as a Boolean element.final ASN1EnumeratedDecodes this ASN.1 element as an enumerated element.final ASN1GeneralizedTimeDecodes this ASN.1 element as a generalized time element.final ASN1IA5StringDecodes this ASN.1 element as an IA5 string element.final ASN1IntegerDecodes this ASN.1 element as an integer element.final ASN1LongDecodes this ASN.1 element as a long element.final ASN1NullDecodes this ASN.1 element as a null element.final ASN1NumericStringDecodes this ASN.1 element as a numeric string element.final ASN1ObjectIdentifierDecodes this ASN.1 element as an object identifier element.final ASN1OctetStringDecodes this ASN.1 element as an octet string element.final ASN1PrintableStringDecodes this ASN.1 element as a printable string element.final ASN1SequenceDecodes this ASN.1 element as a sequence element.final ASN1SetDecodes this ASN.1 element as a set element.final ASN1UTCTimeDecodes this ASN.1 element as a UTC time element.final ASN1UTF8StringDecodes this ASN.1 element as a UTF-8 string element.final byte[]encode()Encodes this ASN.1 element to a byte array.static byte[]encodeLength(int length) Encodes the provided length to a byte array.voidencodeTo(ByteStringBuffer buffer) Appends an encoded representation of this ASN.1 element to the provided buffer.final booleanIndicates whether the provided object is equal to this ASN.1 BER element.final booleanequalsIgnoreType(ASN1Element element) Indicates whether the provided ASN.1 element is equal to this element, ignoring any potential difference in the BER type.final bytegetType()Retrieves the BER type for this element.byteRetrieves a value that corresponds to the type class for this element.byte[]getValue()Retrieves the encoded value for this element.intRetrieves the number of bytes contained in the value.final inthashCode()Retrieves a hash code for this ASN.1 BER element.booleanIndicates whether the type indicates that this element is constructed.static ASN1ElementreadFrom(InputStream inputStream) Reads an ASN.1 element from the provided input stream.static ASN1ElementreadFrom(InputStream inputStream, int maxSize) Reads an ASN.1 element from the provided input stream.final StringtoString()Retrieves a string representation of the value for ASN.1 element.voidtoString(StringBuilder buffer) Appends a string representation of the value for this ASN.1 element to the provided buffer.final intwriteTo(OutputStream outputStream) Writes an encoded representation of this ASN.1 element to the provided output stream.
-
Constructor Details
-
ASN1Element
Creates a new ASN.1 BER element with the specified type and no value.- Parameters:
type- The BER type for this element.
-
ASN1Element
Creates a new ASN1 BER element with the specified type and value.- Parameters:
type- The BER type for this element.value- The encoded value for this element.
-
ASN1Element
Creates a new ASN1 BER element with the specified type and value.- Parameters:
type- The BER type for this element.value- The array containing the encoded value for this element. It must not benull.offset- The offset within the array at which the value begins.length- The number of bytes contained in the value.
-
-
Method Details
-
getType
Retrieves the BER type for this element.- Returns:
- The BER type for this element.
-
getTypeClass
Retrieves a value that corresponds to the type class for this element. The value returned will be one ofASN1Constants.TYPE_MASK_UNIVERSAL_CLASS,ASN1Constants.TYPE_MASK_APPLICATION_CLASS,ASN1Constants.TYPE_MASK_CONTEXT_SPECIFIC_CLASS, orASN1Constants.TYPE_MASK_PRIVATE_CLASS.- Returns:
- A value that corresponds to the type class for this element.
-
isConstructed
Indicates whether the type indicates that this element is constructed. A constructed element is one whose value is comprised of the encoded representation of zero or more ASN.1 elements. If the type does not indicate that the element is constructed, then the element is considered primitive.- Returns:
trueif the type indicates that the element is constructed, orfalseif the type indicates that the element is primitive.
-
getValueLength
Retrieves the number of bytes contained in the value.- Returns:
- The number of bytes contained in the value.
-
getValue
Retrieves the encoded value for this element.- Returns:
- The encoded value for this element.
-
encode
Encodes this ASN.1 element to a byte array.- Returns:
- A byte array containing the encoded representation of this ASN.1 element.
-
encodeTo
Appends an encoded representation of this ASN.1 element to the provided buffer.- Parameters:
buffer- The buffer to which the encoded representation should be appended.
-
encodeLength
Encodes the provided length to a byte array.- Parameters:
length- The length to be encoded.- Returns:
- A byte array containing the encoded length.
-
decode
Decodes the content in the provided byte array as an ASN.1 element.- Parameters:
elementBytes- The byte array containing the data to decode.- Returns:
- The decoded ASN.1 BER element.
- Throws:
ASN1Exception- If the provided byte array does not represent a valid ASN.1 element.
-
decodeAsBitString
Decodes this ASN.1 element as a bit string element.- Returns:
- The decoded bit string element.
- Throws:
ASN1Exception- If this element cannot be decoded as a bit string element.
-
decodeAsBoolean
Decodes this ASN.1 element as a Boolean element.- Returns:
- The decoded Boolean element.
- Throws:
ASN1Exception- If this element cannot be decoded as a Boolean element.
-
decodeAsEnumerated
Decodes this ASN.1 element as an enumerated element.- Returns:
- The decoded enumerated element.
- Throws:
ASN1Exception- If this element cannot be decoded as an enumerated element.
-
decodeAsGeneralizedTime
Decodes this ASN.1 element as a generalized time element.- Returns:
- The decoded generalized time element.
- Throws:
ASN1Exception- If this element cannot be decoded as a generalized time element.
-
decodeAsIA5String
Decodes this ASN.1 element as an IA5 string element.- Returns:
- The decoded IA5 string element.
- Throws:
ASN1Exception- If this element cannot be decoded as a IA5 string element.
-
decodeAsInteger
Decodes this ASN.1 element as an integer element.- Returns:
- The decoded integer element.
- Throws:
ASN1Exception- If this element cannot be decoded as an integer element.
-
decodeAsLong
Decodes this ASN.1 element as a long element.- Returns:
- The decoded long element.
- Throws:
ASN1Exception- If this element cannot be decoded as a long element.
-
decodeAsBigInteger
Decodes this ASN.1 element as a big integer element.- Returns:
- The decoded big integer element.
- Throws:
ASN1Exception- If this element cannot be decoded as a big integer element.
-
decodeAsNull
Decodes this ASN.1 element as a null element.- Returns:
- The decoded null element.
- Throws:
ASN1Exception- If this element cannot be decoded as a null element.
-
decodeAsNumericString
Decodes this ASN.1 element as a numeric string element.- Returns:
- The decoded numeric string element.
- Throws:
ASN1Exception- If this element cannot be decoded as a numeric string element.
-
decodeAsObjectIdentifier
Decodes this ASN.1 element as an object identifier element.- Returns:
- The decoded object identifier element.
- Throws:
ASN1Exception- If this element cannot be decoded as an object identifier element.
-
decodeAsOctetString
Decodes this ASN.1 element as an octet string element.- Returns:
- The decoded octet string element.
-
decodeAsPrintableString
Decodes this ASN.1 element as a printable string element.- Returns:
- The decoded printable string element.
- Throws:
ASN1Exception- If this element cannot be decoded as a printable string element.
-
decodeAsSequence
Decodes this ASN.1 element as a sequence element.- Returns:
- The decoded sequence element.
- Throws:
ASN1Exception- If this element cannot be decoded as a sequence element.
-
decodeAsSet
Decodes this ASN.1 element as a set element.- Returns:
- The decoded set element.
- Throws:
ASN1Exception- If this element cannot be decoded as a set element.
-
decodeAsUTCTime
Decodes this ASN.1 element as a UTC time element.- Returns:
- The decoded UTC time element.
- Throws:
ASN1Exception- If this element cannot be decoded as a UTC time element.
-
decodeAsUTF8String
Decodes this ASN.1 element as a UTF-8 string element.- Returns:
- The decoded UTF_8 string element.
- Throws:
ASN1Exception- If this element cannot be decoded as a UTF-8 string element.
-
readFrom
@Nullable public static ASN1Element readFrom(@NotNull InputStream inputStream) throws IOException, ASN1Exception Reads an ASN.1 element from the provided input stream.- Parameters:
inputStream- The input stream from which to read the element.- Returns:
- The element read from the input stream, or
nullif the end of the input stream is reached without reading any data. - Throws:
IOException- If a problem occurs while attempting to read from the input stream.ASN1Exception- If a problem occurs while attempting to decode the element.
-
readFrom
@Nullable public static ASN1Element readFrom(@NotNull InputStream inputStream, int maxSize) throws IOException, ASN1Exception Reads an ASN.1 element from the provided input stream.- Parameters:
inputStream- The input stream from which to read the element.maxSize- The maximum value size in bytes that will be allowed. A value less than or equal to zero indicates that no maximum size should be enforced. An attempt to read an element with a value larger than this will cause anASN1Exceptionto be thrown.- Returns:
- The element read from the input stream, or
nullif the end of the input stream is reached without reading any data. - Throws:
IOException- If a problem occurs while attempting to read from the input stream.ASN1Exception- If a problem occurs while attempting to decode the element.
-
writeTo
Writes an encoded representation of this ASN.1 element to the provided output stream.- Parameters:
outputStream- The output stream to which the element should be written.- Returns:
- The total number of bytes written to the output stream.
- Throws:
IOException- If a problem occurs while attempting to write to the provided output stream.- See Also:
-
hashCode
Retrieves a hash code for this ASN.1 BER element. -
equals
Indicates whether the provided object is equal to this ASN.1 BER element. The object will only be considered equal to this ASN.1 element if it is a non-null ASN.1 element with the same type and value as this element. -
equalsIgnoreType
Indicates whether the provided ASN.1 element is equal to this element, ignoring any potential difference in the BER type.- Parameters:
element- The ASN.1 BER element for which to make the determination.- Returns:
trueif the provided ASN.1 element is considered equal to this element (ignoring type differences), orfalseif not.
-
toString
Retrieves a string representation of the value for ASN.1 element. -
toString
Appends a string representation of the value for this ASN.1 element to the provided buffer.- Parameters:
buffer- The buffer to which to append the information.
-