Class WriterStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class WriterStream
    extends java.io.OutputStream
    Note: this class should be JDK 1.1 compatible
    • Constructor Summary

      Constructors 
      Constructor Description
      WriterStream​(java.io.Writer w)
      Create a stream that writes to a writer.
      WriterStream​(java.io.Writer w, java.lang.String charsetName)
      Create a stream that writes to a writer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      We override default implementation to write last characters, which could be lost in buffer otherwise.
      void flush()
      Flush the stream.
      void write​(int b)
      OutputStream's implementation.
      • Methods inherited from class java.io.OutputStream

        nullOutputStream, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WriterStream

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

        public WriterStream​(java.io.Writer w,
                            java.lang.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 Detail

      • flush

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

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

        public void write​(int b)
                   throws java.io.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 java.io.OutputStream
        Parameters:
        b - next byte from OutputStream to write.
        Throws:
        java.io.IOException