FileDocCategorySizeDatePackage
Support_OutputStream.javaAPI DocAndroid 1.5 API3047Wed May 06 22:41:06 BST 2009tests.support

Support_OutputStream

public class Support_OutputStream extends OutputStream
An implementation of {@code OutputStream} that stores the written data in a byte array of fixed size. As a special feature, instances of this class can be instructed to throw an {@code IOException} whenever a method is called. This is used to test the {@code IOException} handling of classes that write to an {@code OutputStream}.

Fields Summary
private static final int
DEFAULT_BUFFER_SIZE
private byte[]
buffer
private int
position
private int
size
private boolean
throwsException
Constructors Summary
public Support_OutputStream()


      
        this(DEFAULT_BUFFER_SIZE);
    
public Support_OutputStream(boolean throwException)

        this(DEFAULT_BUFFER_SIZE);
        throwsException = throwException;
    
public Support_OutputStream(int bufferSize)

        buffer = new byte[bufferSize];
        position = 0;
        size = bufferSize;
        throwsException = false;
    
Methods Summary
public voidclose()

        if (throwsException) {
            throw new IOException("Exception thrown for testing purposes.");
        }
        super.close();
    
public voidflush()

        if (throwsException) {
            throw new IOException("Exception thrown for testing purposes.");
        }
        super.flush();
    
public voidsetThrowsException(boolean newValue)

        throwsException = newValue;
    
public intsize()

        return position;
    
public byte[]toByteArray()

        byte[] toReturn = new byte[position];
        System.arraycopy(buffer, 0, toReturn, 0, position);
        return toReturn;
    
public java.lang.StringtoString()

        return new String(buffer, 0, position);
    
public voidwrite(byte[] buffer)

        if (throwsException) {
            throw new IOException("Exception thrown for testing purposes.");
        }
        for (int i = 0; i < buffer.length; i++) {
            write(buffer[i]);
        }
    
public voidwrite(byte[] buffer, int offset, int count)

        if (throwsException) {
            throw new IOException("Exception thrown for testing purposes.");
        }
        if (offset < 0 || count < 0 || (offset + count) > buffer.length) {
            throw new IndexOutOfBoundsException(); //$NON-NLS-1$
        }
        for (int i = offset; i < offset + count; i++) {
            write(buffer[i]);
        }
    
public voidwrite(int oneByte)

        if (throwsException) {
            throw new IOException("Exception thrown for testing purposes.");
        }
        if (position < size) {
            buffer[position] = (byte)(oneByte & 255);
            position++;
        } else {
            throw new IOException("Internal buffer overflow.");
        }