com.levigo.jadice.document.io
Class ByteArrayBuffer

java.lang.Object
  extended by com.levigo.jadice.document.io.ByteArrayBuffer

public class ByteArrayBuffer
extends Object

A ByteArrayBuffer buffers bytes but is not bound to a fixed capacity like arrays.


Field Summary
protected  byte[] buf
          The the last tail buffer.
protected  List<byte[]> buffers
          A list of already filled up byte arrays.
protected  int count
          Number of bytes in the tail buffer.
protected  int totalSize
          Total number of bytes.
 
Constructor Summary
ByteArrayBuffer()
          Creates new ByteArrayBuffer with default capacity of 4096 bytes
ByteArrayBuffer(int capacity)
          Creates a byte buffer with a certain capacity.
 
Method Summary
 void append(byte b)
          Appends a byte to this buffer
 void append(byte[] b)
          Appends the given array of bytes.
 void append(byte[] b, int off, int len)
          Appends a subarray of the given byte array.
 void append(ByteArrayBuffer buf)
          Appends another ByteArrayBuffer to this buffer.
 void append(char c)
          Appends a character as byte to the buffer.
 void clear()
          Clears the buffer
 byte getByteAt(int index)
          Returns the byte value at the given index
 int getBytesAt(int index, byte[] b, int off, int len)
          Fills up to len bytes from the buffer, and stores them into b starting at index off.
 void setByteAt(int index, byte newValue)
          Changes the byte value at given index
 int size()
          Returns the current size of the buffer.
 byte[] toByteArray()
          Creates a newly allocated byte array.
 String toString()
          Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
 String toString(String enc)
          Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
 void writeTo(OutputStream out)
          Writes the complete contents of this byte buffer output to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

totalSize

protected int totalSize
Total number of bytes.


count

protected int count
Number of bytes in the tail buffer.


buf

protected byte[] buf
The the last tail buffer.


buffers

protected List<byte[]> buffers
A list of already filled up byte arrays.

Constructor Detail

ByteArrayBuffer

public ByteArrayBuffer()
Creates new ByteArrayBuffer with default capacity of 4096 bytes


ByteArrayBuffer

public ByteArrayBuffer(int capacity)
Creates a byte buffer with a certain capacity.

Parameters:
capacity - the initial capacity
Method Detail

append

public void append(byte[] b,
                   int off,
                   int len)
Appends a subarray of the given byte array. The buffer will grow by len bytes.

Parameters:
b - the array to be appended
off - the offset to the start of the array
len - the length of bytes to append

append

public void append(byte[] b)
Appends the given array of bytes.

Parameters:
b - the array to be appended

append

public void append(char c)
Appends a character as byte to the buffer.

Parameters:
c - the character to be appended

append

public void append(ByteArrayBuffer buf)
Appends another ByteArrayBuffer to this buffer.

Parameters:
buf - the ByteArrayBuffer to be appended

append

public void append(byte b)
Appends a byte to this buffer

Parameters:
b - the byte to be appended

getByteAt

public byte getByteAt(int index)
Returns the byte value at the given index

Parameters:
index - the index of the byte value looked for
Returns:
byte the byte value at index position
Throws:
ArrayIndexOutOfBoundsException - if index is smaller than 0 or greater then the current buffer size

getBytesAt

public int getBytesAt(int index,
                      byte[] b,
                      int off,
                      int len)
Fills up to len bytes from the buffer, and stores them into b starting at index off.

Parameters:
index - the index of the byte values looked for
b - the byte array to fill up
off - the starting position within b to fill up to.
len - the maximum number of bytes to fill in.
Returns:
the number of bytes actually filled into b
Throws:
ArrayIndexOutOfBoundsException - if index is smaller than 0 or greater then the current buffer size or if off is negative, len is negative, or off + len is greater than b.length.

setByteAt

public void setByteAt(int index,
                      byte newValue)
Changes the byte value at given index

Parameters:
index - the index of the byte value looked for
newValue - the new byte value at index position
Throws:
ArrayIndexOutOfBoundsException - if index is smaller than 0 or greater then the current buffer size

clear

public void clear()
Clears the buffer


toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns:
the current contents of this output stream, as a byte array.

size

public int size()
Returns the current size of the buffer.

Returns:
the value of the the total size of bytes contained in this buffer

toString

public String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Overrides:
toString in class Object
Returns:
String translated from the buffer's contents.

toString

public String toString(String enc)
                throws UnsupportedEncodingException
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.

Parameters:
enc - a character-encoding name.
Returns:
String translated from the buffer's contents.
Throws:
UnsupportedEncodingException - If the named encoding is not supported.

writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the complete contents of this byte buffer output to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

Parameters:
out - the output stream to which to write the data.
Throws:
IOException - if an I/O error occurs.


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.