Package org.forgerock.opendj.io
Class Asn1Writer
java.lang.Object
org.forgerock.opendj.io.Asn1Writer
- All Implemented Interfaces:
Closeable
,AutoCloseable
An ASN.1 encoder writes ASN.1 elements to an internal byte buffer. The encoded data can be obtained using
toByteArray()
, toByteString()
or asByteBuffer()
.-
Method Summary
Modifier and TypeMethodDescriptionReturns a mutableByteBuffer
view of this ASN.1 encoded data.void
clear()
Clears the content of this ASN.1 writer.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.void
close()
Writes the end of the current ASN.1 sequence, set or explicit tag.byte[]
Returns a byte array containing the ASN.1 encoded data and clears the contents of this writer.Returns aByteString
containing the ASN.1 encoded data and clears the contents of this writer.writeAsn1Element
(byte[] asn1Element) Writes exactly the provided bytes without encoding the data.writeAsn1Element
(byte[] asn1Element, int offset, int length) Writes exactly the provided bytes without encoding the data.writeAsn1Element
(ByteString asn1Element) Writes exactly the provided byte string without encoding the data.writeBitString
(byte type, BitSet value) Writes the providedbitSet
as a bitstring, the trailing zero bits are encoded as unused, using the provided type tag.writeBitString
(byte type, ByteString value) Writes the provided bit string with zero unused bits using the provided type tag.writeBitString
(BitSet value) Writes the providedbitSet
as a bit string, the trailing zero bits are encoded as unused, using the Universal Bit String ASN.1 type tag.writeBitString
(ByteString value) Writes the provided bit string with zero unused bits using the Universal Bit String ASN.1 type tag.writeBoolean
(boolean value) Writes a boolean element using the Universal Boolean ASN.1 type tag.writeBoolean
(byte type, boolean value) Writes a boolean element using the provided type tag.writeEnumerated
(byte type, int value) Writes an enumerated element using the provided type tag.writeEnumerated
(int value) Writes an enumerated element using the Universal Enumerated ASN.1 type tag.writeExplicitTag
(byte type) Writes an explicit tag element.writeExplicitTag
(long type) Writes an explicit tag element having the provided multi-byte tag type encoded as described inAsn1Tag.numberMultiByte(long)
.writeGeneralizedTime
(byte type, Instant value) Writes a generalized time without fractional seconds, using the provided type tag.writeGeneralizedTime
(Instant value) Writes a generalized time without fractional seconds, using the Universal Generalized Time ASN.1 type tag.writeInteger
(byte type, int value) Writes an integer element using the provided type tag.writeInteger
(byte type, long value) Writes an integer element using the provided type tag.writeInteger
(byte type, BigInteger value) Writes an integer element from the providedBigInteger
using the provided type tag.writeInteger
(int value) Writes an integer element using the Universal Integer ASN.1 type tag.writeInteger
(long value) Writes an integer element using the Universal Integer ASN.1 type tag.writeInteger
(BigInteger value) Writes an integer element from the providedBigInteger
value using the Universal Integer ASN.1 type tag.Writes the X.501 type Name as an RDN sequence with the provided type tag.Writes the X.501 type Name as an RDN sequence.Writes a null element using the Universal Null ASN.1 type tag.writeNull
(byte type) Writes a null element using the provided type tag.writeObjectIdentifier
(byte type, String oid) Writes the provided object identifier with the provided type tag.Writes the provided object identifier using the Universal Object Identifier type tag.writeOctetString
(byte[] value) Writes an octet string element using the Universal Octet String ASN.1 type tag.writeOctetString
(byte[] value, int offset, int length) Writes an octet string element using the Universal Octet String ASN.1 type tag.writeOctetString
(byte type, byte[] value) Writes an octet string element using the provided type tag.writeOctetString
(byte type, byte[] value, int offset, int length) Writes an octet string element using the provided type tag.writeOctetString
(byte type, CharSequence value) Writes a string as a UTF-8 encoded octet string element using the provided type tag.writeOctetString
(byte type, ByteString value) Writes an octet string element using the provided type tag.writeOctetString
(CharSequence value) Writes a string as a UTF-8 encoded octet string element using the Universal Octet String ASN.1 type tag.writeOctetString
(ByteString value) Writes an octet string element using the Universal Octet String ASN.1 type tag.Writes a sequence element using the Universal Sequence ASN.1 type tag.writeSequence
(byte type) Writes a sequence element using the provided type tag.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.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.writeSet()
Writes a set element using the Universal Set ASN.1 type tag.writeSet
(byte type) Writes a set element using the provided type tag.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.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.writeUtcTime
(byte type, Instant value) Writes a UTCTime without fractional seconds using the provided type tag.writeUtcTime
(Instant value) Writes a UTC time without fractional seconds using the Universal UTC Time ASN.1 type tag.
-
Method Details
-
toByteString
Returns aByteString
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
Returns a mutableByteBuffer
view of this ASN.1 encoded data. Changes to this writer will interfere with the returned byte buffer and vice versa. Callclear()
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 truncatenewCapacity
- The new capacity.- Throws:
IllegalArgumentException
- If thenewCapacity
is negative or thenewCapacity
is bigger than thethresholdCapacity
.
-
close
public void close()Writes the end of the current ASN.1 sequence, set or explicit tag.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IllegalStateException
- If no sequence is actively being written.
-
writeAsn1Element
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
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
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
Writes an integer element from the providedBigInteger
value using the Universal Integer ASN.1 type tag.- Parameters:
value
- TheBigInteger
to be written.- Returns:
- A reference to this ASN.1 writer.
-
writeInteger
Writes an integer element from the providedBigInteger
using the provided type tag.- Parameters:
type
- The type tag of the element.value
- TheBigInteger
to be written.- Returns:
- A reference to this ASN.1 writer.
-
writeBitString
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
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
Writes the providedbitSet
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
Writes the providedbitSet
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Writes a null element using the Universal Null ASN.1 type tag.- Returns:
- A reference to this ASN.1 writer.
-
writeNull
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
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
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
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
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
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
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
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
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
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
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
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
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
Writes an explicit tag element having the provided multi-byte tag type encoded as described inAsn1Tag.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 inAsn1Tag.numberMultiByte(long)
.- Returns:
- An ASN.1 writer representing the tag which must be closed once the value for the tag has been written.
-
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
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
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
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
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
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.
-