FileDocCategorySizeDatePackage
FastByteArrayOutputStream.javaAPI DocExample6648Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

FastByteArrayOutputStream

public class FastByteArrayOutputStream extends OutputStream
A speedy implementation of ByteArrayOutputStream. It's not synchronized, and it does not copy buffers when it's expanded. There's also no copying of the internal buffer if it's contents is extracted with the writeTo(stream) method.

Fields Summary
private static final int
DEFAULT_BLOCK_SIZE
private LinkedList
buffers
private byte[]
buffer
private boolean
closed
private int
blockSize
private int
index
private int
size
Constructors Summary
public FastByteArrayOutputStream()



    // Constructors --------------------------------------------------
      
        this(DEFAULT_BLOCK_SIZE);
    
public FastByteArrayOutputStream(int aSize)

        blockSize = aSize;
        buffer = new byte[blockSize];
    
Methods Summary
protected voidaddBuffer()
Create a new buffer and store the current one in linked list

        if (buffers == null) {
            buffers = new LinkedList();
        }

        buffers.addLast(buffer);

        buffer = new byte[blockSize];
        size += index;
        index = 0;
    
public voidclose()

        closed = true;
    
public intgetSize()

        return size + index;
    
public byte[]toByteArray()

        byte[] data = new byte[getSize()];

        // Check if we have a list of buffers
        int pos = 0;

        if (buffers != null) {
            Iterator iter = buffers.iterator();

            while (iter.hasNext()) {
                byte[] bytes = (byte[]) iter.next();
                System.arraycopy(bytes, 0, data, pos, blockSize);
                pos += blockSize;
            }
        }

        // write the internal buffer directly
        System.arraycopy(buffer, 0, data, pos, index);

        return data;
    
public java.lang.StringtoString()

        return new String(toByteArray());
    
public voidwrite(int datum)

        if (closed) {
            throw new IOException("Stream closed");
        } else {
            if (index == blockSize) {
                addBuffer();
            }

            // store the byte
            buffer[index++] = (byte) datum;
        }
    
public voidwrite(byte[] data, int offset, int length)

        if (data == null) {
            throw new NullPointerException();
        } else if ((offset < 0) || ((offset + length) > data.length) || (length < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (closed) {
            throw new IOException("Stream closed");
        } else {
            if ((index + length) > blockSize) {
                int copyLength;

                do {
                    if (index == blockSize) {
                        addBuffer();
                    }

                    copyLength = blockSize - index;

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

                    System.arraycopy(data, offset, buffer, index, copyLength);
                    offset += copyLength;
                    index += copyLength;
                    length -= copyLength;
                } while (length > 0);
            } else {
                // Copy in the subarray
                System.arraycopy(data, offset, buffer, index, length);
                index += length;
            }
        }
    
public voidwriteTo(java.io.RandomAccessFile out)

        // Check if we have a list of buffers
        if (buffers != null) {
            Iterator iter = buffers.iterator();

            while (iter.hasNext()) {
                byte[] bytes = (byte[]) iter.next();
                out.write(bytes, 0, blockSize);
            }
        }

        // write the internal buffer directly
        out.write(buffer, 0, index);
    
public voidwriteTo(java.io.Writer out, java.lang.String encoding)

        // Check if we have a list of buffers
        if (buffers != null) {
            Iterator iter = buffers.iterator();

            while (iter.hasNext()) {
                byte[] bytes = (byte[]) iter.next();

                if (encoding != null) {
                    out.write(new String(bytes, encoding));
                } else {
                    out.write(new String(bytes));
                }
            }
        }

        // write the internal buffer directly
        if (encoding != null) {
            out.write(new String(buffer, 0, index, encoding));
        } else {
            out.write(new String(buffer, 0, index));
        }
    
public voidwriteTo(java.io.OutputStream out)

        // Check if we have a list of buffers
        if (buffers != null) {
            Iterator iter = buffers.iterator();

            while (iter.hasNext()) {
                byte[] bytes = (byte[]) iter.next();
                out.write(bytes, 0, blockSize);
            }
        }

        // write the internal buffer directly
        out.write(buffer, 0, index);