Package org.forgerock.opendj.io
Class Asn1Reader
- java.lang.Object
-
- org.forgerock.opendj.io.Asn1Reader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public final class Asn1Reader extends Object implements Closeable
An interface for decoding ASN.1 elements from a data source.Methods for creating
Asn1Reader
s are provided in theAsn1
class.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this ASN.1 reader.boolean
elementAvailable()
Indicates whether the next element can be read without blocking.boolean
hasNextElement()
Indicates whether the current stream, sequence, or set contains another element.int
peekLength()
Returns the data length of the next element without actually reading it.byte
peekType()
Returns the type of the next element without actually reading it.ByteString
readAsn1Element()
Reads the next element and returns its full encoding, which includes tag and length bytes.ByteStringBuilder
readAsn1Element(ByteStringBuilder builder)
Reads the next element and appends its full encoding, which includes tag and length bytes, to the providedByteStringBuilder
.BigInteger
readBigInteger()
Reads the next element as aBigInteger
having the Universal Integer ASN.1 type tag.BigInteger
readBigInteger(byte type)
Reads the next element as aBigInteger
having the provided type tag.boolean
readBoolean()
Reads the next element as a boolean having the Universal Boolean ASN.1 type tag.boolean
readBoolean(byte type)
Reads the next element as a boolean having the provided type tag.void
readEndExplicitTag()
Finishes reading an explicit tag and discards any unread elements.void
readEndSequence()
Finishes reading a sequence and discards any unread elements.void
readEndSet()
Finishes reading a set and discards any unread elements.int
readEnumerated()
Reads the next element as an enumerated having the Universal Enumerated ASN.1 type tag.int
readEnumerated(byte type)
Reads the next element as an enumerated having the provided type tag.long
readInteger()
Reads the next element as an integer having the Universal Integer ASN.1 type tag.long
readInteger(byte type)
Reads the next element as an integer having the provided type tag.void
readNull()
Reads the next element as a null element having the Universal Null ASN.1 type tag.void
readNull(byte type)
Reads the next element as a null element having the provided type tag.ByteString
readOctetString()
Reads the next element as an octet string having any tag.ByteString
readOctetString(byte type)
Reads the next element as an octet string having the provided type tag.ByteStringBuilder
readOctetString(byte type, ByteStringBuilder builder)
Reads the next element as an octet string having the provided type tag and appends it to the providedByteStringBuilder
.ByteStringBuilder
readOctetString(ByteStringBuilder builder)
Reads the next element as an octet string having the Universal Octet String ASN.1 type tag and appends it to the providedByteStringBuilder
.String
readOctetStringAsString()
Reads the next element as an octet string having the Universal Octet String ASN.1 type tag and decodes the value as a UTF-8 encoded string.String
readOctetStringAsString(byte type)
Reads the next element as an octet string having the provided type tag and decodes the value as a UTF-8 encoded string.void
readStartExplicitTag()
Reads the next element as an explicit ignoring the ASN.1 type tag.void
readStartExplicitTag(byte type)
Reads the next element as an explicit tag having the provided tag type.void
readStartSequence()
Reads the next element as a sequence having the Universal Sequence ASN.1 type tag.void
readStartSequence(byte type)
Reads the next element as a sequence having the provided type tag.void
readStartSet()
Reads the next element as a set having the Universal Set ASN.1 type tag.void
readStartSet(byte type)
Reads the next element as a set having the provided type tag.Asn1Reader
skipElement()
Skips the next element without decoding it.Asn1Reader
skipElement(byte type)
Skips the next element having the provided type tag without decoding it.
-
-
-
Method Detail
-
close
public void close() throws IOException
Closes this ASN.1 reader.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- If an error occurs while closing.
-
elementAvailable
public boolean elementAvailable() throws IOException
Indicates whether the next element can be read without blocking.- Returns:
true
if a complete element is available orfalse
otherwise.- Throws:
DecodeException
- If the available data was not valid ASN.1.IOException
- If an unexpected IO error occurred.
-
hasNextElement
public boolean hasNextElement() throws IOException
Indicates whether the current stream, sequence, or set contains another element. Note that this method may returntrue
even if a previous call toelementAvailable()
returnedfalse
, indicating that the current set or sequence contains another element but an attempt to read that element may block. This method will block if there is not enough data available to make the determination (typically only the next element's type is required).- Returns:
true
if the current stream, sequence, or set contains another element, orfalse
if the end of the stream, sequence, or set has been reached.- Throws:
DecodeException
- If the available data was not valid ASN.1.IOException
- If an unexpected IO error occurred.
-
peekLength
public int peekLength() throws IOException
Returns the data length of the next element without actually reading it.- Returns:
- The data length of the next element, zero or positive.
- Throws:
DecodeException
- When EOF is reached when trying to read the length bytes or when the length bytes do not form a valid ASN.1 encoding.IOException
- If an unexpected IO error occurred.
-
peekType
public byte peekType() throws IOException
Returns the type of the next element without actually reading it.- Returns:
- The type of the next element, or
-1
if the end of the stream, sequence, or set has been reached. - Throws:
DecodeException
- If the available data was not valid ASN.1.IOException
- If an unexpected IO error occurred.
-
readAsn1Element
public ByteString readAsn1Element() throws IOException
Reads the next element and returns its full encoding, which includes tag and length bytes.- Returns:
- The full encoding of the next element.
- Throws:
IOException
- If an unexpected IO error occurred.
-
readAsn1Element
public ByteStringBuilder readAsn1Element(ByteStringBuilder builder) throws IOException
Reads the next element and appends its full encoding, which includes tag and length bytes, to the providedByteStringBuilder
.- Parameters:
builder
- TheByteStringBuilder
to append the ASN.1 element to.- Returns:
- A reference to
builder
. - Throws:
IOException
- If an unexpected IO error occurred.
-
readBigInteger
public BigInteger readBigInteger(byte type) throws IOException
Reads the next element as aBigInteger
having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded
BigInteger
value. - Throws:
DecodeException
- If the element cannot be decoded as aBigInteger
.IOException
- If an unexpected IO error occurred.
-
readBigInteger
public BigInteger readBigInteger() throws IOException
Reads the next element as aBigInteger
having the Universal Integer ASN.1 type tag.- Returns:
- The decoded
BigInteger
value. - Throws:
DecodeException
- If the element cannot be decoded as aBigInteger
.IOException
- If an unexpected IO error occurred.
-
readBoolean
public boolean readBoolean() throws IOException
Reads the next element as a boolean having the Universal Boolean ASN.1 type tag.- Returns:
- The decoded boolean value.
- Throws:
DecodeException
- If the element cannot be decoded as a boolean.IOException
- If an unexpected IO error occurred.
-
readBoolean
public boolean readBoolean(byte type) throws IOException
Reads the next element as a boolean having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded boolean value.
- Throws:
DecodeException
- If the element cannot be decoded as a boolean.IOException
- If an unexpected IO error occurred.
-
readEndExplicitTag
public void readEndExplicitTag() throws IOException
Finishes reading an explicit tag and discards any unread elements.- Throws:
DecodeException
- If an error occurs while advancing to the end of the explicit tag.IOException
- If an unexpected IO error occurred.IllegalStateException
- If there is no explicit tag being read.
-
readEndSequence
public void readEndSequence() throws IOException
Finishes reading a sequence and discards any unread elements.- Throws:
DecodeException
- If an error occurs while advancing to the end of the sequence.IOException
- If an unexpected IO error occurred.IllegalStateException
- If there is no sequence being read.
-
readEndSet
public void readEndSet() throws IOException
Finishes reading a set and discards any unread elements.- Throws:
DecodeException
- If an error occurs while advancing to the end of the set.IOException
- If an unexpected IO error occurred.IllegalStateException
- If there is no set being read.
-
readEnumerated
public int readEnumerated() throws IOException
Reads the next element as an enumerated having the Universal Enumerated ASN.1 type tag.- Returns:
- The decoded enumerated value.
- Throws:
DecodeException
- If the element cannot be decoded as an enumerated value.IOException
- If an unexpected IO error occurred.
-
readEnumerated
public int readEnumerated(byte type) throws IOException
Reads the next element as an enumerated having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded enumerated value.
- Throws:
DecodeException
- If the element cannot be decoded as an enumerated value.IOException
- If an unexpected IO error occurred.
-
readInteger
public long readInteger() throws IOException
Reads the next element as an integer having the Universal Integer ASN.1 type tag.- Returns:
- The decoded integer value.
- Throws:
DecodeException
- If the element cannot be decoded as an integer.IOException
- If an unexpected IO error occurred.
-
readInteger
public long readInteger(byte type) throws IOException
Reads the next element as an integer having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded integer value.
- Throws:
DecodeException
- If the element cannot be decoded as an integer.IOException
- If an unexpected IO error occurred.
-
readNull
public void readNull() throws IOException
Reads the next element as a null element having the Universal Null ASN.1 type tag.- Throws:
DecodeException
- If the element cannot be decoded as a null element.IOException
- If an unexpected IO error occurred.
-
readNull
public void readNull(byte type) throws IOException
Reads the next element as a null element having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Throws:
DecodeException
- If the element cannot be decoded as a null element.IOException
- If an unexpected IO error occurred.
-
readOctetString
public ByteString readOctetString() throws IOException
Reads the next element as an octet string having any tag.- Returns:
- The decoded octet string represented using a
ByteString
. - Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readOctetString
public ByteStringBuilder readOctetString(ByteStringBuilder builder) throws IOException
Reads the next element as an octet string having the Universal Octet String ASN.1 type tag and appends it to the providedByteStringBuilder
.- Parameters:
builder
- TheByteStringBuilder
to append the octet string to.- Returns:
- A reference to
builder
. - Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readOctetString
public ByteString readOctetString(byte type) throws IOException
Reads the next element as an octet string having the provided type tag.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded octet string represented using a
ByteString
. - Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readOctetString
public ByteStringBuilder readOctetString(byte type, ByteStringBuilder builder) throws IOException
Reads the next element as an octet string having the provided type tag and appends it to the providedByteStringBuilder
.- Parameters:
type
- The expected type tag of the element.builder
- TheByteStringBuilder
to append the octet string to.- Returns:
- A reference to
builder
. - Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readOctetStringAsString
public String readOctetStringAsString() throws IOException
Reads the next element as an octet string having the Universal Octet String ASN.1 type tag and decodes the value as a UTF-8 encoded string.- Returns:
- The decoded octet string as a UTF-8 encoded string.
- Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readOctetStringAsString
public String readOctetStringAsString(byte type) throws IOException
Reads the next element as an octet string having the provided type tag and decodes the value as a UTF-8 encoded string.- Parameters:
type
- The expected type tag of the element.- Returns:
- The decoded octet string as a UTF-8 encoded string.
- Throws:
DecodeException
- If the element cannot be decoded as an octet string.IOException
- If an unexpected IO error occurred.
-
readStartExplicitTag
public void readStartExplicitTag() throws IOException
Reads the next element as an explicit ignoring the ASN.1 type tag. All further reads will read the elements in the explicit tag untilreadEndExplicitTag()
is called.- Throws:
DecodeException
- If the element cannot be decoded as an explicit tag.IOException
- If an unexpected IO error occurred.
-
readStartExplicitTag
public void readStartExplicitTag(byte type) throws IOException
Reads the next element as an explicit tag having the provided tag type. All further reads will read the elements in the explicit tag untilreadEndExplicitTag()
is called.- Parameters:
type
- The expected type tag of the element.- Throws:
DecodeException
- If the element cannot be decoded as an explicit tag.IOException
- If an unexpected IO error occurred.
-
readStartSequence
public void readStartSequence() throws IOException
Reads the next element as a sequence having the Universal Sequence ASN.1 type tag. All further reads will read the elements in the sequence untilreadEndSequence()
is called.- Throws:
DecodeException
- If the element cannot be decoded as a sequence.IOException
- If an unexpected IO error occurred.
-
readStartSequence
public void readStartSequence(byte type) throws IOException
Reads the next element as a sequence having the provided type tag. All further reads will read the elements in the sequence untilreadEndSequence()
is called.- Parameters:
type
- The expected type tag of the element.- Throws:
DecodeException
- If the element cannot be decoded as a sequence.IOException
- If an unexpected IO error occurred.
-
readStartSet
public void readStartSet() throws IOException
Reads the next element as a set having the Universal Set ASN.1 type tag. All further reads will read the elements in the set untilreadEndSet()
is called.- Throws:
DecodeException
- If the element cannot be decoded as a set.IOException
- If an unexpected IO error occurred.
-
readStartSet
public void readStartSet(byte type) throws IOException
Reads the next element as a set having the provided type tag. All further reads will read the elements in the set untilreadEndSet()
is called.- Parameters:
type
- The expected type tag of the element.- Throws:
DecodeException
- If the element cannot be decoded as a set.IOException
- If an unexpected IO error occurred.
-
skipElement
public Asn1Reader skipElement() throws IOException
Skips the next element without decoding it.- Returns:
- A reference to this ASN.1 reader.
- Throws:
DecodeException
- If the next element could not be skipped.IOException
- If an unexpected IO error occurred.
-
skipElement
public Asn1Reader skipElement(byte type) throws IOException
Skips the next element having the provided type tag without decoding it.- Parameters:
type
- The expected type tag of the element.- Returns:
- A reference to this ASN.1 reader.
- Throws:
DecodeException
- If the next element does not have the provided type tag.IOException
- If an unexpected IO error occurred.
-
-