@CleanupObligation public class AppendableOutputStream extends OutputStream
OutputStream
implementation that transforms a byte stream to a
character stream using a specified character set encoding and writes the resulting
stream to a Writer
. The stream is transformed using a
CharsetDecoder
object, guaranteeing that all character set
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 AppendableOutputStream
.
AppendableOutputStream
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
character set encoding):
OutputStream out = ... Charset cs = ... OutputStreamWriter writer = new OutputStreamWriter(out, cs); AppendableOutputStream out2 = new AppendableOutputStream(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 AppendableOutputStream
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 AppendableOutputStream
are not thread safe.
ReaderInputStream
Modifier and Type | Field and Description |
---|---|
protected CharBuffer |
decoderOut
CharBuffer used as output for the decoder.
|
Constructor and Description |
---|
AppendableOutputStream(Appendable writer,
Charset charset)
Constructs a new
WriterOutputStream with a default output buffer size of
1024 characters. |
AppendableOutputStream(Appendable writer,
CharsetDecoder decoder)
Constructs a new
WriterOutputStream with a default output buffer size of
1024 characters. |
AppendableOutputStream(Appendable writer,
CharsetDecoder decoder,
int bufferSize)
Constructs a new
WriterOutputStream . |
AppendableOutputStream(Appendable writer,
Charset charset,
int bufferSize)
Constructs a new
WriterOutputStream . |
AppendableOutputStream(Appendable writer,
String charsetName)
Constructs a new
WriterOutputStream with a default output buffer size of
1024 characters. |
AppendableOutputStream(Appendable writer,
String charsetName,
int bufferSize)
Constructs a new
WriterOutputStream . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the stream.
|
void |
flush()
Flush the stream.
|
protected void |
flushOutput()
Flush the output.
|
protected void |
processInput(boolean endOfInput)
Decode the contents of the input ByteBuffer into a CharBuffer.
|
String |
toString() |
void |
write(byte[] b)
Write bytes from the specified byte array to the stream.
|
void |
write(byte[] b,
int poff,
int plen)
Write bytes from the specified byte array to the stream.
|
void |
write(int b)
Write a single byte to the stream.
|
protected CharBuffer decoderOut
public AppendableOutputStream(Appendable writer, CharsetDecoder decoder)
WriterOutputStream
with a default output buffer size of
1024 characters. The output buffer will only be flushed when it overflows or when
flush()
or close()
is called.writer
- the target Writer
decoder
- the charset decoderpublic AppendableOutputStream(Appendable writer, CharsetDecoder decoder, int bufferSize)
WriterOutputStream
.writer
- the target Writer
decoder
- the charset decoderbufferSize
- the size of the output buffer in number of characterspublic AppendableOutputStream(Appendable writer, Charset charset, int bufferSize)
WriterOutputStream
.writer
- the target Writer
charset
- the charset encodingbufferSize
- the size of the output buffer in number of characterspublic AppendableOutputStream(Appendable writer, Charset charset)
WriterOutputStream
with a default output buffer size of
1024 characters. The output buffer will only be flushed when it overflows or when
flush()
or close()
is called.writer
- the target Writer
charset
- the charset encodingpublic AppendableOutputStream(Appendable writer, String charsetName, int bufferSize)
WriterOutputStream
.writer
- the target Writer
charsetName
- the name of the charset encodingbufferSize
- the size of the output buffer in number of characterspublic AppendableOutputStream(Appendable writer, String charsetName)
WriterOutputStream
with a default output buffer size of
1024 characters. The output buffer will only be flushed when it overflows or when
flush()
or close()
is called.writer
- the target Writer
charsetName
- the name of the charset encodingpublic void write(byte[] b, int poff, int plen) throws IOException
write
in class OutputStream
b
- the byte array containing the bytes to writepoff
- the start offset in the byte arrayplen
- the number of bytes to writeIOException
- if an I/O error occurspublic final void write(byte[] b) throws IOException
write
in class OutputStream
b
- the byte array containing the bytes to writeIOException
- if an I/O error occurspublic final void write(int b) throws IOException
write
in class OutputStream
b
- the byte to writeIOException
- if an I/O error occurspublic void flush() throws IOException
Writer
. After that
Writer.flush()
will be called.flush
in interface Flushable
flush
in class OutputStream
IOException
- if an I/O error occurs@DischargesObligation public void close() throws IOException
Writer
. After that
Writer.close()
will be called.close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
- if an I/O error occursprotected void processInput(boolean endOfInput) throws IOException
endOfInput
- indicates end of inputIOException
- if an I/O error occursprotected void flushOutput() throws IOException
IOException
- if an I/O error occursCopyright © 2018 SPF4J. All rights reserved.