APIdia Launch Partner
Top Description Fields Constructors Methods
sun.security.util

public Class DerInputStream

extends Object
Class Inheritance
Imports
java.io.InputStream, .IOException, java.math.BigInteger, java.util.Arrays, .Date, .Optional, java.util.function.Predicate

A DER input stream, used for parsing ASN.1 DER-encoded data such as that found in X.509 certificates. DER is a subset of BER/1, which has the advantage that it allows only a single encoding of primitive data. (High level data such as dates still support many encodings.) That is, it uses the "Definite" Encoding Rules (DER) not the "Basic" ones (BER).

Note that, like BER/1, DER streams are streams of explicitly tagged data values. Accordingly, this programming interface does not expose any variant of the java.io.InputStream interface, since that kind of input stream holds untagged data values and using that I/O model could prevent correct parsing of the DER data.

At this time, this class supports only a subset of the types of DER data encodings which are defined. That subset is sufficient for parsing most X.509 certificates.

Authors
David Brownell, Amit Kapoor, Hemma Prafullchandra

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final byte[]
pack-priv final int
pack-priv int
pack-priv int
pack-priv final int

Constructor Summary

AccessConstructor and Description
public
DerInputStream(byte[] data, int start, int length, boolean allowBER)

Constructs a DerInputStream by assigning all its fields.

public
DerInputStream(byte[] data)

public
DerInputStream(byte[] data, int offset, int len)

Method Summary

Modifier and TypeMethod and Description
public void
atEnd()

Ensures there is no more data.

public int
available()

Returns the number of bytes available for reading.

private boolean

Returns:

true if matches, false if not or stream is at end.
checkNextTag
(Predicate<Byte>
the rule to check for the tag.
rule
)

Checks if the tag of the next DerValue matches the rule.

private boolean

Returns:

true if matches, false if not or stream is at end.
checkNextTag
(byte
the expected tag
tag
)

Detect if the tag of the next DerValue is the specified one.

public BigInteger
public byte[]
public String
pack-priv static int
public DerValue

Returns:

the read DerValue.
getDerValue
()

Reads a DerValue from this stream.

public int
public Date
public String
public String
public int
pack-priv static int

Returns:

the length or -1 if indefinite length found.
getLength
(InputStream in)

Get a length from the input stream, allowing for at most 32 bits of encoding to be used.

public void
public byte[]
public ObjectIdentifier
public Optional<DerValue>

Returns:

the next DerValue, or empty if not found or stream at end
getOptional
(byte
the expected tag
tag
)

Returns the next DerValue if its tag is the given one.

public Optional<DerValue>

Returns:

the inner DerValue, or empty if not found or stream at end
getOptionalExplicitContextSpecific
(int
the expected tag
n
)

Returns the inner DerValue if the next DerValue is an EXPLICIT context-specific value tagged by n.

public Optional<DerValue>

Returns:

the restored DerValue, or empty if not found or stream at end
getOptionalImplicitContextSpecific
(int
the expected tag
n
,
byte
the real tag for the IMPLICIT type
tag
)

Returns the restored DerValue if the next DerValue is an IMPLICIT context-specific value tagged by n.

public BigInteger
public String
public DerValue[]
getSequence(int startLen)

public DerValue[]
getSet(int startLen)

public DerValue[]
getSet(int startLen, boolean implicit)

public String
public Date
public BitArray
public Date
public String
public void
mark(int readAheadLimit)

Mark the current position in the buffer, so that a later call to reset will return here.

public int
public void
reset()

Return to the position of the last mark call.

public boolean

Returns:

true if matches, false if not or stream is at end.
seeOptionalContextSpecific
(int
the expected tag
n
)

Detect if the next DerValue is a context-specific value tagged by n.

public byte[]
toByteArray()

Returns the remaining unread bytes, or, all bytes if none read yet.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowBERback to summary
pack-priv final boolean allowBER
databack to summary
pack-priv final byte[] data
endback to summary
pack-priv final int end
markback to summary
pack-priv int mark
posback to summary
pack-priv int pos
startback to summary
pack-priv final int start

Constructor Detail

DerInputStreamback to summary
public DerInputStream(byte[] data, int start, int length, boolean allowBER)

Constructs a DerInputStream by assigning all its fields. No checking on arguments since all callers are internal. data should never be null even if length is 0.

DerInputStreamback to summary
public DerInputStream(byte[] data) throws IOException
DerInputStreamback to summary
public DerInputStream(byte[] data, int offset, int len) throws IOException

Method Detail

atEndback to summary
public void atEnd() throws IOException

Ensures there is no more data. This can be called when the last expected field is parsed, and we need to make sure no unread is left.

Exceptions
IOException:
if the end is NOT reached yet
availableback to summary
public int available()

Returns the number of bytes available for reading. This is most useful for testing whether the stream is empty.

checkNextTagback to summary
private boolean checkNextTag(Predicate<Byte> rule)

Checks if the tag of the next DerValue matches the rule.

Parameters
rule:Predicate<Byte>

the rule to check for the tag.

Returns:boolean

true if matches, false if not or stream is at end.

checkNextTagback to summary
private boolean checkNextTag(byte tag)

Detect if the tag of the next DerValue is the specified one.

Parameters
tag:byte

the expected tag

Returns:boolean

true if matches, false if not or stream is at end.

getBigIntegerback to summary
public BigInteger getBigInteger() throws IOException
getBitStringback to summary
public byte[] getBitString() throws IOException
getBMPStringback to summary
public String getBMPString() throws IOException
getDefiniteLengthback to summary
pack-priv static int getDefiniteLength(InputStream in) throws IOException
getDerValueback to summary
public DerValue getDerValue() throws IOException

Reads a DerValue from this stream. After the call, the data pointer is right after this DerValue so that the next call will read the next DerValue.

Returns:DerValue

the read DerValue.

Exceptions
IOException:
if a DerValue cannot be constructed starting from this position because of byte shortage or encoding error.
getEnumeratedback to summary
public int getEnumerated() throws IOException
getGeneralizedTimeback to summary
public Date getGeneralizedTime() throws IOException
getGeneralStringback to summary
public String getGeneralString() throws IOException
getIA5Stringback to summary
public String getIA5String() throws IOException
getIntegerback to summary
public int getInteger() throws IOException
getLengthback to summary
pack-priv static int getLength(InputStream in) throws IOException

Get a length from the input stream, allowing for at most 32 bits of encoding to be used. (Not the same as getting a tagged integer!)

Returns:int

the length or -1 if indefinite length found.

Exceptions
IOException:
on parsing error or unsupported lengths.
getNullback to summary
public void getNull() throws IOException
getOctetStringback to summary
public byte[] getOctetString() throws IOException
getOIDback to summary
public ObjectIdentifier getOID() throws IOException
getOptionalback to summary
public Optional<DerValue> getOptional(byte tag) throws IOException

Returns the next DerValue if its tag is the given one.

Parameters
tag:byte

the expected tag

Returns:Optional<DerValue>

the next DerValue, or empty if not found or stream at end

Exceptions
IOException:
if an I/O error happens
getOptionalExplicitContextSpecificback to summary
public Optional<DerValue> getOptionalExplicitContextSpecific(int n) throws IOException

Returns the inner DerValue if the next DerValue is an EXPLICIT context-specific value tagged by n.

Parameters
n:int

the expected tag

Returns:Optional<DerValue>

the inner DerValue, or empty if not found or stream at end

Exceptions
IOException:
if an I/O error happens
getOptionalImplicitContextSpecificback to summary
public Optional<DerValue> getOptionalImplicitContextSpecific(int n, byte tag) throws IOException

Returns the restored DerValue if the next DerValue is an IMPLICIT context-specific value tagged by n.

Parameters
n:int

the expected tag

tag:byte

the real tag for the IMPLICIT type

Returns:Optional<DerValue>

the restored DerValue, or empty if not found or stream at end

Exceptions
IOException:
if an I/O error happens
getPositiveBigIntegerback to summary
public BigInteger getPositiveBigInteger() throws IOException
getPrintableStringback to summary
public String getPrintableString() throws IOException
getSequenceback to summary
public DerValue[] getSequence(int startLen) throws IOException
getSetback to summary
public DerValue[] getSet(int startLen) throws IOException
getSetback to summary
public DerValue[] getSet(int startLen, boolean implicit) throws IOException
getT61Stringback to summary
public String getT61String() throws IOException
getTimeback to summary
public Date getTime() throws IOException
getUnalignedBitStringback to summary
public BitArray getUnalignedBitString() throws IOException
getUTCTimeback to summary
public Date getUTCTime() throws IOException
getUTF8Stringback to summary
public String getUTF8String() throws IOException
markback to summary
public void mark(int readAheadLimit)

Mark the current position in the buffer, so that a later call to reset will return here. The readAheadLimit is useless here, because all data is available, and we can go to anywhere at will.

peekByteback to summary
public int peekByte() throws IOException
resetback to summary
public void reset()

Return to the position of the last mark call. A mark is implicitly set at the beginning of the stream when it is created.

seeOptionalContextSpecificback to summary
public boolean seeOptionalContextSpecific(int n) throws IOException

Detect if the next DerValue is a context-specific value tagged by n.

Parameters
n:int

the expected tag

Returns:boolean

true if matches, false if not or stream is at end.

Exceptions
IOException:
if an I/O error happens while peeking the byte
toByteArrayback to summary
public byte[] toByteArray()

Returns the remaining unread bytes, or, all bytes if none read yet.