Class Asn1Writer

    • Method Detail

      • toByteString

        public ByteString toByteString()
        Returns a ByteString containing the ASN.1 encoded data and clears the contents of this writer.
        Returns:
        A ByteString containing the ASN.1 encoded data.
        Throws:
        IllegalStateException - If there are any unclosed ASN.1 sequences.
      • toByteArray

        public byte[] toByteArray()
        Returns a byte array containing the ASN.1 encoded data and clears the contents of this writer.
        Returns:
        A byte array containing the ASN.1 encoded data.
        Throws:
        IllegalStateException - If there are any unclosed ASN.1 sequences.
      • asByteBuffer

        public ByteBuffer asByteBuffer()
        Returns a mutable ByteBuffer view of this ASN.1 encoded data. Changes to this writer will interfere with the returned byte buffer and vice versa. Call clear() before encoding any new elements.
        Returns:
        A ByteBuffer containing the ASN.1 encoded data.
        Throws:
        IllegalStateException - If there are any unclosed ASN.1 sequences.
      • clear

        public void clear()
        Clears the content of this ASN.1 writer.
      • clearAndTruncate

        public void clearAndTruncate​(int thresholdCapacity,
                                     int newCapacity)
        Sets the length of this ASN.1 writer to zero, and resets the capacity to the specified size if above provided threshold.
        Parameters:
        thresholdCapacity - The threshold capacity triggering a truncate
        newCapacity - The new capacity.
        Throws:
        IllegalArgumentException - If the newCapacity is negative or the newCapacity is bigger than the thresholdCapacity.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(byte[] asn1Element)
        Writes exactly the provided bytes without encoding the data.

        It is the caller responsibility to ensure the provided bytes form a valid ASN.1 element encoding.

        Parameters:
        asn1Element - The ASN.1-encoded element to be written as is.
        Returns:
        A reference to this ASN.1 writer.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(byte[] asn1Element,
                                           int offset,
                                           int length)
        Writes exactly the provided bytes without encoding the data.

        It is the caller responsibility to ensure the provided bytes form a valid ASN.1 element encoding.

        Parameters:
        asn1Element - The byte array containing the ASN.1-encoded element to be written as is.
        offset - The offset in the byte array.
        length - The number of bytes to write.
        Returns:
        A reference to this ASN.1 writer.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(ByteString asn1Element)
        Writes exactly the provided byte string without encoding the data.

        It is the caller responsibility to ensure the provided byte string form a valid ASN.1 element encoding.

        Parameters:
        asn1Element - The ASN.1-encoded element to be written as is.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(BigInteger value)
        Writes an integer element from the provided BigInteger value using the Universal Integer ASN.1 type tag.
        Parameters:
        value - The BigInteger to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(byte type,
                                       BigInteger value)
        Writes an integer element from the provided BigInteger using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The BigInteger to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeBitString

        public Asn1Writer writeBitString​(ByteString value)
        Writes the provided bit string with zero unused bits using the Universal Bit String ASN.1 type tag.
        Parameters:
        value - The bit string value.
        Returns:
        A reference to this ASN.1 writer.
      • writeBitString

        public Asn1Writer writeBitString​(byte type,
                                         ByteString value)
        Writes the provided bit string with zero unused bits using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The bit string value.
        Returns:
        A reference to this ASN.1 writer.
      • writeBitString

        public Asn1Writer writeBitString​(BitSet value)
        Writes the provided bitSet as a bit string, the trailing zero bits are encoded as unused, using the Universal Bit String ASN.1 type tag.
        Parameters:
        value - The bit set to be written as a bit string.
        Returns:
        A reference to this ASN.1 writer.
      • writeBitString

        public Asn1Writer writeBitString​(byte type,
                                         BitSet value)
        Writes the provided bitSet as a bitstring, the trailing zero bits are encoded as unused, using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The bit set to be written as a bit string.
        Returns:
        A reference to this ASN.1 writer.
      • writeBoolean

        public Asn1Writer writeBoolean​(boolean value)
        Writes a boolean element using the Universal Boolean ASN.1 type tag.
        Parameters:
        value - The boolean value.
        Returns:
        A reference to this ASN.1 writer.
      • writeBoolean

        public Asn1Writer writeBoolean​(byte type,
                                       boolean value)
        Writes a boolean element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The boolean value.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           byte[] value)
        Writes an octet string element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The byte array containing the octet string data.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           byte[] value,
                                           int offset,
                                           int length)
        Writes an octet string element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The byte array containing the octet string data.
        offset - The offset in the byte array.
        length - The number of bytes to write.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           ByteString value)
        Writes an octet string element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The octet string value.
        Returns:
        A reference to this ASN.1 writer.
      • writeEnumerated

        public Asn1Writer writeEnumerated​(int value)
        Writes an enumerated element using the Universal Enumerated ASN.1 type tag.
        Parameters:
        value - The enumerated value.
        Returns:
        A reference to this ASN.1 writer.
      • writeEnumerated

        public Asn1Writer writeEnumerated​(byte type,
                                          int value)
        Writes an enumerated element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The enumerated value.
        Returns:
        A reference to this ASN.1 writer.
      • writeGeneralizedTime

        public Asn1Writer writeGeneralizedTime​(Instant value)
        Writes a generalized time without fractional seconds, using the Universal Generalized Time ASN.1 type tag.
        Parameters:
        value - The time to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeGeneralizedTime

        public Asn1Writer writeGeneralizedTime​(byte type,
                                               Instant value)
        Writes a generalized time without fractional seconds, using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The time to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(byte type,
                                       int value)
        Writes an integer element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The integer value.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(int value)
        Writes an integer element using the Universal Integer ASN.1 type tag.
        Parameters:
        value - The integer value.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(long value)
        Writes an integer element using the Universal Integer ASN.1 type tag.
        Parameters:
        value - The integer value.
        Returns:
        A reference to this ASN.1 writer.
      • writeInteger

        public Asn1Writer writeInteger​(byte type,
                                       long value)
        Writes an integer element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The integer value.
        Returns:
        A reference to this ASN.1 writer.
      • writeName

        public Asn1Writer writeName​(Dn value)
        Writes the X.501 type Name as an RDN sequence. In accordance with the X.501 standard, the RDNs are written in descending order.
        Parameters:
        value - The distinguished name (with RDNs in ascending order) representing the name to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeName

        public Asn1Writer writeName​(byte type,
                                    Dn value)
        Writes the X.501 type Name as an RDN sequence with the provided type tag. In accordance with the X.501 standard, the RDNs are written in descending order.
        Parameters:
        type - The type tag of the element.
        value - The distinguished name (with RDNs in ascending order) representing the name to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeNull

        public Asn1Writer writeNull()
        Writes a null element using the Universal Null ASN.1 type tag.
        Returns:
        A reference to this ASN.1 writer.
      • writeNull

        public Asn1Writer writeNull​(byte type)
        Writes a null element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        Returns:
        A reference to this ASN.1 writer.
      • writeObjectIdentifier

        public Asn1Writer writeObjectIdentifier​(String oid)
        Writes the provided object identifier using the Universal Object Identifier type tag.
        Parameters:
        oid - The String representation of the object identifier to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeObjectIdentifier

        public Asn1Writer writeObjectIdentifier​(byte type,
                                                String oid)
        Writes the provided object identifier with the provided type tag.
        Parameters:
        type - The type tag of the element.
        oid - The String representation of the object identifier to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte[] value)
        Writes an octet string element using the Universal Octet String ASN.1 type tag.
        Parameters:
        value - The byte array containing the octet string data.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte[] value,
                                           int offset,
                                           int length)
        Writes an octet string element using the Universal Octet String ASN.1 type tag.
        Parameters:
        value - The byte array containing the octet string data.
        offset - The offset in the byte array.
        length - The number of bytes to write.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(ByteString value)
        Writes an octet string element using the Universal Octet String ASN.1 type tag.
        Parameters:
        value - The octet string value.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(CharSequence value)
        Writes a string as a UTF-8 encoded octet string element using the Universal Octet String ASN.1 type tag.
        Parameters:
        value - The string to be written as a UTF-8 encoded octet string.
        Returns:
        A reference to this ASN.1 writer.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           CharSequence value)
        Writes a string as a UTF-8 encoded octet string element using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The string to be written as a UTF-8 encoded octet string.
        Returns:
        A reference to this ASN.1 writer.
      • writeSequenceOfOctetStrings

        public Asn1Writer writeSequenceOfOctetStrings​(Collection<ByteString> values)
        Writes a sequence of octet string elements using the Universal Sequence ASN.1 type tag for the sequence and Universal Octet String ASN.1 type tag for the elements. This implementation is optimized for the case where the sequence contains zero or one elements.
        Parameters:
        values - The octet string values.
        Returns:
        A reference to this ASN.1 writer.
      • writeSequenceOfUtf8OctetStrings

        public Asn1Writer writeSequenceOfUtf8OctetStrings​(Collection<? extends CharSequence> values)
        Writes a sequence of strings as a sequence of UTF-8 encoded octet string elements using the Universal Sequence ASN.1 type tag for the sequence and Universal Octet String ASN.1 type tag for the elements. This implementation is optimized for the case where the sequence contains zero or one elements.
        Parameters:
        values - The strings to be written as a sequence of UTF-8 encoded octet strings.
        Returns:
        A reference to this ASN.1 writer.
      • writeSetOfOctetStrings

        public Asn1Writer writeSetOfOctetStrings​(Collection<ByteString> values)
        Writes a set of octet string elements using the Universal Set ASN.1 type tag for the set and Universal Octet String ASN.1 type tag for the elements. This implementation is optimized for the case where the set contains zero or one elements.
        Parameters:
        values - The octet string values.
        Returns:
        A reference to this ASN.1 writer.
      • writeSetOfUtf8OctetStrings

        public Asn1Writer writeSetOfUtf8OctetStrings​(Collection<? extends CharSequence> values)
        Writes a set of strings as a set of UTF-8 encoded octet string elements using the Universal Set ASN.1 type tag for the set and Universal Octet String ASN.1 type tag for the elements. This implementation is optimized for the case where the set contains zero or one elements.
        Parameters:
        values - The strings to be written as a set of UTF-8 encoded octet strings.
        Returns:
        A reference to this ASN.1 writer.
      • writeExplicitTag

        public Asn1Writer writeExplicitTag​(byte type)
        Writes an explicit tag element. The returned ASN.1 writer must be closed once the value for the tag has been written.
        Parameters:
        type - The type tag of the element.
        Returns:
        An ASN.1 writer representing the tag which must be closed once the value for the tag has been written.
      • writeExplicitTag

        public Asn1Writer writeExplicitTag​(long type)
        Writes an explicit tag element having the provided multi-byte tag type encoded as described in Asn1Tag.numberMultiByte(long). The returned ASN.1 writer must be closed once the value for the tag has been written.
        Parameters:
        type - The type tag of the element, encoded as described in Asn1Tag.numberMultiByte(long).
        Returns:
        An ASN.1 writer representing the tag which must be closed once the value for the tag has been written.
      • writeSequence

        public Asn1Writer writeSequence()
        Writes a sequence element using the Universal Sequence ASN.1 type tag. The returned ASN.1 writer must be closed once the sequence elements have been written.
        Returns:
        An ASN.1 writer representing the sequence which must be closed once the sequence elements have been written.
      • writeSequence

        public Asn1Writer writeSequence​(byte type)
        Writes a sequence element using the provided type tag. The returned ASN.1 writer must be closed once the sequence elements have been written.
        Parameters:
        type - The type tag of the element.
        Returns:
        An ASN.1 writer representing the sequence which must be closed once the sequence elements have been written.
      • writeSet

        public Asn1Writer writeSet()
        Writes a set element using the Universal Set ASN.1 type tag. The returned ASN.1 writer must be closed once the set elements have been written.
        Returns:
        An ASN.1 writer representing the set which must be closed once the set elements have been written.
      • writeSet

        public Asn1Writer writeSet​(byte type)
        Writes a set element using the provided type tag. The returned ASN.1 writer must be closed once the set elements have been written.
        Parameters:
        type - The type tag of the element.
        Returns:
        An ASN.1 writer representing the set which must be closed once the set elements have been written.
      • writeUtcTime

        public Asn1Writer writeUtcTime​(Instant value)
        Writes a UTC time without fractional seconds using the Universal UTC Time ASN.1 type tag.
        Parameters:
        value - The time to be written.
        Returns:
        A reference to this ASN.1 writer.
      • writeUtcTime

        public Asn1Writer writeUtcTime​(byte type,
                                       Instant value)
        Writes a UTCTime without fractional seconds using the provided type tag.
        Parameters:
        type - The type tag of the element.
        value - The time to be written.
        Returns:
        A reference to this ASN.1 writer.