Package com.google.common.io
Class ReaderInputStream
java.lang.Object
java.io.InputStream
com.google.common.io.ReaderInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
An
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ByteBuffer
byteBuffer holds encoded characters that have not yet been sent to the caller of the input stream.private CharBuffer
charBuffer holds characters that have been read from the Reader but not encoded yet.private boolean
Whether we've successfully flushed the encoder.private boolean
Whether we're copying encoded bytes to the caller's buffer.private final CharsetEncoder
private boolean
Whether we've finished reading the reader.private final Reader
private final byte[]
-
Constructor Summary
ConstructorsConstructorDescriptionReaderInputStream
(Reader reader, CharsetEncoder encoder, int bufferSize) Creates a new input stream that will encode the characters fromreader
into bytes using the given character set encoder.ReaderInputStream
(Reader reader, Charset charset, int bufferSize) Creates a new input stream that will encode the characters fromreader
into bytes using the given character set. -
Method Summary
Modifier and TypeMethodDescriptionprivate static int
availableCapacity
(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 CharBuffer
grow
(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
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.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Field Details
-
reader
-
encoder
-
singleByte
private final byte[] singleByte -
charBuffer
charBuffer holds characters that have been read from the Reader but not encoded yet. The buffer is perpetually "flipped" (unencoded characters between position and limit). -
byteBuffer
byteBuffer holds encoded characters that have not yet been sent to the caller of the input stream. When encoding it is "unflipped" (encoded bytes between 0 and position) and when draining it is flipped (undrained bytes between position and limit). -
endOfInput
private boolean endOfInputWhether we've finished reading the reader. -
draining
private boolean drainingWhether we're copying encoded bytes to the caller's buffer. -
doneFlushing
private boolean doneFlushingWhether we've successfully flushed the encoder.
-
-
Constructor Details
-
ReaderInputStream
Creates a new input stream that will encode the characters fromreader
into bytes using the given character set. Malformed input and unmappable characters will be replaced.- Parameters:
reader
- input sourcecharset
- character set used for encoding chars to bytesbufferSize
- size of internal input and output buffers- Throws:
IllegalArgumentException
- if bufferSize is non-positive
-
ReaderInputStream
ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) Creates a new input stream that will encode the characters fromreader
into bytes using the given character set encoder.- Parameters:
reader
- input sourceencoder
- character set encoder used for encoding chars to bytesbufferSize
- size of internal input and output buffers- Throws:
IllegalArgumentException
- if bufferSize is non-positive
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
grow
Returns a new CharBuffer identical to buf, except twice the capacity. -
readMoreChars
Handle the case of underflow caused by needing more input characters.- Throws:
IOException
-
availableCapacity
Returns the number of elements between the limit and capacity. -
startDraining
private void startDraining(boolean overflow) Flips the buffer output buffer so we can start reading bytes from it. If we are starting to drain because there was overflow, and there aren't actually any characters to drain, then the overflow must be due to a small output buffer. -
drain
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.
-