Orbital library

orbital.util
Class Buffer

java.lang.Object
  extended by orbital.util.Buffer

public class Buffer
extends java.lang.Object

This class encapsulates a Buffer containing bytes. Those bytes in the Buffer are kept as a byte[].

Author:
André Platzer
See Also:
java.nio.Buffer

Field Summary
protected  byte[] buf
          The buffer where data is stored.
protected  int count
          The index one greater than the index of the last valid byte in the buffer.
 
Constructor Summary
Buffer()
          The current position in the buffer.
Buffer(byte[] initialContent)
           
Buffer(byte[] initialContent, int off, int len)
          Creates a new Buffer with an initial content specified by a part of a byte-array.
Buffer(int initialCapacity)
          Creates a new Buffer with an initial capacity, but empty (of size 0).
 
Method Summary
 void append(byte[] b)
          Append a byte[] to the end of this Buffer,
 void append(byte[] b, int off, int len)
          Append a part of a byte[] (starting at off) to the end of this Buffer, increasing its size by the length of b.
 void append(int val, int len)
          Append a byte value len times to the end of this Buffer, increasing its size by len.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this Buffer, if necessary, to ensure that it can hold at least the number of bytes specified by the minimum capacity argument.
 byte[] getBuffer()
          Gets the whole underlying byte-buffer containing the data (size like the whole capacity).
 byte[] getBytes()
          Get a copy of the buffer.
 byte[] getBytes(int len)
          Get a part of the buffer, beginning at front (index 0).
 void getBytes(int srcOff, byte[] dst, int dstOff, int len)
          Get a part of the buffer into a specified byte-array.
 byte[] getBytes(int off, int len)
          Get a part of the buffer.
 void remove(int len)
          Remove len beginning at front (index 0).
 void remove(int off, int len)
          Remove len bytes starting at off from this Buffer, decreasing its size by the len.
 void setBuffer(byte[] buffer)
          Sets the underlying byte-buffer containing the data (without setting the byte-count).
 void setBytes(byte[] part, int dstOff)
           
 void setBytes(byte[] part, int srcOff, int dstOff, int len)
          Set a part of the buffer.
 void setSize(int newSize)
          Sets the size of this vector.
 int size()
          Returns the number of bytes in this buffer.
 void trimToSize()
          Trims the capacity of this buffer to be the buffer's current size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

count

protected int count
The index one greater than the index of the last valid byte in the buffer. It is always true that count<=buf.length.


buf

protected byte[] buf
The buffer where data is stored.

Constructor Detail

Buffer

public Buffer()
The current position in the buffer. This is the index of the next character to be read from the buf array.

See Also:
BufferedInputStream

Buffer

public Buffer(int initialCapacity)
Creates a new Buffer with an initial capacity, but empty (of size 0).


Buffer

public Buffer(byte[] initialContent,
              int off,
              int len)
Creates a new Buffer with an initial content specified by a part of a byte-array.

Parameters:
initialContent - the byte-array containing the data.
off - the index where the relevant data start in initialContent
len - the size of the buffer. len is also the number of bytes that are copied from initialContent.

Buffer

public Buffer(byte[] initialContent)
Method Detail

getBytes

public byte[] getBytes()
Get a copy of the buffer.

Returns:
a copy of the whole buffer.

getBytes

public byte[] getBytes(int len)
Get a part of the buffer, beginning at front (index 0).

Returns:
a copy of the part with length len starting at offset 0.

getBytes

public byte[] getBytes(int off,
                       int len)
Get a part of the buffer.

Returns:
a copy of the part with length len starting at off is returned, null for length 0.

getBytes

public void getBytes(int srcOff,
                     byte[] dst,
                     int dstOff,
                     int len)
Get a part of the buffer into a specified byte-array.

Parameters:
dst - the byte-array to be filled with the part desired. Its dimension must be at least len.
dstOff - the destination offset in dst.
srcOff - the source offset in the Buffer array.
len - the number of bytes to be copied.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the part to be get exceeds the current dimension.
java.lang.IllegalArgumentException - if the offset or length are negative, or the length of dst is less than len.
See Also:
System.arraycopy(Object,int,Object,int,int)

setBytes

public void setBytes(byte[] part,
                     int srcOff,
                     int dstOff,
                     int len)
Set a part of the buffer. Sets the part with length len starting at off.

Parameters:
part - the bytes to be set as new part of the Buffer
srcOff - the source offset in the part array.
dstOff - the destination offset in the Buffer.
len - the number of bytes to be copied.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the part to be set exceeds the current dimension.
java.lang.IllegalArgumentException - if the offset or length are negative.
See Also:
System.arraycopy(Object,int,Object,int,int)

setBytes

public void setBytes(byte[] part,
                     int dstOff)

size

public final int size()
Returns the number of bytes in this buffer.

Returns:
the number of bytes in this buffer.

setSize

public final void setSize(int newSize)
Sets the size of this vector. If the new size is greater than the current size, new null items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.

Parameters:
newSize - the new size of this vector.

trimToSize

public final void trimToSize()
Trims the capacity of this buffer to be the buffer's current size. An application can use this operation to minimize the storage of a buffer.


ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this Buffer, if necessary, to ensure that it can hold at least the number of bytes specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity.
Throws:
java.lang.IllegalArgumentException - if desired minimum capacity is negative.

append

public void append(byte[] b)
Append a byte[] to the end of this Buffer,


append

public void append(byte[] b,
                   int off,
                   int len)
Append a part of a byte[] (starting at off) to the end of this Buffer, increasing its size by the length of b. The capacity of this vector is increased if its size becomes greater than its capacity.


append

public void append(int val,
                   int len)
Append a byte value len times to the end of this Buffer, increasing its size by len. The capacity of this vector is increased if its size becomes greater than its capacity.


remove

public void remove(int len)
Remove len beginning at front (index 0).


remove

public void remove(int off,
                   int len)
Remove len bytes starting at off from this Buffer, decreasing its size by the len.


getBuffer

public final byte[] getBuffer()
Gets the whole underlying byte-buffer containing the data (size like the whole capacity).


setBuffer

public final void setBuffer(byte[] buffer)
Sets the underlying byte-buffer containing the data (without setting the byte-count). So you should call setSize() separately.

See Also:
setSize(int)

Orbital library
1.3.0: 11 Apr 2009

Copyright © 1996-2009 André Platzer
All Rights Reserved.