Class BerInputStream

  • Direct Known Subclasses:
    DerInputStream

    public class BerInputStream
    extends Object
    Decodes ASN.1 types encoded with BER (X.690)
    See Also:
    ASN.1
    • Field Detail

      • buffer

        protected byte[] buffer
      • offset

        protected int offset
        The position in the buffer. Next read must place data into the buffer from this offset
      • INDEFINIT_LENGTH

        protected static final int INDEFINIT_LENGTH
        Indicates indefinite length of the current type
        See Also:
        Constant Field Values
      • tag

        public int tag
        Current decoded tag
      • length

        protected int length
        Current decoded length
      • content

        public Object content
        Current decoded content
      • tagOffset

        protected int tagOffset
        Current decoded tag offset
      • contentOffset

        protected int contentOffset
        Current decoded content offset
      • choiceIndex

        public int choiceIndex
        The last choice index
      • times

        public int[] times
        Keeps last decoded: year, month, day, hour, minute, second, millisecond
      • oidElement

        public int oidElement
      • isVerify

        protected boolean isVerify
        Indicates verify or store mode. In store mode a decoded content is stored in a newly allocated appropriate object. The content variable holds a reference to the last created object. In verify mode a decoded content is not stored.
      • isIndefinedLength

        protected boolean isIndefinedLength
        Indicates defined or indefined reading mode for associated InputStream. This mode is defined by reading a length for a first ASN.1 type from InputStream.
    • Constructor Detail

      • BerInputStream

        public BerInputStream​(byte[] encoded)
                       throws IOException
        Creates stream for decoding.
        Throws:
        IOException
      • BerInputStream

        public BerInputStream​(byte[] encoded,
                              int offset,
                              int expectedLength)
                       throws IOException
        Creates stream for decoding.
        Parameters:
        encoded - bytes array to be decoded
        offset - the encoding offset
        expectedLength - expected length of full encoding, this includes identifier, length an content octets
        Throws:
        IOException
      • BerInputStream

        public BerInputStream​(InputStream in)
                       throws IOException
        Creates stream for decoding. Allocates initial buffer of default size
        Throws:
        IOException
      • BerInputStream

        public BerInputStream​(InputStream in,
                              int initialSize)
                       throws IOException
        Creates stream for decoding.
        Parameters:
        initialSize - the internal buffer initial size
        Throws:
        IOException
    • Method Detail

      • reset

        public final void reset​(byte[] encoded)
                         throws IOException
        Resets this stream to initial state.
        Parameters:
        encoded - a new bytes array to be decoded
        Throws:
        IOException - if an error occurs
      • next

        public int next()
                 throws IOException
        Decodes next encoded type. Initializes tag, length, tagOffset and contentOffset variables
        Returns:
        next decoded tag
        Throws:
        IOException - if error occured
      • getLength

        public static int getLength​(byte[] encoding)
        Returns the length of the encoding
      • readBitString

        public void readBitString()
                           throws IOException
        Decodes ASN.1 bitstring type
        Throws:
        IOException
      • readEnumerated

        public void readEnumerated()
                            throws IOException
        Decodes ASN.1 Enumerated type
        Throws:
        IOException
      • readGeneralizedTime

        public void readGeneralizedTime()
                                 throws IOException
        Decodes ASN.1 GeneralizedTime type
        Throws:
        IOException - if error occured
      • readUTCTime

        public void readUTCTime()
                         throws IOException
        Decodes ASN.1 UTCTime type
        Throws:
        IOException - if an I/O error occurs or the end of the stream is reached
      • readOctetString

        public void readOctetString()
                             throws IOException
        Decodes ASN.1 Octetstring type
        Throws:
        IOException
      • readString

        public void readString​(ASN1StringType type)
                        throws IOException
        Decodes ASN.1 String type
        Throws:
        IOException - if an I/O error occurs or the end of the stream is reached
      • getEncoded

        public byte[] getEncoded()
        Returns encoded array. MUST be invoked after decoding corresponding ASN.1 notation
      • getBuffer

        public final byte[] getBuffer()
        Returns internal buffer used for decoding
      • getLength

        public final int getLength()
        Returns length of the current content for decoding
      • getOffset

        public final int getOffset()
        Returns the current offset
      • getEndOffset

        public final int getEndOffset()
        Returns end offset for the current encoded type
      • getTagOffset

        public final int getTagOffset()
        Returns start offset for the current encoded type
      • setVerify

        public final void setVerify()
        Sets verify mode.
      • read

        protected int read()
                    throws IOException
        Reads the next encoded byte from the encoded input stream.
        Throws:
        IOException
      • readContent

        public void readContent()
                         throws IOException
        Reads the next encoded content from the encoded input stream. The method MUST be used for reading a primitive encoded content.
        Throws:
        IOException
      • compactBuffer

        public void compactBuffer()
        Reallocates the buffer in order to make it exactly the size of data it contains