Package com.unboundid.asn1
Class ASN1StreamReader
java.lang.Object
com.unboundid.asn1.ASN1StreamReader
- All Implemented Interfaces:
Closeable,AutoCloseable
@Mutable
@ThreadSafety(level=NOT_THREADSAFE)
public final class ASN1StreamReader
extends Object
implements Closeable
This class provides a mechanism for ASN.1 elements (including sequences and
sets) from an input stream in a manner that allows the data to be decoded on
the fly without constructing
ASN1Element objects if they are not
needed. If any method in this class throws an IOException, then the
caller must close this reader and must not attempt to use it any more.
ASN1StreamReader instances are not threadsafe and must not be
accessed concurrently by multiple threads.-
Constructor Summary
ConstructorsConstructorDescriptionASN1StreamReader(InputStream inputStream) Creates a new ASN.1 stream reader that will read data from the provided input stream.ASN1StreamReader(InputStream inputStream, int maxElementSize) Creates a new ASN.1 stream reader that will read data from the provided input stream. -
Method Summary
Modifier and TypeMethodDescriptionReads the beginning of an ASN.1 sequence from the input stream and returns a value that can be used to determine when the end of the sequence has been reached.beginSet()Reads the beginning of an ASN.1 set from the input stream and returns a value that can be used to determine when the end of the set has been reached.voidclose()Closes this ASN.1 stream reader and the underlying input stream.booleanIndicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught while trying to read the first byte of an element.booleanDeprecated.booleanIndicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught while trying to read subsequent bytes of an element (after one or more bytes have already been read for that element).intpeek()Peeks at the next byte to be read from the input stream without actually consuming it.Reads an ASN.1 integer element from the input stream and returns the value as aBigInteger.Reads an ASN.1 Boolean element from the input stream and returns the value as aBoolean.byte[]Reads an ASN.1 octet string element from the input stream and returns the value as a byte array.Reads a complete ASN.1 element from the input stream.Reads an ASN.1 enumerated element from the input stream and returns the value as anInteger.Reads an ASN.1 generalized time element from the input stream and returns the value as aDate.Reads an ASN.1 integer element from the input stream and returns the value as anInteger.readLong()Reads an ASN.1 integer element from the input stream and returns the value as aLong.voidreadNull()Reads an ASN.1 null element from the input stream.Reads an ASN.1 octet string element from the input stream and returns the value as aStringusing the UTF-8 encoding.Reads an ASN.1 UTC time element from the input stream and returns the value as aDate.voidsetIgnoreSocketTimeout(boolean ignoreSocketTimeout) Deprecated.Use thesetIgnoreSocketTimeout(boolean,boolean)method instead.voidsetIgnoreSocketTimeout(boolean ignoreInitialSocketTimeout, boolean ignoreSubsequentSocketTimeout) Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught during processing.
-
Constructor Details
-
ASN1StreamReader
Creates a new ASN.1 stream reader that will read data from the provided input stream. It will use a maximum element size equal to the default value returned by theLDAPConnectionOptions.getMaxMessageSize()method.- Parameters:
inputStream- The input stream from which data should be read. If the provided input stream does not support the use of themarkandresetmethods, then it will be wrapped with aBufferedInputStream.
-
ASN1StreamReader
Creates a new ASN.1 stream reader that will read data from the provided input stream. It will use a maximum element size ofInteger.MAX_VALUE.- Parameters:
inputStream- The input stream from which data should be read. If the provided input stream does not support the use of themarkandresetmethods, then it will be wrapped with aBufferedInputStream.maxElementSize- The maximum size in bytes of an ASN.1 element that may be read. A value less than or equal to zero will be interpreted asInteger.MAX_VALUE.
-
-
Method Details
-
close
Closes this ASN.1 stream reader and the underlying input stream. This reader must not be used after it has been closed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- If a problem occurs while closing the underlying input stream.
-
ignoreSocketTimeoutException
Deprecated.Use theignoreInitialSocketTimeoutException()andignoreSubsequentSocketTimeoutException()methods instead.Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught during processing.- Returns:
trueifSocketTimeoutExceptionexceptions should be ignored, orfalseif they should not be ignored and should be propagated to the caller.
-
ignoreInitialSocketTimeoutException
Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught while trying to read the first byte of an element.- Returns:
trueifSocketTimeoutExceptionexceptions should be ignored while trying to read the first byte of an element, orfalseif they should not be ignored and should be propagated to the caller.
-
ignoreSubsequentSocketTimeoutException
Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught while trying to read subsequent bytes of an element (after one or more bytes have already been read for that element).- Returns:
trueifSocketTimeoutExceptionexceptions should be ignored while trying to read subsequent bytes of an element, orfalseif they should not be ignored and should be propagated to the caller.
-
setIgnoreSocketTimeout
Deprecated.Use thesetIgnoreSocketTimeout(boolean,boolean)method instead.Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught during processing.- Parameters:
ignoreSocketTimeout- Indicates whether to ignoreSocketTimeoutExceptionexceptions that may be caught during processing.
-
setIgnoreSocketTimeout
public void setIgnoreSocketTimeout(boolean ignoreInitialSocketTimeout, boolean ignoreSubsequentSocketTimeout) Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught during processing.- Parameters:
ignoreInitialSocketTimeout- Indicates whether to ignoreSocketTimeoutExceptionexceptions that may be caught while trying to read the first byte of an element.ignoreSubsequentSocketTimeout- Indicates whether to ignoreSocketTimeoutExceptionexceptions that may be caught while reading beyond the first byte of an element.
-
peek
Peeks at the next byte to be read from the input stream without actually consuming it.- Returns:
- An integer value encapsulating the BER type of the next element in the input stream, or -1 if the end of the input stream has been reached and there is no data to be read. If a value of -1 is returned, then the input stream will not have been closed since this method is not intended to have any impact on the underlying input stream.
- Throws:
IOException- If a problem occurs while reading from the input stream.
-
readElement
Reads a complete ASN.1 element from the input stream.- Returns:
- The ASN.1 element read from the input stream, or
nullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.
-
readBoolean
Reads an ASN.1 Boolean element from the input stream and returns the value as aBoolean.- Returns:
- The
Booleanvalue of the ASN.1 Boolean element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 Boolean element.
-
readEnumerated
Reads an ASN.1 enumerated element from the input stream and returns the value as anInteger.- Returns:
- The
Integervalue of the ASN.1 enumerated element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 enumerated element.
-
readGeneralizedTime
Reads an ASN.1 generalized time element from the input stream and returns the value as aDate.- Returns:
- The
Datevalue of the ASN.1 generalized time element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 generalized time element.
-
readInteger
Reads an ASN.1 integer element from the input stream and returns the value as anInteger.- Returns:
- The
Integervalue of the ASN.1 integer element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 integer element.
-
readLong
Reads an ASN.1 integer element from the input stream and returns the value as aLong.- Returns:
- The
Longvalue of the ASN.1 integer element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 integer element.
-
readBigInteger
Reads an ASN.1 integer element from the input stream and returns the value as aBigInteger.- Returns:
- The
BigIntegervalue of the ASN.1 integer element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 integer element.
-
readNull
Reads an ASN.1 null element from the input stream. No value will be returned but the null element will be consumed.- Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 null element.
-
readBytes
Reads an ASN.1 octet string element from the input stream and returns the value as a byte array.- Returns:
- The byte array value of the ASN.1 octet string element read, or
nullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.
-
readString
Reads an ASN.1 octet string element from the input stream and returns the value as aStringusing the UTF-8 encoding.- Returns:
- The
Stringvalue of the ASN.1 octet string element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.
-
readUTCTime
Reads an ASN.1 UTC time element from the input stream and returns the value as aDate.- Returns:
- The
Datevalue of the ASN.1 UTC time element read, ornullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.ASN1Exception- If the data read cannot be parsed as an ASN.1 UTC time element.
-
beginSequence
Reads the beginning of an ASN.1 sequence from the input stream and returns a value that can be used to determine when the end of the sequence has been reached. Elements which are part of the sequence may be read from this ASN.1 stream reader until theASN1StreamReaderSequence.hasMoreElements()method returnsfalse.- Returns:
- An object which may be used to determine when the end of the
sequence has been reached, or
nullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.
-
beginSet
Reads the beginning of an ASN.1 set from the input stream and returns a value that can be used to determine when the end of the set has been reached. Elements which are part of the set may be read from this ASN.1 stream reader until theASN1StreamReaderSet.hasMoreElements()method returnsfalse.- Returns:
- An object which may be used to determine when the end of the set
has been reached, or
nullif the end of the input stream was reached before any data could be read. Ifnullis returned, then the input stream will have been closed. - Throws:
IOException- If a problem occurs while reading from the input stream, if the end of the input stream is reached in the middle of the element, or or if an attempt is made to read an element larger than the maximum allowed size.
-
ignoreInitialSocketTimeoutException()andignoreSubsequentSocketTimeoutException()methods instead.