Class ReaderInputStream

java.lang.Object
java.io.InputStream
com.google.common.io.ReaderInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

final class ReaderInputStream extends InputStream
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 Details

    • reader

      private final Reader reader
    • encoder

      private final CharsetEncoder encoder
    • singleByte

      private final byte[] singleByte
    • charBuffer

      private 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 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 Details

    • ReaderInputStream

      ReaderInputStream(Reader reader, Charset charset, int bufferSize)
      Creates a new input stream that will encode the characters from reader into bytes using the given character set. Malformed input and unmappable characters will be replaced.
      Parameters:
      reader - input source
      charset - character set used for encoding chars to bytes
      bufferSize - 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 from reader into bytes using the given character set encoder.
      Parameters:
      reader - input source
      encoder - character set encoder used for encoding chars to bytes
      bufferSize - size of internal input and output buffers
      Throws:
      IllegalArgumentException - if bufferSize is non-positive
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • grow

      private static CharBuffer grow(CharBuffer buf)
      Returns a new CharBuffer identical to buf, except twice the capacity.
    • readMoreChars

      private void readMoreChars() throws IOException
      Handle the case of underflow caused by needing more input characters.
      Throws:
      IOException
    • availableCapacity

      private static int availableCapacity(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.