1package tests.support;
2
3import java.io.IOException;
4import java.io.OutputStream;
5
6/**
7 * An implementation of {@code OutputStream} that stores the written data in a
8 * byte array of fixed size. As a special feature, instances of this class can
9 * be instructed to throw an {@code IOException} whenever a method is called.
10 * This is used to test the {@code IOException} handling of classes that write
11 * to an {@code OutputStream}.
12 */
13public class Support_OutputStream extends OutputStream {
14
15    private static final int DEFAULT_BUFFER_SIZE = 32;
16
17    private byte[] buffer;
18
19    private int position;
20
21    private int size;
22
23    private boolean throwsException;
24
25    public Support_OutputStream() {
26        this(DEFAULT_BUFFER_SIZE);
27    }
28
29    public Support_OutputStream(boolean throwException) {
30        this(DEFAULT_BUFFER_SIZE);
31        throwsException = throwException;
32    }
33
34    public Support_OutputStream(int bufferSize) {
35        buffer = new byte[bufferSize];
36        position = 0;
37        size = bufferSize;
38        throwsException = false;
39    }
40
41    @Override
42    public void close() throws IOException {
43        if (throwsException) {
44            throw new IOException("Exception thrown for testing purposes.");
45        }
46        super.close();
47    }
48
49    @Override
50    public void flush() throws IOException {
51        if (throwsException) {
52            throw new IOException("Exception thrown for testing purposes.");
53        }
54        super.flush();
55    }
56
57    @Override
58    public void write(byte buffer[]) throws IOException {
59        if (throwsException) {
60            throw new IOException("Exception thrown for testing purposes.");
61        }
62        for (int i = 0; i < buffer.length; i++) {
63            write(buffer[i]);
64        }
65    }
66
67    @Override
68    public void write(byte buffer[], int offset, int count) throws IOException {
69        if (throwsException) {
70            throw new IOException("Exception thrown for testing purposes.");
71        }
72        if (offset < 0 || count < 0 || (offset + count) > buffer.length) {
73            throw new IndexOutOfBoundsException();
74        }
75        for (int i = offset; i < offset + count; i++) {
76            write(buffer[i]);
77        }
78    }
79
80    @Override
81    public void write(int oneByte) throws IOException {
82        if (throwsException) {
83            throw new IOException("Exception thrown for testing purposes.");
84        }
85        if (position < size) {
86            buffer[position] = (byte)(oneByte & 255);
87            position++;
88        } else {
89            throw new IOException("Internal buffer overflow.");
90        }
91    }
92
93    public byte[] toByteArray() {
94        byte[] toReturn = new byte[position];
95        System.arraycopy(buffer, 0, toReturn, 0, position);
96        return toReturn;
97    }
98
99    public String toString() {
100        return new String(buffer, 0, position);
101    }
102
103    public int size() {
104        return position;
105    }
106
107    public void setThrowsException(boolean newValue) {
108        throwsException = newValue;
109    }
110}
111