Package com.google.common.io
Class ReaderInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.google.common.io.ReaderInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
final class ReaderInputStream extends java.io.InputStreamAnInputStreamthat converts characters from aReaderinto bytes using an arbitrary Charset.This is an alternative to copying the data to an
OutputStreamvia aWriter, which is necessarily blocking. By implementing anInputStreamit allows consumers to "pull" as much data as they can handle, which is more convenient when dealing with flow controlled, async APIs.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBufferbyteBufferbyteBuffer holds encoded characters that have not yet been sent to the caller of the input stream.private java.nio.CharBuffercharBuffercharBuffer holds characters that have been read from the Reader but not encoded yet.private booleandoneFlushingWhether we've successfully flushed the encoder.private booleandrainingWhether we're copying encoded bytes to the caller's buffer.private java.nio.charset.CharsetEncoderencoderprivate booleanendOfInputWhether we've finished reading the reader.private java.io.Readerreaderprivate byte[]singleByte
-
Constructor Summary
Constructors Constructor Description ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder, int bufferSize)Creates a new input stream that will encode the characters fromreaderinto 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 fromreaderinto bytes using the given character set.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static intavailableCapacity(java.nio.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 java.nio.CharBuffergrow(java.nio.CharBuffer buf)Returns a new CharBuffer identical to buf, except twice the capacity.intread()intread(byte[] b, int off, int len)private voidreadMoreChars()Handle 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.
-
-
-
Field Detail
-
reader
private final java.io.Reader reader
-
encoder
private final java.nio.charset.CharsetEncoder encoder
-
singleByte
private final byte[] singleByte
-
charBuffer
private java.nio.CharBuffer 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
private java.nio.ByteBuffer 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 endOfInput
Whether we've finished reading the reader.
-
draining
private boolean draining
Whether we're copying encoded bytes to the caller's buffer.
-
doneFlushing
private boolean doneFlushing
Whether we've successfully flushed the encoder.
-
-
Constructor Detail
-
ReaderInputStream
ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)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:
java.lang.IllegalArgumentException- if bufferSize is non-positive
-
ReaderInputStream
ReaderInputStream(java.io.Reader reader, java.nio.charset.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:
java.lang.IllegalArgumentException- if bufferSize is non-positive
-
-
Method Detail
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
grow
private static java.nio.CharBuffer grow(java.nio.CharBuffer buf)
Returns a new CharBuffer identical to buf, except twice the capacity.
-
readMoreChars
private void readMoreChars() throws java.io.IOExceptionHandle the case of underflow caused by needing more input characters.- Throws:
java.io.IOException
-
availableCapacity
private static int availableCapacity(java.nio.Buffer buffer)
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.
-
-