Class WriterOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.io.output.WriterOutputStream
-
- All Implemented Interfaces:
- Closeable, Flushable, AutoCloseable
public class WriterOutputStream extends OutputStream
OutputStream
implementation 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 aCharsetDecoder
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 underlyingWriter
in 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 underlyingWriter
in aBufferedWriter
.WriterOutputStream
can 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 theWriter
is correlated to the current position of theWriterOutputStream
.WriterOutputStream
implements the inverse transformation ofOutputStreamWriter
; in the following example, writing toout2
would have the same result as writing toout
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 asInputStreamReader
, except that the control flow is reversed: both classes transform a byte stream into a character stream, butInputStreamReader
pulls data from the underlying stream, whileWriterOutputStream
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
- See Also:
-
ReaderInputStream
-
Constructor Summary
Constructors Constructor and Description WriterOutputStream(Writer writer)
Deprecated.2.5 useWriterOutputStream(Writer, Charset)
insteadWriterOutputStream(Writer writer, Charset charset)
Constructs a newWriterOutputStream
with a default output buffer size of characters.WriterOutputStream(Writer writer, CharsetDecoder decoder)
Constructs a newWriterOutputStream
with a default output buffer size of 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
.WriterOutputStream(Writer writer, String charsetName)
Constructs a newWriterOutputStream
with a default output buffer size of characters.WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately)
Constructs a newWriterOutputStream
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
close()
Close the stream.void
flush()
Flush the stream.void
write(byte[] b)
Write bytes from the specified byte array to the stream.void
write(byte[] b, int off, int len)
Write bytes from the specified byte array to the stream.void
write(int b)
Write a single byte to the stream.
-
Constructor Detail
WriterOutputStream
public WriterOutputStream(Writer writer, CharsetDecoder decoder)
Constructs a newWriterOutputStream
with a default output buffer size of characters. The output buffer will only be flushed when it overflows or whenflush()
orclose()
is called.- Parameters:
-
writer
- the targetWriter
-
decoder
- the charset decoder - Since:
- 2.1
WriterOutputStream
public WriterOutputStream(Writer writer, CharsetDecoder decoder, int bufferSize, boolean writeImmediately)
Constructs a newWriterOutputStream
.- Parameters:
-
writer
- the targetWriter
-
decoder
- the charset decoder -
bufferSize
- the size of the output buffer in number of characters -
writeImmediately
- Iftrue
the output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriter
immediately. Iffalse
, the output buffer will only be flushed when it overflows or whenflush()
orclose()
is called. - Since:
- 2.1
WriterOutputStream
public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately)
Constructs a newWriterOutputStream
.- Parameters:
-
writer
- the targetWriter
-
charset
- the charset encoding -
bufferSize
- the size of the output buffer in number of characters -
writeImmediately
- Iftrue
the output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriter
immediately. Iffalse
, the output buffer will only be flushed when it overflows or whenflush()
orclose()
is called.
WriterOutputStream
public WriterOutputStream(Writer writer, Charset charset)
Constructs a newWriterOutputStream
with a default output buffer size of characters. The output buffer will only be flushed when it overflows or whenflush()
orclose()
is called.- Parameters:
-
writer
- the targetWriter
-
charset
- the charset encoding
WriterOutputStream
public WriterOutputStream(Writer writer, String charsetName, int bufferSize, boolean writeImmediately)
Constructs a newWriterOutputStream
.- Parameters:
-
writer
- the targetWriter
-
charsetName
- the name of the charset encoding -
bufferSize
- the size of the output buffer in number of characters -
writeImmediately
- Iftrue
the output buffer will be flushed after each write operation, i.e. all available data will be written to the underlyingWriter
immediately. Iffalse
, the output buffer will only be flushed when it overflows or whenflush()
orclose()
is called.
WriterOutputStream
public WriterOutputStream(Writer writer, String charsetName)
Constructs a newWriterOutputStream
with a default output buffer size of characters. The output buffer will only be flushed when it overflows or whenflush()
orclose()
is called.- Parameters:
-
writer
- the targetWriter
-
charsetName
- the name of the charset encoding
WriterOutputStream
@Deprecated public WriterOutputStream(Writer writer)
Deprecated. 2.5 useWriterOutputStream(Writer, Charset)
insteadConstructs a newWriterOutputStream
that uses the default character encoding and with a default output buffer size of 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 IOException
Write bytes from the specified byte array to the stream.- Overrides:
-
write
in classOutputStream
- Parameters:
-
b
- the byte array containing the bytes to write -
off
- the start offset in the byte array -
len
- the number of bytes to write - Throws:
-
IOException
- if an I/O error occurs.
write
public void write(byte[] b) throws IOException
Write bytes from the specified byte array to the stream.- Overrides:
-
write
in classOutputStream
- Parameters:
-
b
- the byte array containing the bytes to write - Throws:
-
IOException
- if an I/O error occurs.
write
public void write(int b) throws IOException
Write a single byte to the stream.- Specified by:
-
write
in classOutputStream
- Parameters:
-
b
- the byte to write - Throws:
-
IOException
- if an I/O error occurs.
flush
public void flush() throws IOException
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:
-
flush
in interfaceFlushable
- Overrides:
-
flush
in classOutputStream
- Throws:
-
IOException
- if an I/O error occurs.
close
public void close() throws IOException
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:
-
close
in interfaceCloseable
- Specified by:
-
close
in interfaceAutoCloseable
- Overrides:
-
close
in classOutputStream
- Throws:
-
IOException
- if an I/O error occurs.
Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.