Class Asn1Writer

    • Method Detail

      • close

        public void close()
                   throws IOException
        Closes this ASN.1 writer, flushing it first. Closing a previously closed ASN.1 writer has no effect. Any unfinished sequences and/or sets will be ended.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - If an error occurs while closing.
      • writeEndSequence

        public Asn1Writer writeEndSequence()
                                    throws IOException
        Finishes writing a sequence element.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
        IllegalStateException - If there is no sequence being written.
      • flush

        public void flush()
                   throws IOException
        Flushes this ASN.1 writer so that any buffered elements are written immediately to their intended destination. Then, if that destination is another byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of streams.

        If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

        Specified by:
        flush in interface Flushable
        Throws:
        IOException - If an error occurs while flushing.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(byte[] asn1Element)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(byte[] asn1Element,
                                           int offset,
                                           int length)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeAsn1Element

        public Asn1Writer writeAsn1Element​(ByteSequence asn1Element)
                                    throws IOException
        Writes exactly the provided byte sequence without encoding the data.

        It is the caller responsibility to ensure the provided byte sequence 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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBigInteger

        public Asn1Writer writeBigInteger​(BigInteger value)
                                   throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBigInteger

        public Asn1Writer writeBigInteger​(byte type,
                                          BigInteger value)
                                   throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBitString

        public Asn1Writer writeBitString​(byte[] value)
                                  throws IOException
        Writes the provided byte array as a bitstring, as is, with zero unused bits.
        Parameters:
        value - The byte array to be written as a bitstring.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBitString

        public Asn1Writer writeBitString​(BitSet bitSet)
                                  throws IOException
        Writes the provided bitSet as a bitstring, the trailing zero bits are encoded as unused.
        Parameters:
        bitSet - The bitSet to be written as a bitstring.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBoolean

        public Asn1Writer writeBoolean​(boolean value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeBoolean

        public Asn1Writer writeBoolean​(byte type,
                                       boolean value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           byte[] value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           byte[] value,
                                           int offset,
                                           int length)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           ByteSequence value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeEndExplicitTag

        public Asn1Writer writeEndExplicitTag()
                                       throws IOException
        Finishes writing elements within the explicit tag.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeEndSet

        public Asn1Writer writeEndSet()
                               throws IOException
        Finishes writing a set element.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
        IllegalStateException - If there is no set being written.
      • writeEnumerated

        public Asn1Writer writeEnumerated​(int value)
                                   throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeEnumerated

        public Asn1Writer writeEnumerated​(byte type,
                                          int value)
                                   throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeGeneralizedTime

        public Asn1Writer writeGeneralizedTime​(Instant time)
                                        throws IOException
        Writes a GeneralizedTime without fractional seconds.
        Parameters:
        time - The time to be written.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeInteger

        public Asn1Writer writeInteger​(byte type,
                                       int value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeInteger

        public Asn1Writer writeInteger​(int value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeInteger

        public Asn1Writer writeInteger​(long value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeInteger

        public Asn1Writer writeInteger​(byte type,
                                       long value)
                                throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeName

        public Asn1Writer writeName​(Dn dn)
                             throws IOException
        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:
        dn - The Ldap Dn (with RDNs in ascending order) representing the name to be written.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeNull

        public Asn1Writer writeNull()
                             throws IOException
        Writes a null element using the Universal Null ASN.1 type tag.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeNull

        public Asn1Writer writeNull​(byte type)
                             throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeObjectIdentifier

        public Asn1Writer writeObjectIdentifier​(String oid)
                                         throws IOException
        Writes the provided object identifier.
        Parameters:
        oid - The String representation of the object identifier to be written.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte[] value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte[] value,
                                           int offset,
                                           int length)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(ByteSequence value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(CharSequence value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeOctetString

        public Asn1Writer writeOctetString​(byte type,
                                           CharSequence value)
                                    throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeSequenceOfOctetStrings

        public Asn1Writer writeSequenceOfOctetStrings​(Collection<? extends ByteSequence> values)
                                               throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the elements.
      • writeSequenceOfUtf8OctetStrings

        public Asn1Writer writeSequenceOfUtf8OctetStrings​(Collection<? extends CharSequence> values)
                                                   throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the elements.
      • writeSetOfOctetStrings

        public Asn1Writer writeSetOfOctetStrings​(Collection<? extends ByteSequence> values)
                                          throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the elements.
      • writeSetOfUtf8OctetStrings

        public Asn1Writer writeSetOfUtf8OctetStrings​(Collection<? extends CharSequence> values)
                                              throws IOException
        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.
        Throws:
        IOException - If an error occurs while writing the elements.
      • writeStartExplicitTag

        public Asn1Writer writeStartExplicitTag​(byte type)
                                         throws IOException
        Writes an explicit tag element. Once the value for the tag is written, call writeEndExplicitTag().
        Parameters:
        type - The type tag of the element.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeStartSequence

        public Asn1Writer writeStartSequence​(byte type)
                                      throws IOException
        Writes a sequence element using the provided type tag. All further writes will append elements to the sequence until writeEndSequence() is called.
        Parameters:
        type - The type tag of the element.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeStartSequence

        public Asn1Writer writeStartSequence()
                                      throws IOException
        Writes a sequence element using the Universal Sequence ASN.1 type tag. All further writes will append elements to the sequence until writeEndSequence() is called.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeStartSet

        public Asn1Writer writeStartSet()
                                 throws IOException
        Writes a set element using the Universal Set ASN.1 type tag. All further writes will append elements to the set until writeEndSet() is called.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeStartSet

        public Asn1Writer writeStartSet​(byte type)
                                 throws IOException
        Writes a set element using the provided type tag. All further writes will append elements to the set until writeEndSet() is called.
        Parameters:
        type - The type tag of the element.
        Returns:
        A reference to this ASN.1 writer.
        Throws:
        IOException - If an error occurs while writing the element.
      • writeUtcTime

        public Asn1Writer writeUtcTime​(Instant time)
                                throws IOException
        Writes a UTCTime without fractional seconds.
        Parameters:
        time - The time to be written.
        Returns:
        A reference to this X509Certificate writer.
        Throws:
        IOException - If an error occurs while writing the element.