Class UTF8StreamWriter

java.lang.Object
java.io.Writer
org.ka2ddo.util.UTF8StreamWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class UTF8StreamWriter extends Writer
This class implements a memory-optimized optimized writer-to-stream conversion assuming only UTF-8. It is recommended that a NonshareableBufferedOutputStream be used below this class due to the large number of calls to the encapsulated OutputStream's write(int) method.
Author:
Andrew Pavlin, KA2DDO
  • Constructor Details

    • UTF8StreamWriter

      public UTF8StreamWriter(OutputStream out)
      Create a UTF8StreamWriter around the specified OutputStream.
      Parameters:
      out - OutputStream to feed UTF-8 encoded data
  • Method Details

    • write

      public void write(int c) throws IOException
      Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
      Overrides:
      write in class Writer
      Parameters:
      c - int specifying a character to be written
      Throws:
      IOException - If an I/O error occurs
    • append

      public Writer append(char c) throws IOException
      Appends the specified character to this writer.

      An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

           out.write(c) 
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Parameters:
      c - The 16-bit character to append
      Returns:
      This writer
      Throws:
      IOException - If an I/O error occurs
      Since:
      1.5
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Writes a portion of an array of characters.
      Specified by:
      write in class Writer
      Parameters:
      cbuf - Array of characters
      off - Offset from which to start writing characters
      len - Number of characters to write
      Throws:
      IOException - If an I/O error occurs
    • write

      public void write(String str, int off, int len) throws IOException
      Writes a portion of a string.
      Overrides:
      write in class Writer
      Parameters:
      str - A String
      off - Offset from which to start writing characters
      len - Number of characters to write
      Throws:
      IndexOutOfBoundsException - If off is negative, or len is negative, or off+len is negative or greater than the length of the given string
      IOException - If an I/O error occurs
    • flush

      public void flush() throws IOException
      Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException - If an I/O error occurs
    • close

      public void close() throws IOException
      Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException - If an I/O error occurs