Class WriterStream

java.lang.Object
java.io.OutputStream
com.sun.javatest.util.WriterStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class WriterStream extends OutputStream
Note: this class should be JDK 1.1 compatible
  • Constructor Details

    • WriterStream

      public WriterStream(Writer w)
      Create a stream that writes to a writer.
      Parameters:
      w - the writer
    • WriterStream

      public WriterStream(Writer w, String charsetName)
      Create a stream that writes to a writer.
      Parameters:
      w - the writer
      charsetName - name of encoding to be used when decode byte stream (instead of default one)
  • Method Details

    • flush

      public void flush() throws IOException
      Flush the stream.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - if an I/O error occurs
    • close

      public void close() throws IOException
      We override default implementation to write last characters, which could be lost in buffer otherwise.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • write

      public void write(int b) throws IOException
      OutputStream's implementation. Our goal is to convert encoded byte stream form OutputStream to char stream, which can be written using specified writer. We perform this by collecting bytes in buffer and checking with PERIOD_SIZE interval if this buffer represents some char sequence. If so, we write to writer all chars, except last one. We don't write last char, because there still not enough bytes could be collected for it.
      Specified by:
      write in class OutputStream
      Parameters:
      b - next byte from OutputStream to write.
      Throws:
      IOException