Package org.apache.http.util
Class ByteArrayBuffer
- java.lang.Object
-
- org.apache.http.util.ByteArrayBuffer
-
- All Implemented Interfaces:
java.io.Serializable
public final class ByteArrayBuffer extends java.lang.Object implements java.io.Serializable
A resizable byte array.- Since:
- 4.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ByteArrayBuffer(int capacity)
Creates an instance ofByteArrayBuffer
with the given initial capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(byte[] b, int off, int len)
Appendslen
bytes to this buffer from the given source array starting at indexoff
.void
append(char[] b, int off, int len)
Appendslen
chars to this buffer from the given source array starting at indexoff
.void
append(int b)
Appendsb
byte to this buffer.void
append(CharArrayBuffer b, int off, int len)
Appendslen
chars to this buffer from the given source char array buffer starting at indexoff
.byte[]
buffer()
Returns reference to the underlying byte array.int
byteAt(int i)
Returns thebyte
value in this buffer at the specified index.int
capacity()
Returns the current capacity.void
clear()
Clears content of the buffer.void
ensureCapacity(int required)
Ensures that the capacity is at least equal to the specified minimum.int
indexOf(byte b)
Returns the index within this buffer of the first occurrence of the specified byte, starting the search at0
and finishing atlength()
.int
indexOf(byte b, int from, int to)
Returns the index within this buffer of the first occurrence of the specified byte, starting the search at the specifiedbeginIndex
and finishing atendIndex
.boolean
isEmpty()
boolean
isFull()
int
length()
Returns the length of the buffer (byte count).void
setLength(int len)
Sets the length of the buffer.byte[]
toByteArray()
Converts the content of this buffer to an array of bytes.
-
-
-
Constructor Detail
-
ByteArrayBuffer
public ByteArrayBuffer(int capacity)
Creates an instance ofByteArrayBuffer
with the given initial capacity.- Parameters:
capacity
- the capacity
-
-
Method Detail
-
append
public void append(byte[] b, int off, int len)
Appendslen
bytes to this buffer from the given source array starting at indexoff
. The capacity of the buffer is increased, if necessary, to accommodate alllen
bytes.- Parameters:
b
- the bytes to be appended.off
- the index of the first byte to append.len
- the number of bytes to append.- Throws:
java.lang.IndexOutOfBoundsException
- ifoff
if out of range,len
is negative, oroff
+len
is out of range.
-
append
public void append(int b)
Appendsb
byte to this buffer. The capacity of the buffer is increased, if necessary, to accommodate the additional byte.- Parameters:
b
- the byte to be appended.
-
append
public void append(char[] b, int off, int len)
Appendslen
chars to this buffer from the given source array starting at indexoff
. The capacity of the buffer is increased if necessary to accommodate alllen
chars.The chars are converted to bytes using simple cast.
- Parameters:
b
- the chars to be appended.off
- the index of the first char to append.len
- the number of bytes to append.- Throws:
java.lang.IndexOutOfBoundsException
- ifoff
if out of range,len
is negative, oroff
+len
is out of range.
-
append
public void append(CharArrayBuffer b, int off, int len)
Appendslen
chars to this buffer from the given source char array buffer starting at indexoff
. The capacity of the buffer is increased if necessary to accommodate alllen
chars.The chars are converted to bytes using simple cast.
- Parameters:
b
- the chars to be appended.off
- the index of the first char to append.len
- the number of bytes to append.- Throws:
java.lang.IndexOutOfBoundsException
- ifoff
if out of range,len
is negative, oroff
+len
is out of range.
-
clear
public void clear()
Clears content of the buffer. The underlying byte array is not resized.
-
toByteArray
public byte[] toByteArray()
Converts the content of this buffer to an array of bytes.- Returns:
- byte array
-
byteAt
public int byteAt(int i)
Returns thebyte
value in this buffer at the specified index. The index argument must be greater than or equal to0
, and less than the length of this buffer.- Parameters:
i
- the index of the desired byte value.- Returns:
- the byte value at the specified index.
- Throws:
java.lang.IndexOutOfBoundsException
- ifindex
is negative or greater than or equal tolength()
.
-
capacity
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:
- the current capacity
-
length
public int length()
Returns the length of the buffer (byte count).- Returns:
- the length of the buffer
-
ensureCapacity
public void ensureCapacity(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 therequired
argument is non-positive, this method takes no action.- Parameters:
required
- the minimum required capacity.- Since:
- 4.1
-
buffer
public byte[] buffer()
Returns reference to the underlying byte array.- Returns:
- the byte array.
-
setLength
public void setLength(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 to0
.- Parameters:
len
- the new length- Throws:
java.lang.IndexOutOfBoundsException
- if thelen
argument is greater than the current capacity of the buffer or less than0
.
-
isEmpty
public boolean isEmpty()
- Returns:
true
if this buffer is empty,false
otherwise.
-
isFull
public boolean isFull()
- Returns:
true
if this buffer is full,false
otherwise.
-
indexOf
public int indexOf(byte b, int from, int to)
Returns the index within this buffer of the first occurrence of the specified byte, starting the search at the specifiedbeginIndex
and finishing atendIndex
. If no such byte occurs in this buffer within the specified bounds,-1
is returned.There is no restriction on the value of
beginIndex
andendIndex
. IfbeginIndex
is negative, it has the same effect as if it were zero. IfendIndex
is greater thanlength()
, it has the same effect as if it werelength()
. If thebeginIndex
is greater than theendIndex
,-1
is returned.- Parameters:
b
- the byte to search for.from
- the index to start the search from.to
- the index to finish the search at.- 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. - Since:
- 4.1
-
indexOf
public int indexOf(byte b)
Returns the index within this buffer of the first occurrence of the specified byte, starting the search at0
and finishing atlength()
. If no such byte occurs in this buffer within those bounds,-1
is returned.- Parameters:
b
- the byte to search for.- Returns:
- the index of the first occurrence of the byte in the
buffer, or
-1
if the byte does not occur. - Since:
- 4.1
-
-