Class ReaderInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ReaderInputStream
    extends java.io.InputStream
    ReaderInputStream accepts a UTF-16 char stream (Reader) as input and converts it to a UTF-8 byte stream (InputStream) as output.

    This is the inverse of java.io.InputStreamReader which converts a binary stream to a character stream.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.ByteBuffer bbuf  
      private java.nio.CharBuffer cbuf  
      private static int DEFAULT_CHAR_BUFFER_SIZE  
      private java.nio.charset.CharsetEncoder encoder  
      private boolean endOfInput
      true when all of the characters have been read from the reader into inbuf.
      private byte[] oneByte  
      private java.io.Reader reader  
    • Constructor Summary

      Constructors 
      Constructor Description
      ReaderInputStream​(java.io.Reader reader)  
      ReaderInputStream​(java.io.Reader reader, int charBufferSize)
      Allow ReaderInputStreamTest to use small buffers to force UTF-16 surrogate pairs to cross buffer boundaries in interesting ways.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void advance()  
      private static void checkEncodeResult​(java.nio.charset.CoderResult result)  
      void close()  
      int read()  
      int read​(byte[] b, int off, int len)  
      • Methods inherited from class java.io.InputStream

        available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CHAR_BUFFER_SIZE

        private static final int DEFAULT_CHAR_BUFFER_SIZE
        See Also:
        Constant Field Values
      • reader

        private final java.io.Reader reader
      • encoder

        private final java.nio.charset.CharsetEncoder encoder
      • bbuf

        private final java.nio.ByteBuffer bbuf
      • cbuf

        private final java.nio.CharBuffer cbuf
      • endOfInput

        private boolean endOfInput
        true when all of the characters have been read from the reader into inbuf.
      • oneByte

        private final byte[] oneByte
    • Constructor Detail

      • ReaderInputStream

        public ReaderInputStream​(java.io.Reader reader)
      • ReaderInputStream

        ReaderInputStream​(java.io.Reader reader,
                          int charBufferSize)
        Allow ReaderInputStreamTest to use small buffers to force UTF-16 surrogate pairs to cross buffer boundaries in interesting ways. Because this constructor is package-private, the unit test must be in the same package.
    • Method Detail

      • advance

        private void advance()
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • checkEncodeResult

        private static void checkEncodeResult​(java.nio.charset.CoderResult result)
                                       throws java.nio.charset.CharacterCodingException
        Throws:
        java.nio.charset.CharacterCodingException
      • read

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

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException