/// <summary>
        ///     Decode an LBER encoded value into an Asn1Object from an InputStream.
        ///     This method also returns the total length of this encoded
        ///     Asn1Object (length of type + length of length + length of content)
        ///     in the parameter len. This information is helpful when decoding
        ///     structured types.
        /// </summary>
        public Asn1Object Decode(Stream inRenamed, int[] len)
        {
            _asn1Id.Reset(inRenamed);
            _asn1Len.Reset(inRenamed);

            var length = _asn1Len.Length;

            len[0] = _asn1Id.EncodedLength + _asn1Len.EncodedLength + length;

            if (_asn1Id.IsUniversal)
            {
                switch (_asn1Id.Tag)
                {
                case Asn1Sequence.Tag:
                    return(new Asn1Sequence(this, inRenamed, length));

                case Asn1Set.Tag:
                    return(new Asn1Set(this, inRenamed, length));

                case Asn1Boolean.Tag:
                    return(new Asn1Boolean(this, inRenamed, length));

                case Asn1Integer.Tag:
                    return(new Asn1Integer(this, inRenamed, length));

                case Asn1OctetString.Tag:
                    return(new Asn1OctetString(this, inRenamed, length));

                case Asn1Enumerated.Tag:
                    return(new Asn1Enumerated(this, inRenamed, length));

                case Asn1Null.Tag:
                    return(new Asn1Null());    // has no content to decode.

                /* Asn1 TYPE NOT YET SUPPORTED
                 * case Asn1BitString.TAG:
                 * return new Asn1BitString(this, in, length);
                 * case Asn1ObjectIdentifier.TAG:
                 * return new Asn1ObjectIdentifier(this, in, length);
                 * case Asn1Real.TAG:
                 * return new Asn1Real(this, in, length);
                 * case Asn1NumericString.TAG:
                 * return new Asn1NumericString(this, in, length);
                 * case Asn1PrintableString.TAG:
                 * return new Asn1PrintableString(this, in, length);
                 * case Asn1TeletexString.TAG:
                 * return new Asn1TeletexString(this, in, length);
                 * case Asn1VideotexString.TAG:
                 * return new Asn1VideotexString(this, in, length);
                 * case Asn1IA5String.TAG:
                 * return new Asn1IA5String(this, in, length);
                 * case Asn1GraphicString.TAG:
                 * return new Asn1GraphicString(this, in, length);
                 * case Asn1VisibleString.TAG:
                 * return new Asn1VisibleString(this, in, length);
                 * case Asn1GeneralString.TAG:
                 * return new Asn1GeneralString(this, in, length);
                 */

                default:
                    throw new EndOfStreamException("Unknown tag");     // !!! need a better exception
                }
            }

            // APPLICATION or CONTEXT-SPECIFIC tag
            return(new Asn1Tagged(this, inRenamed, length, (Asn1Identifier)_asn1Id.Clone()));
        }
        /// <summary>
        ///     Decode an LBER encoded value into an Asn1Object from an InputStream.
        ///     This method also returns the total length of this encoded
        ///     Asn1Object (length of type + length of length + length of content)
        ///     in the parameter len. This information is helpful when decoding
        ///     structured types.
        /// </summary>
        public async ValueTask <Asn1Object> Decode(Stream inRenamed, int[] len, CancellationToken cancellationToken)
        {
            _asn1Id.Reset(inRenamed);
            _asn1Len.Reset(inRenamed);

            var length = _asn1Len.Length;

            len[0] = _asn1Id.EncodedLength + _asn1Len.EncodedLength + length;

            if (_asn1Id.IsUniversal)
            {
                switch (_asn1Id.Tag)
                {
                case Asn1Sequence.Tag:
                    return(new Asn1Sequence(await Asn1Structured.DecodeStructured(this, inRenamed, length, cancellationToken).ConfigureAwait(false)));

                case Asn1Set.Tag:
                    return(new Asn1Set(await Asn1Structured.DecodeStructured(this, inRenamed, length, cancellationToken).ConfigureAwait(false)));

                case Asn1Boolean.Tag:
                    return(new Asn1Boolean(await DecodeBoolean(inRenamed, length, cancellationToken).ConfigureAwait(false)));

                case Asn1Integer.Tag:
                    return(new Asn1Integer(await DecodeNumeric(inRenamed, length, cancellationToken).ConfigureAwait(false)));

                case Asn1OctetString.Tag:
                    return(new Asn1OctetString(length > 0 ? await DecodeOctetString(inRenamed, length, cancellationToken).ConfigureAwait(false) : new byte[0]));

                case Asn1Enumerated.Tag:
                    return(new Asn1Enumerated(await DecodeNumeric(inRenamed, length, cancellationToken).ConfigureAwait(false)));

                case Asn1Null.Tag:
                    return(new Asn1Null());    // has no content to decode.

                /* Asn1 TYPE NOT YET SUPPORTED
                 * case Asn1BitString.TAG:
                 * return new Asn1BitString(this, in, length);
                 * case Asn1ObjectIdentifier.TAG:
                 * return new Asn1ObjectIdentifier(this, in, length);
                 * case Asn1Real.TAG:
                 * return new Asn1Real(this, in, length);
                 * case Asn1NumericString.TAG:
                 * return new Asn1NumericString(this, in, length);
                 * case Asn1PrintableString.TAG:
                 * return new Asn1PrintableString(this, in, length);
                 * case Asn1TeletexString.TAG:
                 * return new Asn1TeletexString(this, in, length);
                 * case Asn1VideotexString.TAG:
                 * return new Asn1VideotexString(this, in, length);
                 * case Asn1IA5String.TAG:
                 * return new Asn1IA5String(this, in, length);
                 * case Asn1GraphicString.TAG:
                 * return new Asn1GraphicString(this, in, length);
                 * case Asn1VisibleString.TAG:
                 * return new Asn1VisibleString(this, in, length);
                 * case Asn1GeneralString.TAG:
                 * return new Asn1GeneralString(this, in, length);
                 */

                default:
                    throw new EndOfStreamException("Unknown tag");     // !!! need a better exception
                }
            }

            // APPLICATION or CONTEXT-SPECIFIC tag
            return(new Asn1Tagged((Asn1Identifier)_asn1Id.Clone(), new Asn1OctetString(length > 0 ? await DecodeOctetString(inRenamed, length, cancellationToken).ConfigureAwait(false) : new byte[0])));
        }