Class ByteStringBuilder
- java.lang.Object
-
- java.io.OutputStream
-
- org.forgerock.opendj.ldap.ByteStringBuilder
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public final class ByteStringBuilder extends OutputStream
A mutable sequence of bytes backed by a byte array.
-
-
Field Summary
Fields Modifier and Type Field Description static intMAX_COMPACT_SIZEMaximum size in bytes of a compact encoded value.
-
Constructor Summary
Constructors Constructor Description ByteStringBuilder()Creates a new byte string builder with an initial capacity of 32 bytes.ByteStringBuilder(int capacity)Creates a new byte string builder with the specified initial capacity.ByteStringBuilder(ByteString bs)Creates a new byte string builder with the content of the providedByteString.ByteStringBuilder(ByteStringBuilder bs)Creates a new byte string builder with the content of the provided byte string builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteStringBuilderappendBerLength(int length)Appends the ASN.1 BER length encoding representation of the provided integer to this byte string builder.ByteStringBuilderappendByte(int b)Appends the provided byte to this byte string builder.ByteStringBuilderappendBytes(byte[] bytes)Appends the provided byte array to this byte string builder.ByteStringBuilderappendBytes(byte[] bytes, int offset, int length)Appends the provided byte array to this byte string builder.voidappendBytes(DataInput stream, int length)Appends the providedDataInputto this byte string builder.intappendBytes(InputStream stream, int length)Appends the providedInputStreamto this byte string builder.ByteStringBuilderappendBytes(ByteBuffer buffer)Appends the providedByteBufferto this byte string builder.ByteStringBuilderappendBytes(ByteBuffer buffer, int length)Appends the providedByteBufferto this byte string builder.ByteStringBuilderappendBytes(ByteString bytes)Appends the providedByteStringto this byte string builder.ByteStringBuilderappendBytes(ByteStringReader reader, int length)Appends the providedByteStringReaderto this byte string builder.ByteStringBuilderappendCompactUnsigned(long value)Appends the compact encoded bytes of the provided unsigned long to this byte string builder.ByteStringBuilderappendInt(int i)Appends the big-endian encoded bytes of the provided integer to this byte string builder.ByteStringBuilderappendLong(long l)Appends the big-endian encoded bytes of the provided long to this byte string builder.ByteStringBuilderappendObject(Object o)Appends the byte string representation of the provided object to this byte string builder.ByteStringBuilderappendShort(int i)Appends the big-endian encoded bytes of the provided short to this byte string builder.ByteStringBuilderappendUtf8(char[] chars)Appends the UTF-8 encoded bytes of the provided char array to this byte string builder.ByteStringBuilderappendUtf8(String s)Appends the UTF-8 encoded bytes of the provided string to this byte string builder.voidclose()Implementation ofOutputStream.close()which doesn't do anything because data is written directly to a byte array.voidflush()Implementation ofOutputStream.flush()which doesn't do anything because data is written directly to a byte array.booleanisEmpty()Returnstrueif this byte string builder has a length of zero.intlength()Returns the length of this byte string builder.ByteStringBuildersetByte(int index, byte b)Sets the byte value at the specified index.ByteStringBuildersetLength(int newLength)Sets the length of this byte string builder.byte[]toByteArray()Returns a byte array containing the contents of this byte string builder.ByteBuffertoByteBuffer()Returns a modifiableByteBuffercontaining the contents of this byte string builder.ByteStringtoByteString()Returns aByteStringcontaining the contents of this byte string builder.StringtoString()Returns the UTF-8 decoded string representation of this byte string builder.voidwrite(byte[] bytes)Appends the provided bytes to this byte string builder.voidwrite(byte[] bytes, int i, int i1)Appends the provided bytes to this byte string builder.voidwrite(int i)Appends the provided byte to this byte string builder.-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
MAX_COMPACT_SIZE
public static final int MAX_COMPACT_SIZE
Maximum size in bytes of a compact encoded value.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ByteStringBuilder
public ByteStringBuilder()
Creates a new byte string builder with an initial capacity of 32 bytes.
-
ByteStringBuilder
public ByteStringBuilder(int capacity)
Creates a new byte string builder with the specified initial capacity.- Parameters:
capacity- The initial capacity.- Throws:
IllegalArgumentException- If thecapacityis negative.
-
ByteStringBuilder
public ByteStringBuilder(ByteString bs)
Creates a new byte string builder with the content of the providedByteString. Its capacity is set to the length of the providedByteString.- Parameters:
bs- TheByteStringto copy
-
ByteStringBuilder
public ByteStringBuilder(ByteStringBuilder bs)
Creates a new byte string builder with the content of the provided byte string builder. Its capacity is set to the capacity of the provided byte string builder.- Parameters:
bs- TheByteStringBuilderto copy.
-
-
Method Detail
-
appendByte
public ByteStringBuilder appendByte(int b)
Appends the provided byte to this byte string builder.Note: this method accepts an
intfor ease of reading and writing.This method only keeps the lowest 8-bits of the provided
int. Higher bits will be truncated. This method performs the equivalent of:
ORint i = ...; int i8bits = i & 0xFF; // only use "i8bits"int i = ...; byte b = (byte) i; // only use "b"- Parameters:
b- The byte to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
public ByteStringBuilder appendBytes(byte[] bytes)
Appends the provided byte array to this byte string builder.An invocation of the form:
src.append(bytes)
Behaves in exactly the same way as the invocation:src.append(bytes, 0, bytes.length);
- Parameters:
bytes- The byte array to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
public ByteStringBuilder appendBytes(byte[] bytes, int offset, int length)
Appends the provided byte array to this byte string builder.- Parameters:
bytes- The byte array to be appended to this byte string builder.offset- The offset of the byte array to be used; must be non-negative and no larger thanbytes.length.length- The length of the byte array to be used; must be non-negative and no larger thanbytes.length - offset.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException- Ifoffsetis negative or iflengthis negative or ifoffset + lengthis greater thanbytes.length.
-
appendBytes
public ByteStringBuilder appendBytes(ByteBuffer buffer)
Appends the providedByteBufferto this byte string builder.- Parameters:
buffer- The byte buffer to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
public ByteStringBuilder appendBytes(ByteBuffer buffer, int length)
Appends the providedByteBufferto this byte string builder.- Parameters:
buffer- The byte buffer to be appended to this byte string builder.length- The number of bytes to be appended frombuffer.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException- Iflengthis less than zero or greater thanbuffer.remaining().
-
appendBytes
public ByteStringBuilder appendBytes(ByteString bytes)
Appends the providedByteStringto this byte string builder.- Parameters:
bytes- The byte string to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
public ByteStringBuilder appendBytes(ByteStringReader reader, int length)
Appends the providedByteStringReaderto this byte string builder.- Parameters:
reader- The byte string reader to be appended to this byte string builder.length- The number of bytes to be appended fromreader.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException- Iflengthis less than zero or greater thanreader.remaining().
-
appendUtf8
public ByteStringBuilder appendUtf8(char[] chars)
Appends the UTF-8 encoded bytes of the provided char array to this byte string builder.- Parameters:
chars- The char array whose UTF-8 encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBytes
public void appendBytes(DataInput stream, int length) throws EOFException, IOException
Appends the providedDataInputto this byte string builder.- Parameters:
stream- The data input stream to be appended to this byte string builder.length- The maximum number of bytes to be appended fromstream.- Throws:
IndexOutOfBoundsException- Iflengthis less than zero.EOFException- If this stream reaches the end before reading all the bytes.IOException- If an I/O error occurs.
-
appendBytes
public int appendBytes(InputStream stream, int length) throws IOException
Appends the providedInputStreamto this byte string builder.- Parameters:
stream- The input stream to be appended to this byte string builder.length- The maximum number of bytes to be appended frombuffer.- Returns:
- The number of bytes read from the input stream, or
-1if the end of the input stream has been reached. - Throws:
IndexOutOfBoundsException- Iflengthis less than zero.IOException- If an I/O error occurs.
-
appendInt
public ByteStringBuilder appendInt(int i)
Appends the big-endian encoded bytes of the provided integer to this byte string builder.- Parameters:
i- The integer whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendLong
public ByteStringBuilder appendLong(long l)
Appends the big-endian encoded bytes of the provided long to this byte string builder.- Parameters:
l- The long whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendCompactUnsigned
public ByteStringBuilder appendCompactUnsigned(long value)
Appends the compact encoded bytes of the provided unsigned long to this byte string builder. This method allows to encode unsigned long up to 56 bits using fewer bytes (from 1 to 8) than append(long). The encoding has the important property that it preserves ordering, so it can be used for keys.- Parameters:
value- The long whose compact encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendObject
public ByteStringBuilder appendObject(Object o)
Appends the byte string representation of the provided object to this byte string builder. The object is converted to a byte string as follows:- if the object is an instance of
ByteStringthen this method is equivalent to callingappendBytes(ByteString) - if the object is a
byte[]then this method is equivalent to callingappendBytes(byte[]) - if the object is a
char[]then this method is equivalent to callingappendUtf8(char[]) - for all other types of object this method is equivalent to calling
appendUtf8(String)with thetoString()representation of the provided object.
LongandIntegerobjects like any other type ofObject. More specifically, the following invocations are not equivalent:append(0)is not equivalent toappend((Object) 0)append(0L)is not equivalent toappend((Object) 0L)
- Parameters:
o- The object to be appended to this byte string builder.- Returns:
- This byte string builder.
- if the object is an instance of
-
appendShort
public ByteStringBuilder appendShort(int i)
Appends the big-endian encoded bytes of the provided short to this byte string builder.Note: this method accepts an
intfor ease of reading and writing.This method only keeps the lowest 16-bits of the provided
int. Higher bits will be truncated. This method performs the equivalent of:
ORint i = ...; int i16bits = i & 0xFFFF; // only use "i16bits"int i = ...; short s = (short) i; // only use "s"- Parameters:
i- The short whose big-endian encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendUtf8
public ByteStringBuilder appendUtf8(String s)
Appends the UTF-8 encoded bytes of the provided string to this byte string builder.- Parameters:
s- The string whose UTF-8 encoding is to be appended to this byte string builder.- Returns:
- This byte string builder.
-
appendBerLength
public ByteStringBuilder appendBerLength(int length)
Appends the ASN.1 BER length encoding representation of the provided integer to this byte string builder.- Parameters:
length- The value to encode using the BER length encoding rules.- Returns:
- This byte string builder.
-
write
public void write(byte[] bytes)
Appends the provided bytes to this byte string builder. Equivalent toappendBytes(byte[]).- Overrides:
writein classOutputStream
-
write
public void write(byte[] bytes, int i, int i1)Appends the provided bytes to this byte string builder. Equivalent toappendBytes(byte[], int, int).- Overrides:
writein classOutputStream
-
write
public void write(int i)
Appends the provided byte to this byte string builder. Equivalent toappendByte(int).- Specified by:
writein classOutputStream
-
flush
public void flush()
Implementation ofOutputStream.flush()which doesn't do anything because data is written directly to a byte array.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream
-
close
public void close()
Implementation ofOutputStream.close()which doesn't do anything because data is written directly to a byte array.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
isEmpty
public boolean isEmpty()
Returnstrueif this byte string builder has a length of zero.- Returns:
trueif this byte string builder has a length of zero.
-
length
public int length()
Returns the length of this byte string builder.- Returns:
- The length of this byte string builder.
-
setByte
public ByteStringBuilder setByte(int index, byte b)
Sets the byte value at the specified index.An index ranges from zero to
length() - 1. The first byte value of the byte string is at index zero, the next at index one, and so on, as for array indexing.- Parameters:
index- The index of the byte to be set.b- The byte to set on this byte string builder.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException- If the index argument is negative or not less than length().
-
setLength
public ByteStringBuilder setLength(int newLength)
Sets the length of this byte string builder.If the
newLengthargument is less than the current length, the length is changed to the specified length.If the
newLengthargument is greater than or equal to the current length, then the capacity is increased and sufficient null bytes are appended so that length becomes thenewLengthargument.The
newLengthargument must be greater than or equal to0.- Parameters:
newLength- The new length.- Returns:
- This byte string builder.
- Throws:
IndexOutOfBoundsException- If thenewLengthargument is negative.
-
toByteArray
public byte[] toByteArray()
Returns a byte array containing the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0. Subsequent changes to this byte string builder will not impact the returned byte array and vice versa.- Returns:
- A byte array containing the contents of this byte string builder.
-
toByteString
public ByteString toByteString()
Returns aByteStringcontaining the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0. Subsequent changes to this byte string builder will not impact the returnedByteString.- Returns:
- The
ByteStringcontaining the contents of this byte string builder.
-
toByteBuffer
public ByteBuffer toByteBuffer()
Returns a modifiableByteBuffercontaining the contents of this byte string builder. On return, this byte string builder will be reset to a capacity of0. Subsequent changes to this byte string builder will not impact the returnedByteBufferand vice versa.- Returns:
- The
ByteBuffercontaining the contents of this byte string builder.
-
-