FileDocCategorySizeDatePackage
ByteArrayOutputStream.javaAPI DocApache Axis 1.47161Sat Apr 22 18:57:28 BST 2006org.apache.axis.utils

ByteArrayOutputStream

public class ByteArrayOutputStream extends OutputStream
This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.

The data can be retrieved using toByteArray() and toString().

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

This is an alternative implementation of the java.io.ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.

author
Jeremias Maerki

Fields Summary
private List
buffers
private int
currentBufferIndex
private int
filledBufferSum
private byte[]
currentBuffer
private int
count
Constructors Summary
public ByteArrayOutputStream()
Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.


                             
      
        this(1024);
    
public ByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

param
size the initial size.
throws
IllegalArgumentException if size is negative.

        if (size < 0) {
            throw new IllegalArgumentException(
                    Messages.getMessage("illegalArgumentException01",
                            Integer.toString(size)));
        }
        needNewBuffer(size);
    
Methods Summary
public voidclose()
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

throws
IOException in case an I/O error occurs

        //nop
    
private byte[]getBuffer(int index)

        return (byte[]) buffers.get(index);
    
private voidneedNewBuffer(int newcount)

        if (currentBufferIndex < buffers.size() - 1) {
            //Recycling old buffer
            filledBufferSum += currentBuffer.length;
            currentBufferIndex++;
            currentBuffer = getBuffer(currentBufferIndex);
        } else {
            //Creating new buffer
            int newBufferSize;
            if (currentBuffer == null) {
                newBufferSize = newcount;
                filledBufferSum = 0;
            } else {
                newBufferSize = Math.max(currentBuffer.length << 1,
                        newcount - filledBufferSum);
                filledBufferSum += currentBuffer.length;
            }
            currentBufferIndex++;
            currentBuffer = new byte[newBufferSize];
            buffers.add(currentBuffer);
        }
    
public synchronized voidreset()

see
java.io.ByteArrayOutputStream#reset()

        count = 0;
        filledBufferSum = 0;
        currentBufferIndex = 0;
        currentBuffer = getBuffer(currentBufferIndex);
    
public intsize()

see
java.io.ByteArrayOutputStream#size()

        return count;
    
public synchronized byte[]toByteArray()

see
java.io.ByteArrayOutputStream#toByteArray()

        int remaining = count;
        int pos = 0;
        byte[] newbuf = new byte[count];
        for (int i = 0; i < buffers.size(); i++) {
            byte[] buf = getBuffer(i);
            int c = Math.min(buf.length, remaining);
            System.arraycopy(buf, 0, newbuf, pos, c);
            pos += c;
            remaining -= c;
            if (remaining == 0) {
                break;
            }
        }
        return newbuf;
    
public java.lang.StringtoString()

see
java.io.ByteArrayOutputStream#toString()

        return new String(toByteArray());
    
public java.lang.StringtoString(java.lang.String enc)

see
java.io.ByteArrayOutputStream#toString(String)

        return new String(toByteArray(), enc);
    
public synchronized voidwrite(byte[] b, int off, int len)

see
java.io.OutputStream#write(byte[], int, int)

        if ((off < 0)
                || (off > b.length)
                || (len < 0)
                || ((off + len) > b.length)
                || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException(
                    Messages.getMessage("indexOutOfBoundsException00"));
        } else if (len == 0) {
            return;
        }
        int newcount = count + len;
        int remaining = len;
        int inBufferPos = count - filledBufferSum;
        while (remaining > 0) {
            int part = Math.min(remaining, currentBuffer.length - inBufferPos);
            System.arraycopy(b, off + len - remaining, currentBuffer,
                    inBufferPos, part);
            remaining -= part;
            if (remaining > 0) {
                needNewBuffer(newcount);
                inBufferPos = 0;
            }
        }
        count = newcount;
    
public synchronized voidwrite(int b)
Calls the write(byte[]) method.

see
java.io.OutputStream#write(int)

        write(new byte[]{(byte) b}, 0, 1);
    
public synchronized voidwriteTo(java.io.OutputStream out)

see
java.io.ByteArrayOutputStream#writeTo(OutputStream)

        int remaining = count;
        for (int i = 0; i < buffers.size(); i++) {
            byte[] buf = getBuffer(i);
            int c = Math.min(buf.length, remaining);
            out.write(buf, 0, c);
            remaining -= c;
            if (remaining == 0) {
                break;
            }
        }