Package com.unboundid.asn1
Class ASN1StreamReader
- java.lang.Object
-
- com.unboundid.asn1.ASN1StreamReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
@Mutable @ThreadSafety(level=NOT_THREADSAFE) public final class ASN1StreamReader extends java.lang.Object implements java.io.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 constructingASN1Elementobjects if they are not needed. If any method in this class throws anIOException, then the caller must close this reader and must not attempt to use it any more.ASN1StreamReaderinstances are not threadsafe and must not be accessed concurrently by multiple threads.
-
-
Constructor Summary
Constructors Constructor Description ASN1StreamReader(java.io.InputStream inputStream)Creates a new ASN.1 stream reader that will read data from the provided input stream.ASN1StreamReader(java.io.InputStream inputStream, int maxElementSize)Creates a new ASN.1 stream reader that will read data from the provided input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ASN1StreamReaderSequencebeginSequence()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.ASN1StreamReaderSetbeginSet()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.booleanignoreInitialSocketTimeoutException()Indicates whether to ignorejava.net.SocketTimeoutExceptionexceptions that may be caught while trying to read the first byte of an element.booleanignoreSocketTimeoutException()Deprecated.Use theignoreInitialSocketTimeoutException()andignoreSubsequentSocketTimeoutException()methods instead.booleanignoreSubsequentSocketTimeoutException()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).intpeek()Peeks at the next byte to be read from the input stream without actually consuming it.java.math.BigIntegerreadBigInteger()Reads an ASN.1 integer element from the input stream and returns the value as aBigInteger.java.lang.BooleanreadBoolean()Reads an ASN.1 Boolean element from the input stream and returns the value as aBoolean.byte[]readBytes()Reads an ASN.1 octet string element from the input stream and returns the value as a byte array.ASN1ElementreadElement()Reads a complete ASN.1 element from the input stream.java.lang.IntegerreadEnumerated()Reads an ASN.1 enumerated element from the input stream and returns the value as anInteger.java.util.DatereadGeneralizedTime()Reads an ASN.1 generalized time element from the input stream and returns the value as aDate.java.lang.IntegerreadInteger()Reads an ASN.1 integer element from the input stream and returns the value as anInteger.java.lang.LongreadLong()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.java.lang.StringreadString()Reads an ASN.1 octet string element from the input stream and returns the value as aStringusing the UTF-8 encoding.java.util.DatereadUTCTime()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 Detail
-
ASN1StreamReader
public ASN1StreamReader(java.io.InputStream inputStream)
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.
-
ASN1StreamReader
public ASN1StreamReader(java.io.InputStream inputStream, int maxElementSize)
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 Detail
-
close
public void close() throws java.io.IOException
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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- If a problem occurs while closing the underlying input stream.
-
ignoreSocketTimeoutException
@Deprecated public boolean 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
public boolean 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
public boolean 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 public void setIgnoreSocketTimeout(boolean ignoreSocketTimeout)
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
public int peek() throws java.io.IOException
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:
java.io.IOException- If a problem occurs while reading from the input stream.
-
readElement
public ASN1Element readElement() throws java.io.IOException
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:
java.io.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
public java.lang.Boolean readBoolean() throws java.io.IOException, ASN1Exception
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:
java.io.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
public java.lang.Integer readEnumerated() throws java.io.IOException, ASN1Exception
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:
java.io.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
public java.util.Date readGeneralizedTime() throws java.io.IOException, ASN1Exception
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:
java.io.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
public java.lang.Integer readInteger() throws java.io.IOException, ASN1Exception
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:
java.io.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
public java.lang.Long readLong() throws java.io.IOException, ASN1Exception
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:
java.io.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
public java.math.BigInteger readBigInteger() throws java.io.IOException, ASN1Exception
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:
java.io.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
public void readNull() throws java.io.IOException, ASN1Exception
Reads an ASN.1 null element from the input stream. No value will be returned but the null element will be consumed.- Throws:
java.io.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
public byte[] readBytes() throws java.io.IOException
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:
java.io.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
public java.lang.String readString() throws java.io.IOException
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:
java.io.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
public java.util.Date readUTCTime() throws java.io.IOException, ASN1Exception
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:
java.io.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
public ASN1StreamReaderSequence beginSequence() throws java.io.IOException
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:
java.io.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
public ASN1StreamReaderSet beginSet() throws java.io.IOException
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:
java.io.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.
-
-