Package org.forgerock.opendj.io
Class Asn1Writer
- java.lang.Object
-
- org.forgerock.opendj.io.Asn1Writer
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public final class Asn1Writer extends Object implements Closeable
An ASN.1 encoder writes ASN.1 elements to an internal byte buffer. The encoded data can be obtained usingtoByteArray()
,toByteString()
orasByteBuffer()
.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBuffer
asByteBuffer()
Returns 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[]
toByteArray()
Returns a byte array containing the ASN.1 encoded data and clears the contents of this writer.ByteString
toByteString()
Returns aByteString
containing the ASN.1 encoded data and clears the contents of this writer.Asn1Writer
writeAsn1Element(byte[] asn1Element)
Writes exactly the provided bytes without encoding the data.Asn1Writer
writeAsn1Element(byte[] asn1Element, int offset, int length)
Writes exactly the provided bytes without encoding the data.Asn1Writer
writeAsn1Element(ByteString asn1Element)
Writes exactly the provided byte string without encoding the data.Asn1Writer
writeBitString(byte type, BitSet value)
Writes the providedbitSet
as a bitstring, the trailing zero bits are encoded as unused, using the provided type tag.Asn1Writer
writeBitString(byte type, ByteString value)
Writes the provided bit string with zero unused bits using the provided type tag.Asn1Writer
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.Asn1Writer
writeBitString(ByteString value)
Writes the provided bit string with zero unused bits using the Universal Bit String ASN.1 type tag.Asn1Writer
writeBoolean(boolean value)
Writes a boolean element using the Universal Boolean ASN.1 type tag.Asn1Writer
writeBoolean(byte type, boolean value)
Writes a boolean element using the provided type tag.Asn1Writer
writeEnumerated(byte type, int value)
Writes an enumerated element using the provided type tag.Asn1Writer
writeEnumerated(int value)
Writes an enumerated element using the Universal Enumerated ASN.1 type tag.Asn1Writer
writeExplicitTag(byte type)
Writes an explicit tag element.Asn1Writer
writeExplicitTag(long type)
Writes an explicit tag element having the provided multi-byte tag type encoded as described inAsn1Tag.numberMultiByte(long)
.Asn1Writer
writeGeneralizedTime(byte type, Instant value)
Writes a generalized time without fractional seconds, using the provided type tag.Asn1Writer
writeGeneralizedTime(Instant value)
Writes a generalized time without fractional seconds, using the Universal Generalized Time ASN.1 type tag.Asn1Writer
writeInteger(byte type, int value)
Writes an integer element using the provided type tag.Asn1Writer
writeInteger(byte type, long value)
Writes an integer element using the provided type tag.Asn1Writer
writeInteger(byte type, BigInteger value)
Writes an integer element from the providedBigInteger
using the provided type tag.Asn1Writer
writeInteger(int value)
Writes an integer element using the Universal Integer ASN.1 type tag.Asn1Writer
writeInteger(long value)
Writes an integer element using the Universal Integer ASN.1 type tag.Asn1Writer
writeInteger(BigInteger value)
Writes an integer element from the providedBigInteger
value using the Universal Integer ASN.1 type tag.Asn1Writer
writeName(byte type, Dn value)
Writes the X.501 type Name as an RDN sequence with the provided type tag.Asn1Writer
writeName(Dn value)
Writes the X.501 type Name as an RDN sequence.Asn1Writer
writeNull()
Writes a null element using the Universal Null ASN.1 type tag.Asn1Writer
writeNull(byte type)
Writes a null element using the provided type tag.Asn1Writer
writeObjectIdentifier(byte type, String oid)
Writes the provided object identifier with the provided type tag.Asn1Writer
writeObjectIdentifier(String oid)
Writes the provided object identifier using the Universal Object Identifier type tag.Asn1Writer
writeOctetString(byte[] value)
Writes an octet string element using the Universal Octet String ASN.1 type tag.Asn1Writer
writeOctetString(byte[] value, int offset, int length)
Writes an octet string element using the Universal Octet String ASN.1 type tag.Asn1Writer
writeOctetString(byte type, byte[] value)
Writes an octet string element using the provided type tag.Asn1Writer
writeOctetString(byte type, byte[] value, int offset, int length)
Writes an octet string element using the provided type tag.Asn1Writer
writeOctetString(byte type, CharSequence value)
Writes a string as a UTF-8 encoded octet string element using the provided type tag.Asn1Writer
writeOctetString(byte type, ByteString value)
Writes an octet string element using the provided type tag.Asn1Writer
writeOctetString(CharSequence value)
Writes a string as a UTF-8 encoded octet string element using the Universal Octet String ASN.1 type tag.Asn1Writer
writeOctetString(ByteString value)
Writes an octet string element using the Universal Octet String ASN.1 type tag.Asn1Writer
writeSequence()
Writes a sequence element using the Universal Sequence ASN.1 type tag.Asn1Writer
writeSequence(byte type)
Writes a sequence element using the provided type tag.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.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.Asn1Writer
writeSet()
Writes a set element using the Universal Set ASN.1 type tag.Asn1Writer
writeSet(byte type)
Writes a set element using the provided type tag.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.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.Asn1Writer
writeUtcTime(byte type, Instant value)
Writes a UTCTime without fractional seconds using the provided type tag.Asn1Writer
writeUtcTime(Instant value)
Writes a UTC time without fractional seconds using the Universal UTC Time ASN.1 type tag.
-
-
-
Method Detail
-
toByteString
public ByteString 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
public ByteBuffer 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
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 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
public Asn1Writer writeInteger(byte type, BigInteger value)
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
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 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
public Asn1Writer writeBitString(byte type, BitSet value)
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
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 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
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.
-
-