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.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 likeOutputStreamWriter
. This class is here because Java'sOutputStreamWriter
apparently doesn't believe in writing BOMs.For optimum performance, it is recommended that you wrap all instances of
UnicodeWriter
with ajava.io.BufferedWriter
.- Version:
- 0.8
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.OutputStreamWriter
internalOut
The writer actually doing the writing.static 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.private static byte[]
UTF16BE_BOM
private static byte[]
UTF16LE_BOM
private static byte[]
UTF32BE_BOM
private static byte[]
UTF32LE_BOM
private 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 void
close()
Closes this writer.void
flush()
Flushes the stream.java.lang.String
getEncoding()
Returns the encoding being used to write this output stream (i.e., the encoding of the file).static boolean
getWriteUtf8BOM()
Returns whether UTF-8 files should have a BOM in them when written.private void
init(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 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(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.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:
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.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:
java.io.IOException
- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(java.io.File file, java.nio.charset.Charset charset) throws java.io.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:
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.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:
java.io.IOException
- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(java.io.OutputStream out, java.nio.charset.Charset charset) throws java.io.IOException
Creates 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.IOException
Creates 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.IOException
Closes this writer.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
- Throws:
java.io.IOException
- If an IO exception occurs.
-
flush
public void flush() throws java.io.IOException
Flushes the stream.- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in 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.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:
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.IOException
Writes a portion of an array of characters.- Specified by:
write
in 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.IOException
Writes a single character.- Overrides:
write
in 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.IOException
Writes a portion of a string.- Overrides:
write
in 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.
-
-