Class WriterOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.io.output.WriterOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class WriterOutputStream extends java.io.OutputStreamOutputStreamimplementation that transforms a byte stream to a character stream using a specified charset encoding and writes the resulting stream to aWriter. The stream is transformed using aCharsetDecoderobject, guaranteeing that all charset encodings supported by the JRE are handled correctly.The output of the
CharsetDecoderis buffered using a fixed size buffer. This implies that the data is written to the underlyingWriterin chunks that are no larger than the size of this buffer. By default, the buffer is flushed only when it overflows or whenflush()orclose()is called. In general there is therefore no need to wrap the underlyingWriterin aBufferedWriter.WriterOutputStreamcan also be instructed to flush the buffer after each write operation. In this case, all available data is written immediately to the underlyingWriter, implying that the current position of theWriteris correlated to the current position of theWriterOutputStream.WriterOutputStreamimplements the inverse transformation ofOutputStreamWriter; in the following example, writing toout2would have the same result as writing tooutdirectly (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);
WriterOutputStreamimplements the same transformation asInputStreamReader, except that the control flow is reversed: both classes transform a byte stream into a character stream, butInputStreamReaderpulls data from the underlying stream, whileWriterOutputStreampushes 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
OutputStreamobject, but where the stream is known to represent character data that must be decoded for further use.Instances of
WriterOutputStreamare not thread safe.- Since:
- 2.0
- See Also:
ReaderInputStream
-
-
Field Summary
Fields Modifier and Type Field Description private static intBUFFER_SIZEprivate java.nio.charset.CharsetDecoderdecoderprivate java.nio.ByteBufferdecoderInByteBuffer used as input for the decoder.private java.nio.CharBufferdecoderOutCharBuffer used as output for the decoder.private booleanwriteImmediatelyprivate java.io.Writerwriter
-
Constructor Summary
Constructors Constructor Description WriterOutputStream(java.io.Writer writer)Deprecated.2.5 useWriterOutputStream(Writer, Charset)insteadWriterOutputStream(java.io.Writer writer, java.lang.String charsetName)Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(java.io.Writer writer, java.lang.String charsetName, int bufferSize, boolean writeImmediately)Constructs a newWriterOutputStream.WriterOutputStream(java.io.Writer writer, java.nio.charset.Charset charset)Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(java.io.Writer writer, java.nio.charset.CharsetDecoder decoder)Constructs a newWriterOutputStreamwith a default output buffer size of 1024 characters.WriterOutputStream(java.io.Writer writer, java.nio.charset.CharsetDecoder decoder, int bufferSize, boolean writeImmediately)Constructs a newWriterOutputStream.WriterOutputStream(java.io.Writer writer, java.nio.charset.Charset charset, int bufferSize, boolean writeImmediately)Constructs a newWriterOutputStream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidcheckIbmJdkWithBrokenUTF16(java.nio.charset.Charset charset)Check if the JDK in use properly supports the given charset.voidclose()Close the stream.voidflush()Flush the stream.private voidflushOutput()Flush 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 Detail
-
BUFFER_SIZE
private static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
writer
private final java.io.Writer writer
-
decoder
private final java.nio.charset.CharsetDecoder decoder
-
writeImmediately
private final boolean writeImmediately
-
decoderIn
private final java.nio.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
private final java.nio.CharBuffer decoderOut
CharBuffer used as output for the decoder. It should be somewhat larger as we write from this buffer to the underlying Writer.
-
-
Constructor Detail
-
WriterOutputStream
public WriterOutputStream(java.io.Writer writer, java.nio.charset.CharsetDecoder decoder)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(java.io.Writer writer, java.nio.charset.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- Iftruethe output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriterimmediately. Iffalse, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.- Since:
- 2.1
-
WriterOutputStream
public WriterOutputStream(java.io.Writer writer, java.nio.charset.Charset charset, int bufferSize, boolean writeImmediately)Constructs a newWriterOutputStream.- Parameters:
writer- the targetWritercharset- the charset encodingbufferSize- the size of the output buffer in number of characterswriteImmediately- Iftruethe output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriterimmediately. Iffalse, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.
-
WriterOutputStream
public WriterOutputStream(java.io.Writer writer, java.nio.charset.Charset charset)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(java.io.Writer writer, java.lang.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- Iftruethe output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriterimmediately. Iffalse, the output buffer will only be flushed when it overflows or whenflush()orclose()is called.
-
WriterOutputStream
public WriterOutputStream(java.io.Writer writer, java.lang.String charsetName)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
@Deprecated public WriterOutputStream(java.io.Writer writer)
Deprecated.2.5 useWriterOutputStream(Writer, Charset)insteadConstructs 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 Detail
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWrite bytes from the specified byte array to the stream.- Overrides:
writein classjava.io.OutputStream- Parameters:
b- the byte array containing the bytes to writeoff- the start offset in the byte arraylen- the number of bytes to write- Throws:
java.io.IOException- if an I/O error occurs
-
write
public void write(byte[] b) throws java.io.IOExceptionWrite bytes from the specified byte array to the stream.- Overrides:
writein classjava.io.OutputStream- Parameters:
b- the byte array containing the bytes to write- Throws:
java.io.IOException- if an I/O error occurs
-
write
public void write(int b) throws java.io.IOExceptionWrite a single byte to the stream.- Specified by:
writein classjava.io.OutputStream- Parameters:
b- the byte to write- Throws:
java.io.IOException- if an I/O error occurs
-
flush
public void flush() throws java.io.IOExceptionFlush 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 interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionClose 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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- if an I/O error occurs
-
processInput
private void processInput(boolean endOfInput) throws java.io.IOExceptionDecode the contents of the input ByteBuffer into a CharBuffer.- Parameters:
endOfInput- indicates end of input- Throws:
java.io.IOException- if an I/O error occurs
-
flushOutput
private void flushOutput() throws java.io.IOExceptionFlush the output.- Throws:
java.io.IOException- if an I/O error occurs
-
checkIbmJdkWithBrokenUTF16
private static void checkIbmJdkWithBrokenUTF16(java.nio.charset.Charset charset)
Check if the JDK in use properly supports the given charset.- Parameters:
charset- the charset to check the support for
-
-