package org.bouncycastle.asn1;

import java.io.EOFException;
import java.io.InputStream;
import java.io.IOException;

class DefiniteLengthInputStream
        extends LimitedInputStream
{
    private int               _length;

    DefiniteLengthInputStream(
        InputStream in,
        int         length)
    {
        super(in);

        if (length < 0)
        {
            throw new IllegalArgumentException("negative lengths not allowed");
        }

        this._length = length;
    }

    public int read()
        throws IOException
    {
        if (_length > 0)
        {
            int b = _in.read();

            if (b < 0)
            {
                throw new EOFException();
            }

            --_length;
            return b;
        }

        setParentEofDetect(true);

        return -1;
    }

    byte[] toByteArray()
        throws IOException
    {
        byte[] bytes = new byte[_length];

        if (_length > 0)
        {
            int pos = 0;
            do
            {
                int read = _in.read(bytes, pos, _length - pos);

                if (read < 0)
                {
                    throw new EOFException();
                }

                pos += read;
            }
            while (pos < _length);

            _length = 0;
        }

        setParentEofDetect(true);

        return bytes;
    }
}

@@ -822,24 +822,450 @@
  -1;%0A    %7D%0A%0A
+    public int read(byte%5B%5D buf, int off, int len)%0A        throws IOException%0A    %7B%0A        if (_length %3E 0)%0A        %7B%0A            int toRead = Math.min(len, _length);%0A            int numRead = _in.read(buf, off, toRead);%0A%0A            if (numRead %3C 0)%0A                throw new EOFException();%0A%0A            _length -= numRead;%0A            return numRead;%0A        %7D%0A%0A        setParentEofDetect(true);%0A%0A        return -1;%0A    %7D%0A%0A
     byte%5B%5D t

Add extra read(...) method for performance