Top Description Fields Constructors Methods
org.apache.hc.core5.util

public final Class ByteArrayBuffer

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.nio.ByteBuffer

A resizable byte array.
Since
4.0

Field Summary

Modifier and TypeField and Description
private byte[]
private int
private static final long

Constructor Summary

AccessConstructor and Description
public
ByteArrayBuffer(final int
the capacity
capacity
)

Creates an instance of ByteArrayBuffer with the given initial capacity.

Method Summary

Modifier and TypeMethod and Description
public void
append(final byte[]
the bytes to be appended.
b
,
final int
the index of the first byte to append.
off
,
final int
the number of bytes to append.
len
)

Appends len bytes to this buffer from the given source array starting at index off.

public void
append(final int
the byte to be appended.
b
)

Appends b byte to this buffer.

public void
append(final char[]
the chars to be appended.
b
,
final int
the index of the first char to append.
off
,
final int
the number of bytes to append.
len
)

Appends len chars to this buffer from the given source array starting at index off.

public void
append(final CharArrayBuffer
the chars to be appended.
b
,
final int
the index of the first char to append.
off
,
final int
the number of bytes to append.
len
)

Appends len chars to this buffer from the given source char array buffer starting at index off.

public void
append(final ByteBuffer buffer)

public byte[]

Returns:

the byte array.
array
()

Returns reference to the underlying byte array.

public int

Returns:

the byte value at the specified index.
byteAt
(final int
the index of the desired byte value.
i
)

Returns the byte value in this buffer at the specified index.

public int

Returns:

the current capacity
capacity
()

Returns the current capacity.

public void
clear()

Clears content of the buffer.

public void
ensureCapacity(final int
the minimum required capacity.
required
)

Ensures that the capacity is at least equal to the specified minimum.

private void
expand(final int newlen)

public int

Returns:

the index of the first occurrence of the byte in the buffer within the given bounds, or -1 if the byte does not occur.
indexOf
(final byte
the byte to search for.
b
,
final int
the index to start the search from.
from
,
final int
the index to finish the search at.
to
)

Returns the index within this buffer of the first occurrence of the specified byte, starting the search at the specified beginIndex and finishing at endIndex.

public int

Returns:

the index of the first occurrence of the byte in the buffer, or -1 if the byte does not occur.
indexOf
(final byte
the byte to search for.
b
)

Returns the index within this buffer of the first occurrence of the specified byte, starting the search at 0 and finishing at length().

public boolean

Returns:

true if this buffer is empty, false otherwise.
isEmpty
()

Returns true if this buffer is empty, that is, its length() is equal to 0.

public boolean

Returns:

true if this buffer is full, false otherwise.
isFull
()

Returns true if this buffer is full, that is, its length() is equal to its capacity().

public int

Returns:

the length of the buffer
length
()

Returns the length of the buffer (byte count).

public void
setLength(final int
the new length
len
)

Sets the length of the buffer.

public byte[]

Returns:

byte array
toByteArray
()

Converts the content of this buffer to an array of bytes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayback to summary
private byte[] array
lenback to summary
private int len
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

ByteArrayBufferback to summary
public ByteArrayBuffer(final int capacity)

Creates an instance of ByteArrayBuffer with the given initial capacity.

Parameters
capacity:int

the capacity

Method Detail

appendback to summary
public void append(final byte[] b, final int off, final int len)

Appends len bytes to this buffer from the given source array starting at index off. The capacity of the buffer is increased, if necessary, to accommodate all len bytes.

Parameters
b:byte[]

the bytes to be appended.

off:int

the index of the first byte to append.

len:int

the number of bytes to append.

Exceptions
IndexOutOfBoundsException:
if off if out of range, len is negative, or off + len is out of range.
appendback to summary
public void append(final int b)

Appends b byte to this buffer. The capacity of the buffer is increased, if necessary, to accommodate the additional byte.

Parameters
b:int

the byte to be appended.

appendback to summary
public void append(final char[] b, final int off, final int len)

Appends len chars to this buffer from the given source array starting at index off. The capacity of the buffer is increased if necessary to accommodate all len chars.

The chars are converted to bytes using simple cast.

Parameters
b:char[]

the chars to be appended.

off:int

the index of the first char to append.

len:int

the number of bytes to append.

Exceptions
IndexOutOfBoundsException:
if off if out of range, len is negative, or off + len is out of range.
appendback to summary
public void append(final CharArrayBuffer b, final int off, final int len)

Appends len chars to this buffer from the given source char array buffer starting at index off. The capacity of the buffer is increased if necessary to accommodate all len chars.

The chars are converted to bytes using simple cast.

Parameters
b:CharArrayBuffer

the chars to be appended.

off:int

the index of the first char to append.

len:int

the number of bytes to append.

Exceptions
IndexOutOfBoundsException:
if off if out of range, len is negative, or off + len is out of range.
appendback to summary
public void append(final ByteBuffer buffer)
arrayback to summary
public byte[] array()

Returns reference to the underlying byte array.

Returns:byte[]

the byte array.

byteAtback to summary
public int byteAt(final int i)

Returns the byte value in this buffer at the specified index. The index argument must be greater than or equal to 0, and less than the length of this buffer.

Parameters
i:int

the index of the desired byte value.

Returns:int

the byte value at the specified index.

Exceptions
IndexOutOfBoundsException:
if index is negative or greater than or equal to length().
capacityback to summary
public int capacity()

Returns the current capacity. The capacity is the amount of storage available for newly appended bytes, beyond which an allocation will occur.

Returns:int

the current capacity

clearback to summary
public void clear()

Clears content of the buffer. The underlying byte array is not resized.

ensureCapacityback to summary
public void ensureCapacity(final int required)

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. If the required argument is non-positive, this method takes no action.

Parameters
required:int

the minimum required capacity.

Since
4.1
expandback to summary
private void expand(final int newlen)
indexOfback to summary
public int indexOf(final byte b, final int from, final int to)

Returns the index within this buffer of the first occurrence of the specified byte, starting the search at the specified beginIndex and finishing at endIndex. If no such byte occurs in this buffer within the specified bounds, -1 is returned.

There is no restriction on the value of beginIndex and endIndex. If beginIndex is negative, it has the same effect as if it were zero. If endIndex is greater than length(), it has the same effect as if it were length(). If the beginIndex is greater than the endIndex, -1 is returned.

Parameters
b:byte

the byte to search for.

from:int

the index to start the search from.

to:int

the index to finish the search at.

Returns:int

the index of the first occurrence of the byte in the buffer within the given bounds, or -1 if the byte does not occur.

Since
4.1
indexOfback to summary
public int indexOf(final byte b)

Returns the index within this buffer of the first occurrence of the specified byte, starting the search at 0 and finishing at length(). If no such byte occurs in this buffer within those bounds, -1 is returned.

Parameters
b:byte

the byte to search for.

Returns:int

the index of the first occurrence of the byte in the buffer, or -1 if the byte does not occur.

Since
4.1
isEmptyback to summary
public boolean isEmpty()

Returns true if this buffer is empty, that is, its length() is equal to 0.

Returns:boolean

true if this buffer is empty, false otherwise.

isFullback to summary
public boolean isFull()

Returns true if this buffer is full, that is, its length() is equal to its capacity().

Returns:boolean

true if this buffer is full, false otherwise.

lengthback to summary
public int length()

Returns the length of the buffer (byte count).

Returns:int

the length of the buffer

setLengthback to summary
public void setLength(final int len)

Sets the length of the buffer. The new length value is expected to be less than the current capacity and greater than or equal to 0.

Parameters
len:int

the new length

Exceptions
IndexOutOfBoundsException:
if the len argument is greater than the current capacity of the buffer or less than 0.
toByteArrayback to summary
public byte[] toByteArray()

Converts the content of this buffer to an array of bytes.

Returns:byte[]

byte array