@GwtIncompatible final class ReaderInputStream extends java.io.InputStream
InputStream
that converts characters from a Reader
into bytes using an
arbitrary Charset.
This is an alternative to copying the data to an OutputStream
via a Writer
,
which is necessarily blocking. By implementing an InputStream
it allows consumers to
"pull" as much data as they can handle, which is more convenient when dealing with flow
controlled, async APIs.
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
byteBuffer
byteBuffer holds encoded characters that have not yet been sent to the caller of the input
stream.
|
private java.nio.CharBuffer |
charBuffer
charBuffer holds characters that have been read from the Reader but not encoded yet.
|
private boolean |
doneFlushing
Whether we've successfully flushed the encoder.
|
private boolean |
draining
Whether we're copying encoded bytes to the caller's buffer.
|
private java.nio.charset.CharsetEncoder |
encoder |
private boolean |
endOfInput
Whether we've finished reading the reader.
|
private java.io.Reader |
reader |
private byte[] |
singleByte |
Constructor and Description |
---|
ReaderInputStream(java.io.Reader reader,
java.nio.charset.CharsetEncoder encoder,
int bufferSize)
Creates a new input stream that will encode the characters from
reader into bytes using
the given character set encoder. |
ReaderInputStream(java.io.Reader reader,
java.nio.charset.Charset charset,
int bufferSize)
Creates a new input stream that will encode the characters from
reader into bytes using
the given character set. |
Modifier and Type | Method and Description |
---|---|
private static int |
availableCapacity(java.nio.Buffer buffer)
Returns the number of elements between the limit and capacity.
|
void |
close() |
private int |
drain(byte[] b,
int off,
int len)
Copy as much of the byte buffer into the output array as possible, returning the (positive)
number of characters copied.
|
private static java.nio.CharBuffer |
grow(java.nio.CharBuffer buf)
Returns a new CharBuffer identical to buf, except twice the capacity.
|
int |
read() |
int |
read(byte[] b,
int off,
int len) |
private void |
readMoreChars()
Handle the case of underflow caused by needing more input characters.
|
private void |
startDraining(boolean overflow)
Flips the buffer output buffer so we can start reading bytes from it.
|
private final java.io.Reader reader
private final java.nio.charset.CharsetEncoder encoder
private final byte[] singleByte
private java.nio.CharBuffer charBuffer
private java.nio.ByteBuffer byteBuffer
private boolean endOfInput
private boolean draining
private boolean doneFlushing
ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)
reader
into bytes using
the given character set. Malformed input and unmappable characters will be replaced.reader
- input sourcecharset
- character set used for encoding chars to bytesbufferSize
- size of internal input and output buffersjava.lang.IllegalArgumentException
- if bufferSize is non-positiveReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder, int bufferSize)
reader
into bytes using
the given character set encoder.reader
- input sourceencoder
- character set encoder used for encoding chars to bytesbufferSize
- size of internal input and output buffersjava.lang.IllegalArgumentException
- if bufferSize is non-positivepublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
private static java.nio.CharBuffer grow(java.nio.CharBuffer buf)
private void readMoreChars() throws java.io.IOException
java.io.IOException
private static int availableCapacity(java.nio.Buffer buffer)
private void startDraining(boolean overflow)
private int drain(byte[] b, int off, int len)