public class WriterOutputStream
extends java.io.OutputStream
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):
To build an instance, see WriterOutputStream.Builder.
OutputStream out = ... Charset cs = ... OutputStreamWriter writer = new OutputStreamWriter(out, cs); WriterOutputStream out2 = WriterOutputStream.builder() .setWriter(writer) .setCharset(cs) .get();
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.
ReaderInputStream| Modifier and Type | Class and Description |
|---|---|
static class |
WriterOutputStream.Builder
Builds a new
WriterOutputStream instance. |
| Constructor and Description |
|---|
WriterOutputStream(java.io.Writer writer)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.nio.charset.Charset charset)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.nio.charset.CharsetDecoder decoder)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.nio.charset.CharsetDecoder decoder,
int bufferSize,
boolean writeImmediately)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.nio.charset.Charset charset,
int bufferSize,
boolean writeImmediately)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.lang.String charsetName)
Deprecated.
|
WriterOutputStream(java.io.Writer writer,
java.lang.String charsetName,
int bufferSize,
boolean writeImmediately)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
static WriterOutputStream.Builder |
builder()
Constructs a new
WriterOutputStream.Builder. |
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.
|
@Deprecated public WriterOutputStream(java.io.Writer writer)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream 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 when flush() or close() is called.writer - the target Writer@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.nio.charset.Charset charset)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream with a default output buffer size of characters. The output buffer will only be flushed
when it overflows or when flush() or close() is called.writer - the target Writercharset - the charset encoding@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.nio.charset.Charset charset,
int bufferSize,
boolean writeImmediately)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream.writer - the target 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 Writer immediately. If false, the output buffer will only be flushed when it overflows or when
flush() or close() is called.@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.nio.charset.CharsetDecoder decoder)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream with a default output buffer size of characters. The output buffer will only be flushed
when it overflows or when flush() or close() is called.writer - the target Writerdecoder - the charset decoder@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.nio.charset.CharsetDecoder decoder,
int bufferSize,
boolean writeImmediately)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream.writer - the target 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 Writer immediately. If false, the output buffer will only be flushed when it overflows or when
flush() or close() is called.@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.lang.String charsetName)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream with a default output buffer size of characters. The output buffer will only be flushed
when it overflows or when flush() or close() is called.writer - the target WritercharsetName - the name of the charset encoding@Deprecated
public WriterOutputStream(java.io.Writer writer,
java.lang.String charsetName,
int bufferSize,
boolean writeImmediately)
builder(), WriterOutputStream.Builder, and WriterOutputStream.Builder.get()WriterOutputStream.writer - the target 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 Writer immediately. If false, the output buffer will only be flushed when it overflows or when
flush() or close() is called.public static WriterOutputStream.Builder builder()
WriterOutputStream.Builder.WriterOutputStream.Builder.public void close()
throws java.io.IOException
Writer. After that
Writer.close() will be called.close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.OutputStreamjava.io.IOException - if an I/O error occurs.public void flush()
throws java.io.IOException
Writer. After that
Writer.flush() will be called.flush in interface java.io.Flushableflush in class java.io.OutputStreamjava.io.IOException - if an I/O error occurs.public void write(byte[] b)
throws java.io.IOException
write in class java.io.OutputStreamb - the byte array containing the bytes to writejava.io.IOException - if an I/O error occurs.public void write(byte[] b,
int off,
int len)
throws java.io.IOException
write in class java.io.OutputStreamb - the byte array containing the bytes to writeoff - the start offset in the byte arraylen - the number of bytes to writejava.io.IOException - if an I/O error occurs.public void write(int b)
throws java.io.IOException
write in class java.io.OutputStreamb - the byte to writejava.io.IOException - if an I/O error occurs.Copyright © 2010 - 2023 Adobe. All Rights Reserved