Class WriterOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
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 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 Writer, implying that
the current position of the Writer is correlated to the current position
of the WriterOutputStream.
WriterOutputStream implements the inverse transformation of 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 InputStreamReader,
except that the control flow is reversed: both classes transform a byte stream
into a character stream, but 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.
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CharsetDecoderprivate final ByteBufferByteBuffer used as input for the decoder.private final CharBufferCharBuffer used as output for the decoder.private static final intprivate final booleanprivate final Writer -
Constructor Summary
ConstructorsConstructorDescriptionWriterOutputStream(Writer writer) Constructs a newWriterOutputStreamthat uses the default character encoding and with a default output buffer size of 1024 characters.WriterOutputStream(Writer writer, String charsetName) Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately) Constructs a newWriterOutputStream.WriterOutputStream(Writer writer, Charset charset) Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(Writer writer, CharsetDecoder decoder) Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately) Constructs a newWriterOutputStream.WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) Constructs a newWriterOutputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the stream.voidflush()Flush the stream.private voidFlush the output.private voidprocessInput(boolean endOfInput) Decode the contents of the input ByteBuffer into a CharBuffer.voidwrite(byte[] b) Write bytes from the specified byte array to the stream.voidwrite(byte[] b, int off, int len) Write bytes from the specified byte array to the stream.voidwrite(int b) Write a single byte to the stream.
-
Field Details
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE- See Also:
-
writer
-
decoder
-
writeImmediately
private final boolean writeImmediately -
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
CharBuffer used as output for the decoder. It should be somewhat larger as we write from this buffer to the underlying Writer.
-
-
Constructor Details
-
WriterOutputStream
Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or whenflush()orclose()is called.- Parameters:
writer- the targetWriterdecoder- the charset decoder- Since:
- 2.1
-
WriterOutputStream
public WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately) Constructs a newWriterOutputStream.- Parameters:
writer- the targetWriterdecoder- 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 underlyingWriterimmediately. If false, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.- Since:
- 2.1
-
WriterOutputStream
Constructs a newWriterOutputStream.- Parameters:
writer- the targetWritercharset- 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 underlyingWriterimmediately. If false, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.
-
WriterOutputStream
Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or whenflush()orclose()is called.- Parameters:
writer- the targetWritercharset- the charset encoding
-
WriterOutputStream
public WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately) Constructs a newWriterOutputStream.- Parameters:
writer- the targetWritercharsetName- 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 underlyingWriterimmediately. If false, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.
-
WriterOutputStream
Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters. The output buffer will only be flushed when it overflows or whenflush()orclose()is called.- Parameters:
writer- the targetWritercharsetName- the name of the charset encoding
-
WriterOutputStream
Constructs a newWriterOutputStreamthat 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 whenflush()orclose()is called.- Parameters:
writer- the targetWriter
-
-
Method Details
-
write
Write bytes from the specified byte array to the stream.- Overrides:
writein classOutputStream- Parameters:
b- the byte array containing the bytes to writeoff- the start offset in the byte arraylen- the number of bytes to write- Throws:
IOException- if an I/O error occurs
-
write
Write bytes from the specified byte array to the stream.- Overrides:
writein classOutputStream- Parameters:
b- the byte array containing the bytes to write- Throws:
IOException- if an I/O error occurs
-
write
Write a single byte to the stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to write- Throws:
IOException- if an I/O error occurs
-
flush
Flush the stream. Any remaining content accumulated in the output buffer will be written to the underlyingWriter. After thatWriter.flush()will be called.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an I/O error occurs
-
close
Close the stream. Any remaining content accumulated in the output buffer will be written to the underlyingWriter. After thatWriter.close()will be called.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an I/O error occurs
-
processInput
Decode the contents of the input ByteBuffer into a CharBuffer.- Parameters:
endOfInput- indicates end of input- Throws:
IOException- if an I/O error occurs
-
flushOutput
Flush the output.- Throws:
IOException- if an I/O error occurs
-