Package org.fife.io
Class UnicodeWriter
- java.lang.Object
-
- java.io.Writer
-
- org.fife.io.UnicodeWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.Appendable,java.lang.AutoCloseable
public class UnicodeWriter extends java.io.WriterWrites 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 likeOutputStreamWriter. This class is here because Java'sOutputStreamWriterapparently doesn't believe in writing BOMs.For optimum performance, it is recommended that you wrap all instances of
UnicodeWriterwith ajava.io.BufferedWriter.- Version:
- 0.8
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.OutputStreamWriterinternalOutThe writer actually doing the writing.static java.lang.StringPROPERTY_WRITE_UTF8_BOMIf this system property evaluates to "true", ignoring case, files written out as UTF-8 will have a BOM written for them.private static byte[]UTF16BE_BOMprivate static byte[]UTF16LE_BOMprivate static byte[]UTF32BE_BOMprivate static byte[]UTF32LE_BOMprivate static byte[]UTF8_BOM
-
Constructor Summary
Constructors Constructor Description UnicodeWriter(java.io.File file, java.lang.String encoding)This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.UnicodeWriter(java.io.File file, java.nio.charset.Charset charset)This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.UnicodeWriter(java.io.OutputStream out, java.lang.String encoding)Creates a new writer.UnicodeWriter(java.io.OutputStream out, java.nio.charset.Charset charset)Creates a new writer.UnicodeWriter(java.lang.String fileName, java.lang.String encoding)This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.UnicodeWriter(java.lang.String fileName, java.nio.charset.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this writer.voidflush()Flushes the stream.java.lang.StringgetEncoding()Returns the encoding being used to write this output stream (i.e., the encoding of the file).static booleangetWriteUtf8BOM()Returns whether UTF-8 files should have a BOM in them when written.private voidinit(java.io.OutputStream out, java.lang.String encoding)Initializes the internal output stream and writes the BOM if the specified encoding is a Unicode encoding.static voidsetWriteUtf8BOM(boolean write)Sets whether UTF-8 files should have a BOM written in them.voidwrite(char[] cbuf, int off, int len)Writes a portion of an array of characters.voidwrite(int c)Writes a single character.voidwrite(java.lang.String str, int off, int len)Writes a portion of a string.
-
-
-
Field Detail
-
PROPERTY_WRITE_UTF8_BOM
public static final java.lang.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:
- Constant Field Values
-
internalOut
private java.io.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 Detail
-
UnicodeWriter
public UnicodeWriter(java.lang.String fileName, java.nio.charset.Charset charset) throws java.io.IOExceptionThis 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:
java.io.IOException- If an IO exception occurs.- See Also:
StandardCharsets
-
UnicodeWriter
public UnicodeWriter(java.lang.String fileName, java.lang.String encoding) throws java.io.IOExceptionThis 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:
java.io.IOException- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOExceptionThis 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:
java.io.IOException- If an IO exception occurs.- See Also:
StandardCharsets
-
UnicodeWriter
public UnicodeWriter(java.io.File file, java.lang.String encoding) throws java.io.IOExceptionThis 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:
java.io.IOException- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(java.io.OutputStream out, java.nio.charset.Charset charset) throws java.io.IOExceptionCreates a new writer.- Parameters:
out- The output stream to write.charset- The character set to use.- Throws:
java.io.IOException- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(java.io.OutputStream out, java.lang.String encoding) throws java.io.IOExceptionCreates a new writer.- Parameters:
out- The output stream to write.encoding- The encoding to use.- Throws:
java.io.IOException- If an IO exception occurs.
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses this writer.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Writer- Throws:
java.io.IOException- If an IO exception occurs.
-
flush
public void flush() throws java.io.IOExceptionFlushes the stream.- Specified by:
flushin interfacejava.io.Flushable- Specified by:
flushin classjava.io.Writer- Throws:
java.io.IOException- If an IO exception occurs.
-
getEncoding
public java.lang.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:
setWriteUtf8BOM(boolean),UnicodeWriter
-
init
private void init(java.io.OutputStream out, java.lang.String encoding) throws java.io.IOExceptionInitializes 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:
java.io.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:
getWriteUtf8BOM(),UnicodeWriter
-
write
public void write(char[] cbuf, int off, int len) throws java.io.IOExceptionWrites a portion of an array of characters.- Specified by:
writein classjava.io.Writer- Parameters:
cbuf- The buffer of characters.off- The offset from which to start writing characters.len- The number of characters to write.- Throws:
java.io.IOException- If an I/O error occurs.
-
write
public void write(int c) throws java.io.IOExceptionWrites a single character.- Overrides:
writein classjava.io.Writer- Parameters:
c- An integer specifying the character to write.- Throws:
java.io.IOException- If an IO error occurs.
-
write
public void write(java.lang.String str, int off, int len) throws java.io.IOExceptionWrites a portion of a string.- Overrides:
writein classjava.io.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:
java.io.IOException- If an IO error occurs.
-
-