java.io.OutputStream implementation that transforms a byte stream to a character stream using a specified charset encoding and writes the resulting stream to a java.io.Writer. The stream is transformed using a java.nio.charset.CharsetDecoder object, guaranteeing that all charset encodings supported by the JRE are handled correctly.
The output of the java.nio.charset.CharsetDecoder is buffered using a fixed size buffer. This implies that the data is written to the underlying java.io.Writer in chunks that are no larger than the size of this buffer. By default, the buffer is flushed only when it overflows or when flush() or close() is called. In general there is therefore no need to wrap the underlying java.io.Writer in a java.io.BufferedWriter. WriterOutputStream can also be instructed to flush the buffer after each write operation. In this case, all available data is written immediately to the underlying java.io.Writer, implying that the current position of the java.io.Writer is correlated to the current position of the WriterOutputStream.
WriterOutputStream implements the inverse transformation of java.io.OutputStreamWriter; in the following example, writing to out2 would have the same result as writing to out directly (provided that the byte sequence is legal with respect to the charset encoding):
OutputStream out = ... Charset cs = ... OutputStreamWriter writer = new OutputStreamWriter(out, cs); WriterOutputStream out2 = new WriterOutputStream(writer, cs);WriterOutputStream implements the same transformation as java.io.InputStreamReader, except that the control flow is reversed: both classes transform a byte stream into a character stream, but java.io.InputStreamReader pulls data from the underlying stream, while WriterOutputStream pushes it to the underlying stream.
Note that while there are use cases where there is no alternative to using this class, very often the need to use this class is an indication of a flaw in the design of the code. This class is typically used in situations where an existing API only accepts an java.io.OutputStream object, but where the stream is known to represent character data that must be decoded for further use.
Instances of WriterOutputStream are not thread safe.
| Constructor and description |
|---|
WriterOutputStream
(java.io.Writer writer, java.nio.charset.CharsetDecoder decoder)Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. |
WriterOutputStream
(java.io.Writer writer, java.nio.charset.CharsetDecoder decoder, int bufferSize, boolean writeImmediately)Constructs a new WriterOutputStream. |
WriterOutputStream
(java.io.Writer writer, java.nio.charset.Charset charset, int bufferSize, boolean writeImmediately)Constructs a new WriterOutputStream. |
WriterOutputStream
(java.io.Writer writer, java.nio.charset.Charset charset)Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. |
WriterOutputStream
(java.io.Writer writer, java.lang.String charsetName, int bufferSize, boolean writeImmediately)Constructs a new WriterOutputStream. |
WriterOutputStream
(java.io.Writer writer, java.lang.String charsetName)Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. |
WriterOutputStream
(java.io.Writer writer)Constructs a new WriterOutputStream that uses the default character encoding and with a default output buffer size of 1024 characters. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
void |
close()Close the stream. |
|
void |
flush()Flush the stream. |
|
void |
write(byte[] b, int off, int len)Write bytes from the specified byte array to the stream. |
|
void |
write(byte[] b)Write bytes from the specified byte array to the stream. |
|
void |
write(int b)Write a single byte to the stream. |
| Methods inherited from class | Name |
|---|---|
class java.io.OutputStream |
java.io.OutputStream#write([B, int, int), java.io.OutputStream#write([B), java.io.OutputStream#write(int), java.io.OutputStream#flush(), java.io.OutputStream#close(), java.io.OutputStream#wait(long, int), java.io.OutputStream#wait(long), java.io.OutputStream#wait(), java.io.OutputStream#equals(java.lang.Object), java.io.OutputStream#toString(), java.io.OutputStream#hashCode(), java.io.OutputStream#getClass(), java.io.OutputStream#notify(), java.io.OutputStream#notifyAll() |
Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or when flush() or close() is called.
writer - the target java.io.Writerdecoder - the charset decoderConstructs a new WriterOutputStream.
writer - the target java.io.Writerdecoder - the charset decoderbufferSize - the size of the output buffer in number of characterswriteImmediately - If true the output buffer will be flushed after each
write operation, i.e. all available data will be written to the
underlying java.io.Writer immediately. If false, the
output buffer will only be flushed when it overflows or when
flush() or close() is called.Constructs a new WriterOutputStream.
writer - the target java.io.Writercharset - the charset encodingbufferSize - the size of the output buffer in number of characterswriteImmediately - If true the output buffer will be flushed after each
write operation, i.e. all available data will be written to the
underlying java.io.Writer immediately. If false, the
output buffer will only be flushed when it overflows or when
flush() or close() is called.Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or when flush() or close() is called.
writer - the target java.io.Writercharset - the charset encodingConstructs a new WriterOutputStream.
writer - the target java.io.WritercharsetName - the name of the charset encodingbufferSize - the size of the output buffer in number of characterswriteImmediately - If true the output buffer will be flushed after each
write operation, i.e. all available data will be written to the
underlying java.io.Writer immediately. If false, the
output buffer will only be flushed when it overflows or when
flush() or close() is called.Constructs a new WriterOutputStream with a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or when flush() or close() is called.
writer - the target java.io.WritercharsetName - the name of the charset encodingConstructs a new WriterOutputStream that uses the default character encoding and with a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or when flush() or close() is called.
writer - the target java.io.WriterClose the stream. Any remaining content accumulated in the output buffer will be written to the underlying java.io.Writer. After that java.io.Writer#close() will be called.
Flush the stream. Any remaining content accumulated in the output buffer will be written to the underlying java.io.Writer. After that java.io.Writer#flush() will be called.
Write bytes from the specified byte array to the stream.
b - the byte array containing the bytes to writeoff - the start offset in the byte arraylen - the number of bytes to writeWrite bytes from the specified byte array to the stream.
b - the byte array containing the bytes to writeWrite a single byte to the stream.
b - the byte to write