org.apache.http.util
Class ByteArrayBuffer

java.lang.Object
  extended by org.apache.http.util.ByteArrayBuffer

public final class ByteArrayBuffer
extends Object

A resizable byte array.

Since:
4.0
Version:
$Revision: 744533 $

Constructor Summary
ByteArrayBuffer(int capacity)
          Creates an instance of ByteArrayBuffer with the given initial capacity.
 
Method Summary
 void append(byte[] b, int off, int len)
          Appends len bytes to this buffer from the given source array starting at index off.
 void append(char[] b, int off, int len)
          Appends len chars to this buffer from the given source array starting at index off.
 void append(CharArrayBuffer b, int off, int len)
          Appends len chars to this buffer from the given source char array buffer starting at index off.
 void append(int b)
          Appends b byte to this buffer.
 byte[] buffer()
          Returns reference to the underlying byte array.
 int byteAt(int i)
          Returns the byte value in this buffer at the specified index.
 int capacity()
          Returns the current capacity.
 void clear()
          Clears content of the buffer.
 boolean isEmpty()
          Returns true if this buffer is empty, that is, its length() is equal to 0.
 boolean isFull()
          Returns true if this buffer is full, that is, its length() is equal to its capacity().
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteArrayBuffer

public ByteArrayBuffer(int capacity)
Creates an instance of ByteArrayBuffer with the given initial capacity.

Parameters:
capacity - the capacity
Method Detail

append

public void append(byte[] b,
                   int off,
                   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 - the bytes to be appended.
off - the index of the first byte to append.
len - the number of bytes to append.
Throws:
IndexOutOfBoundsException - if off if out of range, len is negative, or off + len is out of range.

append

public void append(int b)
Appends b 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)
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 - the chars to be appended.
off - the index of the first char to append.
len - the number of bytes to append.
Throws:
IndexOutOfBoundsException - if off if out of range, len is negative, or off + len is out of range.

append

public void append(CharArrayBuffer b,
                   int off,
                   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 - the chars to be appended.
off - the index of the first char to append.
len - the number of bytes to append.
Throws:
IndexOutOfBoundsException - if off if out of range, len is negative, or off + 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 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 - the index of the desired byte value.
Returns:
the byte value at the specified index.
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length().

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

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 to 0.

Parameters:
len - the new length
Throws:
IndexOutOfBoundsException - if the len argument is greater than the current capacity of the buffer or less than 0.

isEmpty

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

Returns:
true if this buffer is empty, false otherwise.

isFull

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

Returns:
true if this buffer is full, false otherwise.


Copyright © 2005-2009 Apache Software Foundation. All Rights Reserved.