Package org.fife.io

Class UnicodeWriter

java.lang.Object
java.io.Writer
org.fife.io.UnicodeWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class UnicodeWriter extends Writer
Writes Unicode text to an output stream. If the specified encoding is UTF-16 or UTF-32, then the text is preceded by the proper Unicode BOM. If it is any other encoding, this class behaves just like OutputStreamWriter. This class is here because Java's OutputStreamWriter apparently doesn't believe in writing BOMs.

For optimum performance, it is recommended that you wrap all instances of UnicodeWriter with a java.io.BufferedWriter.

Version:
0.8
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The writer actually doing the writing.
    static final String
    If this system property evaluates to "true", ignoring case, files written out as UTF-8 will have a BOM written for them.
    private static final byte[]
     
    private static final byte[]
     
    private static final byte[]
     
    private static final byte[]
     
    private static final byte[]
     

    Fields inherited from class java.io.Writer

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
    UnicodeWriter(File file, String encoding)
    This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
    UnicodeWriter(File file, Charset charset)
    This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
    Creates a new writer.
    Creates a new writer.
    UnicodeWriter(String fileName, String encoding)
    This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
    UnicodeWriter(String fileName, Charset charset)
    This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this writer.
    void
    Flushes the stream.
    Returns the encoding being used to write this output stream (i.e., the encoding of the file).
    static boolean
    Returns whether UTF-8 files should have a BOM in them when written.
    private void
    init(OutputStream out, String encoding)
    Initializes the internal output stream and writes the BOM if the specified encoding is a Unicode encoding.
    static void
    setWriteUtf8BOM(boolean write)
    Sets whether UTF-8 files should have a BOM written in them.
    void
    write(char[] cbuf, int off, int len)
    Writes a portion of an array of characters.
    void
    write(int c)
    Writes a single character.
    void
    write(String str, int off, int len)
    Writes a portion of a string.

    Methods inherited from class java.io.Writer

    append, append, append, nullWriter, write, write

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PROPERTY_WRITE_UTF8_BOM

      public static final String PROPERTY_WRITE_UTF8_BOM
      If this system property evaluates to "true", ignoring case, files written out as UTF-8 will have a BOM written for them. Otherwise, (even if the property is not set), UTF-8 files will not have a BOM written (which is typical, older builds of Windows Notepad are the outlier here).
      See Also:
    • internalOut

      private OutputStreamWriter internalOut
      The writer actually doing the writing.
    • UTF8_BOM

      private static final byte[] UTF8_BOM
    • UTF16LE_BOM

      private static final byte[] UTF16LE_BOM
    • UTF16BE_BOM

      private static final byte[] UTF16BE_BOM
    • UTF32LE_BOM

      private static final byte[] UTF32LE_BOM
    • UTF32BE_BOM

      private static final byte[] UTF32BE_BOM
  • Constructor Details

    • UnicodeWriter

      public UnicodeWriter(String fileName, Charset charset) throws IOException
      This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
      Parameters:
      fileName - The file to which to write.
      charset - The character set to use.
      Throws:
      IOException - If an IO exception occurs.
      See Also:
    • UnicodeWriter

      public UnicodeWriter(String fileName, String encoding) throws IOException
      This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
      Parameters:
      fileName - The file to which to write.
      encoding - The encoding to use.
      Throws:
      IOException - If an IO exception occurs.
    • UnicodeWriter

      public UnicodeWriter(File file, Charset charset) throws IOException
      This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
      Parameters:
      file - The file to which to write.
      charset - The character set to use.
      Throws:
      IOException - If an IO exception occurs.
      See Also:
    • UnicodeWriter

      public UnicodeWriter(File file, String encoding) throws IOException
      This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
      Parameters:
      file - The file to which to write.
      encoding - The encoding to use.
      Throws:
      IOException - If an IO exception occurs.
    • UnicodeWriter

      public UnicodeWriter(OutputStream out, Charset charset) throws IOException
      Creates a new writer.
      Parameters:
      out - The output stream to write.
      charset - The character set to use.
      Throws:
      IOException - If an IO exception occurs.
    • UnicodeWriter

      public UnicodeWriter(OutputStream out, String encoding) throws IOException
      Creates a new writer.
      Parameters:
      out - The output stream to write.
      encoding - The encoding to use.
      Throws:
      IOException - If an IO exception occurs.
  • Method Details

    • close

      public void close() throws IOException
      Closes this writer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException - If an IO exception occurs.
    • flush

      public void flush() throws IOException
      Flushes the stream.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException - If an IO exception occurs.
    • getEncoding

      public String getEncoding()
      Returns the encoding being used to write this output stream (i.e., the encoding of the file).
      Returns:
      The encoding of the stream.
    • getWriteUtf8BOM

      public static boolean getWriteUtf8BOM()
      Returns whether UTF-8 files should have a BOM in them when written.
      Returns:
      Whether to write a BOM for UTF-8 files.
      See Also:
    • init

      private void init(OutputStream out, String encoding) throws IOException
      Initializes the internal output stream and writes the BOM if the specified encoding is a Unicode encoding.
      Parameters:
      out - The output stream we are writing.
      encoding - The encoding in which to write.
      Throws:
      IOException - If an I/O error occurs while writing a BOM.
    • setWriteUtf8BOM

      public static void setWriteUtf8BOM(boolean write)
      Sets whether UTF-8 files should have a BOM written in them.
      Parameters:
      write - Whether to write a BOM.
      See Also:
    • 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 - The buffer of characters.
      off - The offset from which to start writing characters.
      len - The number of characters to write.
      Throws:
      IOException - If an I/O error occurs.
    • write

      public void write(int c) throws IOException
      Writes a single character.
      Overrides:
      write in class Writer
      Parameters:
      c - An integer specifying the character to write.
      Throws:
      IOException - If an IO 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 - The string from which to write.
      off - The offset from which to start writing characters.
      len - The number of characters to write.
      Throws:
      IOException - If an IO error occurs.