3 import java.io.IOException;
7 * An implementation of {@code OutputStream} that should serve as the
8 * underlying stream for classes to be tested.
9 * In particular this implementation allows to have IOExecptions thrown on demand.
10 * For simplicity of use and understanding all fields are public.
12 public class Support_ASimpleWriter extends Writer {
14 public static final int DEFAULT_BUFFER_SIZE = 32;
22 // Set to true when exception is wanted:
23 public boolean throwExceptionOnNextUse = false;
25 public Support_ASimpleWriter() {
26 this(DEFAULT_BUFFER_SIZE);
29 public Support_ASimpleWriter(boolean throwException) {
30 this(DEFAULT_BUFFER_SIZE);
31 throwExceptionOnNextUse = throwException;
34 public Support_ASimpleWriter(int bufferSize) {
35 buf = new char[bufferSize];
41 public void close() throws IOException {
42 if (throwExceptionOnNextUse) {
43 throw new IOException("Exception thrown for testing purpose.");
48 public void flush() throws IOException {
49 if (throwExceptionOnNextUse) {
50 throw new IOException("Exception thrown for testing purpose.");
55 public void write(char[] src, int offset, int count) throws IOException {
56 if (throwExceptionOnNextUse) {
57 throw new IOException("Exception thrown for testing purpose.");
59 if (offset < 0 || count < 0 || (offset + count) > buf.length) {
60 throw new IndexOutOfBoundsException();
63 System.arraycopy(src, offset, buf, pos, count);
65 } catch (IndexOutOfBoundsException e) {
67 throw new IOException("Internal Buffer Overflow");
71 public byte[] toByteArray() {
72 byte[] toReturn = new byte[pos];
73 System.arraycopy(buf, 0, toReturn, 0, pos);
77 public String toString() {
78 return new String(buf, 0, pos);