FileDocCategorySizeDatePackage
ByteArrayBuilder.javaAPI DocAndroid 1.5 API3806Wed May 06 22:41:56 BST 2009android.webkit

ByteArrayBuilder

public class ByteArrayBuilder extends Object
Utility class optimized for accumulating bytes, and then spitting them back out. It does not optimize for returning the result in a single array, though this is supported in the API. It is fastest if the retrieval can be done via iterating through chunks. Things to add: - consider dynamically increasing our min_capacity, as we see mTotalSize increase

Fields Summary
private static final int
DEFAULT_CAPACITY
private LinkedList
mChunks
private LinkedList
mPool
free pool
private int
mMinCapacity
Constructors Summary
public ByteArrayBuilder()


      
        init(0);
    
public ByteArrayBuilder(int minCapacity)

        init(minCapacity);
    
Methods Summary
public voidappend(byte[] array)

        append(array, 0, array.length);
    
public synchronized voidappend(byte[] array, int offset, int length)

        while (length > 0) {
            Chunk c = appendChunk(length);
            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;
        }
    
private android.webkit.ByteArrayBuilder$ChunkappendChunk(int length)

        if (length < mMinCapacity) {
            length = mMinCapacity;
        }

        Chunk c;
        if (mChunks.isEmpty()) {
            c = obtainChunk(length);
        } else {
            c = mChunks.getLast();
            if (c.mLength == c.mArray.length) {
                c = obtainChunk(length);
            }
        }
        return c;
    
public synchronized voidclear()

        Chunk c = getFirstChunk();
        while (c != null) {
            releaseChunk(c);
            c = getFirstChunk();
        }
    
public synchronized android.webkit.ByteArrayBuilder$ChunkgetFirstChunk()
The fastest way to retrieve the data is to iterate through the chunks. This returns the first chunk. Note: this pulls the chunk out of the queue. The caller must call releaseChunk() to dispose of it.

        if (mChunks.isEmpty()) return null;
        return mChunks.removeFirst();
    
private voidinit(int minCapacity)

        mChunks = new LinkedList<Chunk>();
        mPool = new LinkedList<Chunk>();

        if (minCapacity <= 0) {
            minCapacity = DEFAULT_CAPACITY;
        }
        mMinCapacity = minCapacity;
    
public booleanisEmpty()

        return mChunks.isEmpty();
    
private android.webkit.ByteArrayBuilder$ChunkobtainChunk(int length)

        Chunk c;
        if (mPool.isEmpty()) {
            c = new Chunk(length);
        } else {
            c = mPool.removeFirst();
        }
        mChunks.addLast(c);
        return c;
    
public synchronized voidreleaseChunk(android.webkit.ByteArrayBuilder$Chunk c)
recycles chunk

        c.mLength = 0;
        mPool.addLast(c);