net.i2p.client.streaming
Class ByteCollector

java.lang.Object
  extended by net.i2p.client.streaming.ByteCollector

 class ByteCollector
extends java.lang.Object

Like a StringBuffer, but for bytes. This class is not internally synchronized, so care should be taken when using in a multithreaded environment.


Field Summary
(package private)  byte[] contents
           
(package private)  int size
           
 
Constructor Summary
ByteCollector()
          New collector with the default initial capacity
ByteCollector(byte b)
          New collector with the specified byte
ByteCollector(byte[] b)
          New collector containing the specified bytes
ByteCollector(int capacity)
          New collector with an initial capacity as specified
 
Method Summary
 ByteCollector append(byte b)
          Add a new byte to the collector (extending the buffer if necessary)
 ByteCollector append(byte[] b)
          Add new bytes to the collector (extending the buffer if necessary)
 ByteCollector append(byte[] b, int len)
          Add new bytes to the collector (extending the buffer if necessary)
 ByteCollector append(byte[] b, int off, int len)
          Add new bytes to the collector (extending the buffer if necessary)
 ByteCollector append(ByteCollector bc)
          Add the contents of the byte collector to the current collector (extending the buffer if necessary)
 void clear()
          Empty the collector.
 void clearAndShorten()
          Empty the collector and reduce its capacity.
 boolean ensureCapacity(int cap)
          Make sure we have sufficient storage space.
 boolean equals(java.lang.Object o)
          Compare the collectors.
 int getCurrentSize()
          How many bytes are available for retrieval?
 int hashCode()
           
 int indexOf(byte b)
          Search through the collector for the first occurrence of the specified byte
 int indexOf(byte[] ba)
          Search through the collector for the first occurrence of the sequence of bytes
 int indexOf(ByteCollector bc)
          Search through the collector for the first occurrence of the sequence of bytes contained within the specified collector
 boolean isEmpty()
          Does the collector have meaningful data or is it empty?
 byte removeFirst()
          Remove the first byte from the collector, shifting its contents accordingly.
 byte[] startToByteArray(int maxlen)
          Pull off the first $maxlen bytes from the collector, shifting the remaining bytes into the beginning of the collector's array.
 byte[] toByteArray()
          Copy the contents of the collector into a new array and return it
 java.lang.String toString()
          Render the bytes as a string
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

contents

byte[] contents

size

int size
Constructor Detail

ByteCollector

public ByteCollector()
New collector with the default initial capacity


ByteCollector

public ByteCollector(int capacity)
New collector with an initial capacity as specified


ByteCollector

public ByteCollector(byte[] b)
New collector containing the specified bytes


ByteCollector

public ByteCollector(byte b)
New collector with the specified byte

Method Detail

append

public ByteCollector append(byte b)
Add a new byte to the collector (extending the buffer if necessary)

Parameters:
b - byte to add
Returns:
this object

append

public ByteCollector append(byte[] b)
Add new bytes to the collector (extending the buffer if necessary)

Parameters:
b - bytes to add
Returns:
this object

append

public ByteCollector append(byte[] b,
                            int len)
Add new bytes to the collector (extending the buffer if necessary)

Parameters:
b - byte array to add from
len - number of bytes in the array to add
Returns:
this object

append

public ByteCollector append(byte[] b,
                            int off,
                            int len)
Add new bytes to the collector (extending the buffer if necessary)

Parameters:
b - byte array to add from
off - offset into the array to begin adding from
len - number of bytes in the array to add
Returns:
this object

append

public ByteCollector append(ByteCollector bc)
Add the contents of the byte collector to the current collector (extending the buffer if necessary)

Parameters:
bc - collector to copy
Returns:
this object

toByteArray

public byte[] toByteArray()
Copy the contents of the collector into a new array and return it

Returns:
new array containing a copy of the current collector's data

startToByteArray

public byte[] startToByteArray(int maxlen)
Pull off the first $maxlen bytes from the collector, shifting the remaining bytes into the beginning of the collector's array.

Parameters:
maxlen - max number of bytes we want to pull from the collector (we will get less if the collector doesnt have that many bytes yet)
Returns:
copy of the bytes pulled from the collector

getCurrentSize

public int getCurrentSize()
How many bytes are available for retrieval?

Returns:
number of bytes

ensureCapacity

public boolean ensureCapacity(int cap)
Make sure we have sufficient storage space.

Parameters:
cap - minimum number of bytes that the buffer should contain
Returns:
true if the the collector was expanded to meet the minimum, false if it was already large enough

isEmpty

public boolean isEmpty()
Does the collector have meaningful data or is it empty?

Returns:
true if it has no data

indexOf

public int indexOf(ByteCollector bc)
Search through the collector for the first occurrence of the sequence of bytes contained within the specified collector

Parameters:
bc - bytes that will be searched for
Returns:
index into the current collector, or -1 if it isn't found

indexOf

public int indexOf(byte b)
Search through the collector for the first occurrence of the specified byte

Parameters:
b - byte that will be searched for
Returns:
index into the current collector, or -1 if it isn't found

indexOf

public int indexOf(byte[] ba)
Search through the collector for the first occurrence of the sequence of bytes

Parameters:
ba - bytes that will be searched for
Returns:
index into the current collector, or -1 if it isn't found

clear

public void clear()
Empty the collector. This does not affect its capacity.


clearAndShorten

public void clearAndShorten()
Empty the collector and reduce its capacity.


toString

public java.lang.String toString()
Render the bytes as a string

Overrides:
toString in class java.lang.Object
Returns:
the, uh, string

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Compare the collectors.

Overrides:
equals in class java.lang.Object
Returns:
true if and only if both are the same size and the byte arrays they contain are equal.

removeFirst

public byte removeFirst()
                 throws java.lang.IllegalArgumentException
Remove the first byte from the collector, shifting its contents accordingly.

Returns:
byte removed
Throws:
java.lang.IllegalArgumentException - if the collector is empty