Package org.marketcetera.util.unicode
Class UnicodeOutputStreamWriter
- java.lang.Object
-
- java.io.Writer
-
- org.marketcetera.util.unicode.UnicodeOutputStreamWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
- Direct Known Subclasses:
UnicodeFileWriter
public class UnicodeOutputStreamWriter extends Writer
A variation ofOutputStreamWriterthat is BOM-aware. It can operate in any of the following modes:- As a standard output stream writer that uses the default JVM charset.
- A writer that uses a specific charset and (optionally) injects a specific signature in the header of the output stream.
- A writer that uses the signature/charset which a supplied reader had used to decode a byte stream. This is useful when a file is modified, and the original signature/charset must be preserved.
- Since:
- 0.6.0
- Version:
- $Id: UnicodeOutputStreamWriter.java 16154 2012-07-14 16:34:05Z colin $
- Author:
- tlerios@marketcetera.com
-
-
Field Summary
Fields Modifier and Type Field Description private SignatureCharsetmRequestedSignatureCharsetprivate SignatureCharsetmSignatureCharsetprivate OutputStreammStreamprivate OutputStreamWritermWriterprivate booleanmWriteSignature
-
Constructor Summary
Constructors Modifier Constructor Description UnicodeOutputStreamWriter(OutputStream stream)Creates a new writer over the given stream that uses the default JVM charset (and no signature is injected).UnicodeOutputStreamWriter(OutputStream stream, Reader reader)Creates a new writer over the given stream that uses the actual signature/charset of the given reader.protectedUnicodeOutputStreamWriter(OutputStream stream, Reader reader, boolean writeSignature)Creates a new writer over the given stream that uses the actual signature/charset of the given reader.UnicodeOutputStreamWriter(OutputStream stream, SignatureCharset requestedSignatureCharset)Creates a new writer over the given stream that normally injects the given signature, and uses its associated charset for encoding.protectedUnicodeOutputStreamWriter(OutputStream stream, SignatureCharset requestedSignatureCharset, boolean writeSignature)Creates a new writer over the given stream that normally injects the given signature, and uses its associated charset for encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Writerappend(char c)Writerappend(CharSequence csq)Writerappend(CharSequence csq, int start, int end)voidclose()voidflush()SignatureCharsetgetRequestedSignatureCharset()Returns the receiver's requested signature/charset.SignatureCharsetgetSignatureCharset()Returns the receiver's actual signature/charset (that is, the one in use to encode the stream).private voidinit()Initializes the receiver.voidwrite(char[] cbuf)voidwrite(char[] cbuf, int off, int len)voidwrite(int c)voidwrite(String str)voidwrite(String str, int off, int len)-
Methods inherited from class java.io.Writer
nullWriter
-
-
-
-
Field Detail
-
mStream
private OutputStream mStream
-
mWriter
private OutputStreamWriter mWriter
-
mRequestedSignatureCharset
private SignatureCharset mRequestedSignatureCharset
-
mSignatureCharset
private SignatureCharset mSignatureCharset
-
mWriteSignature
private boolean mWriteSignature
-
-
Constructor Detail
-
UnicodeOutputStreamWriter
public UnicodeOutputStreamWriter(OutputStream stream)
Creates a new writer over the given stream that uses the default JVM charset (and no signature is injected).- Parameters:
stream- The stream.
-
UnicodeOutputStreamWriter
protected UnicodeOutputStreamWriter(OutputStream stream, SignatureCharset requestedSignatureCharset, boolean writeSignature)
Creates a new writer over the given stream that normally injects the given signature, and uses its associated charset for encoding. However, if the charset in the given signature/charset pair is not supported by the JVM, the default JVM charset is used instead (and no signature is injected). Signature injection is also skipped if the given flag is set to false (even if the signature/charset pair has a signature).- Parameters:
stream- The stream.requestedSignatureCharset- The signature/charset. It may be null to use the default JVM charset.writeSignature- True if a signature (if any) should be written.
-
UnicodeOutputStreamWriter
public UnicodeOutputStreamWriter(OutputStream stream, SignatureCharset requestedSignatureCharset)
Creates a new writer over the given stream that normally injects the given signature, and uses its associated charset for encoding. However, if the charset in the given signature/charset pair is not supported by the JVM, the default JVM charset is used instead (and no signature is injected).- Parameters:
stream- The stream.requestedSignatureCharset- The signature/charset. It may be null to use the default JVM charset.
-
UnicodeOutputStreamWriter
protected UnicodeOutputStreamWriter(OutputStream stream, Reader reader, boolean writeSignature) throws IOException
Creates a new writer over the given stream that uses the actual signature/charset of the given reader. If the reader is not a valid instance of aUnicodeInputStreamReader, or if it used the default JVM charset, then the writer uses the default JVM charset (and no signature is injected). Signature injection is also skipped if the given flag is set to false (even if the given reader's signature/charset pair has a signature).- Parameters:
stream- The stream.reader- The reader.writeSignature- True if a signature (if any) should be written.- Throws:
IOException- Thrown if an I/O error occurs.
-
UnicodeOutputStreamWriter
public UnicodeOutputStreamWriter(OutputStream stream, Reader reader) throws IOException
Creates a new writer over the given stream that uses the actual signature/charset of the given reader. If the reader is not a valid instance of aUnicodeInputStreamReader, or if it used the default JVM charset, then the writer uses the default JVM charset (and no signature is injected).- Parameters:
stream- The stream.reader- The reader.- Throws:
IOException- Thrown if an I/O error occurs.
-
-
Method Detail
-
write
public void write(int c) throws IOException- Overrides:
writein classWriter- Throws:
IOException
-
write
public void write(char[] cbuf) throws IOException- Overrides:
writein classWriter- Throws:
IOException
-
write
public void write(char[] cbuf, int off, int len) throws IOException- Specified by:
writein classWriter- Throws:
IOException
-
write
public void write(String str) throws IOException
- Overrides:
writein classWriter- Throws:
IOException
-
write
public void write(String str, int off, int len) throws IOException
- Overrides:
writein classWriter- Throws:
IOException
-
append
public Writer append(CharSequence csq) throws IOException
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
append
public Writer append(CharSequence csq, int start, int end) throws IOException
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
append
public Writer append(char c) throws IOException
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException
-
getRequestedSignatureCharset
public SignatureCharset getRequestedSignatureCharset()
Returns the receiver's requested signature/charset. This pair may have either been requested directly or by requesting that a reader's actual signature/charset be matched.- Returns:
- The requested signature/charset, which may be null if none was requested.
-
getSignatureCharset
public SignatureCharset getSignatureCharset() throws IOException
Returns the receiver's actual signature/charset (that is, the one in use to encode the stream).- Returns:
- The signature/charset, which may be null if the default JVM charset is used.
- Throws:
IOException- Thrown if an I/O error occurs.
-
init
private void init() throws IOExceptionInitializes the receiver.- Throws:
IOException- Thrown if an I/O error occurs.
-
-