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 ByteBufferbyteBuffer holds encoded characters that have not yet been sent to the caller of the input stream.private CharBuffercharBuffer holds characters that have been read from the Reader but not encoded yet.private booleanWhether we've successfully flushed the encoder.private booleanWhether we're copying encoded bytes to the caller's buffer.private final CharsetEncoderprivate booleanWhether we've finished reading the reader.private final Readerprivate final byte[] -
Constructor Summary
ConstructorsConstructorDescriptionReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) Creates a new input stream that will encode the characters fromreaderinto bytes using the given character set encoder.ReaderInputStream(Reader reader, Charset charset, int bufferSize) Creates a new input stream that will encode the characters fromreaderinto bytes using the given character set. -
Method Summary
Modifier and TypeMethodDescriptionprivate static intavailableCapacity(Buffer buffer) Returns the number of elements between the limit and capacity.voidclose()private intdrain(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 CharBuffergrow(CharBuffer buf) Returns a new CharBuffer identical to buf, except twice the capacity.intread()intread(byte[] b, int off, int len) private voidHandle the case of underflow caused by needing more input characters.private voidstartDraining(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 fromreaderinto 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 fromreaderinto 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin 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.
-