- Used multiple chunk to compose a buffer.
ie:
public static class Chunk
{
public byte[] mArray;
public int mLength;
public Chunk(int length)
{
mArray = new byte[length];
mLength = 0;
}
}
class ByteArrayBuilder
{
private LinkedListmChunks;
public ByteArrayBuilder()
{
mChunks = new LinkedList();
}
public synchronized void append(byte[] array, int offset, int length)
{
while (length > 0)
{
Chunk c = null;
if (mChunks.isEmpty())
{
c = new Chunk(length);
mChunks.addLast(c);
}
else
{
c = mChunks.getLast();
if (c.mLength == c.mArray.length)
{
c = new Chunk(length);
mChunks.addLast(c);
}
}
int amount = Math.min(length, c.mArray.length - c.mLength);
System.arraycopy(array, offset, c.mArray, c.mLength, amount);
c.mLength += amount;
length -= amount;
offset += amount;
}
} - xxx