public class ArrayBuilder extends Object
This class provides a Builder pattern to allow generating a byte array from other byte arrays, or from some common KLV types.
It supports method chaining to make the code more fluent.
Constructor and Description |
---|
ArrayBuilder()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
ArrayBuilder |
append(byte[] bytes)
Append a byte array.
|
ArrayBuilder |
append(UniversalLabel universalLabel)
Append a Universal Label.
|
ArrayBuilder |
appendAsBerLength(int length)
Append an unsigned integer length encoded as a BER value.
|
ArrayBuilder |
appendAsBit(boolean b)
Append a Boolean value as a single bit.
|
ArrayBuilder |
appendAsFloat32Primitive(float value)
Append a 32-bit floating point (
float ) value. |
ArrayBuilder |
appendAsFloat64Primitive(double value)
Append a 64-bit floating point (
double ) value. |
ArrayBuilder |
appendAsInt32Primitive(int value)
Append a 32-bit signed integer (
int ) value. |
ArrayBuilder |
appendAsOID(int value)
Append an unsigned integer encoded as a BER-OID value.
|
ArrayBuilder |
appendAsUInt32Primitive(long value)
Append a 32-bit unsigned integer (
long with range of uint32 ) value. |
ArrayBuilder |
appendByte(byte b)
Append a single byte.
|
ArrayBuilder |
flushBitBuffer()
Flush any stored bits to the array.
|
ArrayBuilder |
prepend(UniversalLabel universalLabel)
Prepend a Universal Label to the start of the array.
|
ArrayBuilder |
prependLength()
Prepend a BER encoded length of the following bytes to the byte array.
|
ArrayBuilder |
prependLengthPlus(int additionalLength)
Prepend a BER encoded length of the following bytes to the byte array.
|
byte[] |
toBytes()
Build the byte array from the appended parts.
|
public ArrayBuilder append(byte[] bytes)
bytes
- the byte array to append.public ArrayBuilder appendByte(byte b)
b
- the byte to append.public ArrayBuilder appendAsBit(boolean b)
b
- the boolean value to append.public ArrayBuilder flushBitBuffer()
If there are not enough bits to fill a byte, the last byte will be zero-bit filled.
public ArrayBuilder appendAsOID(int value)
value
- the unsigned integer value.public ArrayBuilder appendAsBerLength(int length)
This is usually what you want for a length field.
If the encoding says "BER-OID", use appendAsOID(int)
instead.
length
- the unsigned integer value.public ArrayBuilder appendAsFloat64Primitive(double value)
double
) value.value
- the value to appendpublic ArrayBuilder appendAsFloat32Primitive(float value)
float
) value.value
- the value to appendpublic ArrayBuilder appendAsInt32Primitive(int value)
int
) value.value
- the value to appendpublic ArrayBuilder appendAsUInt32Primitive(long value)
long
with range of uint32
) value.value
- the value to appendpublic ArrayBuilder append(UniversalLabel universalLabel)
universalLabel
- the value to appendpublic ArrayBuilder prepend(UniversalLabel universalLabel)
universalLabel
- the value to prependpublic ArrayBuilder prependLength()
This is the type of representation used for Variable Length Packs. Be careful about subsequent operations that append to the array, because this length field will not be updated.
public ArrayBuilder prependLengthPlus(int additionalLength)
This is like prependLength
, but allows an additional (known) number of bytes to be
include in the length, which is useful for adding checksum-type information.
Be careful about subsequent operations that append to the array, because this length field will not be updated.
additionalLength
- additional length to add to the calculated length.public byte[] toBytes()
Copyright © 2022 West Ridge Systems. All rights reserved.