Home | History | Annotate | Download | only in support
      1 package tests.support;
      2 
      3 import java.io.IOException;
      4 import java.io.OutputStream;
      5 
      6 /**
      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.
     11  */
     12 public class Support_ASimpleOutputStream extends OutputStream {
     13 
     14     public static final int DEFAULT_BUFFER_SIZE = 32;
     15 
     16     public byte[] buf;
     17 
     18     public int pos;
     19 
     20     public int size;
     21 
     22     // Set to true when exception is wanted:
     23     public boolean throwExceptionOnNextUse = false;
     24 
     25     public Support_ASimpleOutputStream() {
     26         this(DEFAULT_BUFFER_SIZE);
     27     }
     28 
     29     public Support_ASimpleOutputStream(boolean throwException) {
     30         this(DEFAULT_BUFFER_SIZE);
     31         throwExceptionOnNextUse = throwException;
     32     }
     33 
     34     public Support_ASimpleOutputStream(int bufferSize) {
     35         buf = new byte[bufferSize];
     36         pos = 0;
     37         size = bufferSize;
     38     }
     39 
     40     @Override
     41     public void close() throws IOException {
     42         if (throwExceptionOnNextUse) {
     43             throw new IOException("Exception thrown for testing purpose.");
     44         }
     45     }
     46 
     47     @Override
     48     public void flush() throws IOException {
     49         if (throwExceptionOnNextUse) {
     50             throw new IOException("Exception thrown for testing purpose.");
     51         }
     52     }
     53 
     54 //    @Override
     55 //    public void write(byte buffer[]) throws IOException {
     56 //        if (throwExceptionOnNextUse) {
     57 //            throw new IOException("Exception thrown for testing purposes.");
     58 //        }
     59 //        for (int i = 0; i < buffer.length; i++) {
     60 //            write(buffer[i]);
     61 //        }
     62 //    }
     63 //
     64 //    @Override
     65 //    public void write(byte buffer[], int offset, int count) throws IOException {
     66 //        if (throwExceptionOnNextUse) {
     67 //            throw new IOException("Exception thrown for testing purposes.");
     68 //        }
     69 //        if (offset < 0 || count < 0 || (offset + count) > buffer.length) {
     70 //            throw new IndexOutOfBoundsException();
     71 //        }
     72 //        for (int i = offset; i < offset + count; i++) {
     73 //            write(buffer[i]);
     74 //        }
     75 //    }
     76 
     77     @Override
     78     public void write(int oneByte) throws IOException {
     79         if (throwExceptionOnNextUse) {
     80             throw new IOException("Exception thrown for testing purpose.");
     81         }
     82         if (pos < size) {
     83             buf[pos] = (byte)(oneByte & 255);
     84             pos++;
     85         } else {
     86             throw new IOException("Internal buffer overflow.");
     87         }
     88     }
     89 
     90     public byte[] toByteArray() {
     91         byte[] toReturn = new byte[pos];
     92         System.arraycopy(buf, 0, toReturn, 0, pos);
     93         return toReturn;
     94     }
     95 
     96     public String toString() {
     97         return new String(buf, 0, pos);
     98     }
     99 }
    100