OutputStream implementation that transforms a byte stream to a
character stream using a specified charset encoding and writes the resulting
stream to a Writer. The stream is transformed using a
CharsetDecoder object, guaranteeing that all charset
encodings supported by the JRE are handled correctly.
The output of the CharsetDecoder is buffered using a fixed size buffer.
This implies that the data is written to the underlying 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 Writer
in a java.. 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 Writer, implying that
the current position of the Writer is correlated to the current position
of the WriterOutputStream.
WriterOutputStream implements the inverse transformation of java.;
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 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.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private final CharsetDecoder | |
| private final ByteBuffer | decoderIn
ByteBuffer used as input for the decoder. |
| private final CharBuffer | decoderOut
CharBuffer used as output for the decoder. |
| private final boolean | |
| private final Writer |
| Access | Constructor and Description |
|---|---|
| public | WriterOutputStream(final Writer
the target writer)Writer
Deprecated
2.5 use
WriterOutputStream(Writer, Charset) instead
Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final Charset Writerthe charset encoding charset)Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final Charset Writerthe charset encoding charset, final int the size of the output buffer in number of characters bufferSize, final boolean writeImmediately)Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final CharsetDecoder Writerthe charset decoder decoder)Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final CharsetDecoder Writerthe charset decoder decoder, final int the size of the output buffer in number of characters bufferSize, final boolean writeImmediately)Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final String Writerthe name of the charset encoding charsetName)Constructs a new |
| public | WriterOutputStream(final Writer
the target writer, final String Writerthe name of the charset encoding charsetName, final int the size of the output buffer in number of characters bufferSize, final boolean writeImmediately)Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| private void | |
| private void | processInput(final boolean
indicates end of input endOfInput)Decode the contents of the input ByteBuffer into a CharBuffer. |
| public void | write(final byte[]
the byte array containing the bytes to write b)Overrides java. Write bytes from the specified byte array to the stream. |
| public void | write(final byte[]
the byte array containing the bytes to write b, int the start offset in the byte array off, int the number of bytes to write len)Overrides java. Write bytes from the specified byte array to the stream. |
| public void | write(final int
the byte to write b)Implements abstract java. Write a single byte to the stream. |
| BUFFER_SIZE | back to summary |
|---|---|
| private static final int BUFFER_SIZE | |
| decoder | back to summary |
|---|---|
| private final CharsetDecoder decoder | |
| decoderIn | back to summary |
|---|---|
| private final ByteBuffer decoderIn ByteBuffer used as input for the decoder. This buffer can be small as it is used only to transfer the received data to the decoder. | |
| decoderOut | back to summary |
|---|---|
| private final CharBuffer decoderOut CharBuffer used as output for the decoder. It should be somewhat larger as we write from this buffer to the underlying Writer. | |
| writeImmediately | back to summary |
|---|---|
| private final boolean writeImmediately | |
| writer | back to summary |
|---|---|
| private final Writer writer | |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer)
Deprecated 2.5 use Constructs a new
| |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final Charset charset) Constructs a new | |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize, final boolean writeImmediately) Constructs a new
| |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) Constructs a new
| |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, final int bufferSize, final boolean writeImmediately) Constructs a new
| |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final String charsetName) Constructs a new | |
| WriterOutputStream | back to summary |
|---|---|
| public WriterOutputStream(final Writer writer, final String charsetName, final int bufferSize, final boolean writeImmediately) Constructs a new
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Close the stream. Any remaining content accumulated in the output buffer
will be written to the underlying
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Overrides java. Implements java. Flush the stream. Any remaining content accumulated in the output buffer
will be written to the underlying
| |
| flushOutput | back to summary |
|---|---|
| private void flushOutput() throws IOException Flush the output.
| |
| processInput | back to summary |
|---|---|
| private void processInput(final boolean endOfInput) throws IOException Decode the contents of the input ByteBuffer into a CharBuffer.
| |
| write | back to summary |
|---|---|
| public void write(final byte[] b) throws IOException Overrides java. Write bytes from the specified byte array to the stream.
| |
| write | back to summary |
|---|---|
| public void write(final byte[] b, int off, int len) throws IOException Overrides java. Write bytes from the specified byte array to the stream.
| |
| write | back to summary |
|---|---|
| public void write(final int b) throws IOException Implements abstract java. Write a single byte to the stream.
| |