Package org.fife.io
Class UnicodeWriter
java.lang.Object
java.io.Writer
org.fife.io.UnicodeWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
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
FieldsModifier and TypeFieldDescriptionprivate OutputStreamWriter
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[]
-
Constructor Summary
ConstructorsConstructorDescriptionUnicodeWriter
(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.UnicodeWriter
(OutputStream out, String encoding) Creates a new writer.UnicodeWriter
(OutputStream out, Charset charset) 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 TypeMethodDescriptionvoid
close()
Closes this writer.void
flush()
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
Writes a portion of a string.
-
Field Details
-
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
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
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
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
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
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
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
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
Closes this writer.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
- If an IO exception occurs.
-
flush
Flushes the stream.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classWriter
- Throws:
IOException
- If an IO exception occurs.
-
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
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
Writes a portion of an array of characters.- Specified by:
write
in classWriter
- 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
Writes a single character.- Overrides:
write
in classWriter
- Parameters:
c
- An integer specifying the character to write.- Throws:
IOException
- If an IO error occurs.
-
write
Writes a portion of a string.- Overrides:
write
in classWriter
- 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.
-