Class UTF8Reader

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

    public final class UTF8Reader
    extends java.io.Reader
    Optimized Reader that reads UTF-8 encoded content from an input stream. In addition to doing (hopefully) optimal conversion, it can also take array of "pre-read" (leftover) bytes; this is necessary when preliminary stream/reader is trying to figure out underlying character encoding.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean _autoClose  
      private int _byteCount
      Total read byte count; used for error reporting purposes
      private int _charCount
      Total read character count; used for error reporting purposes
      private int _decodeErrorOffset
      Flag that is set when a pending decode error has been detected; needed to properly handle deferred reporting.
      private byte[] _inputBuffer  
      private int _inputEnd
      Pointed to the end marker, that is, position one after the last valid available byte.
      private int _inputPtr
      Pointer to the next available byte (if any), iff less than mByteBufferEnd
      private java.io.InputStream _inputSource  
      private com.fasterxml.jackson.core.io.IOContext _ioContext
      IO context to use for returning input buffer, iff buffer is to be recycled when input ends.
      private int _surrogate
      Decoded first character of a surrogate pair, if one needs to be buffered
      private char[] _tmpBuffer  
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt, byte[] buf, int ptr, int len)  
      UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt, java.io.InputStream in, boolean autoClose)  
      UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt, java.io.InputStream in, boolean autoClose, byte[] buf, int ptr, int len)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean canModifyBuffer()
      Method that can be used to see if we can actually modify the underlying buffer.
      void close()  
      void freeBuffers()
      This method should be called along with (or instead of) normal close.
      protected java.io.InputStream getStream()  
      private boolean loadMore​(int available)  
      int read()
      Although this method is implemented by the base class, AND it should never be called by parser code, let's still implement it bit more efficiently just in case
      int read​(char[] cbuf)  
      int read​(char[] cbuf, int start, int len)  
      protected int readBytes()
      Method for reading as many bytes from the underlying stream as possible (that fit in the buffer), to the beginning of the buffer.
      protected int readBytesAt​(int offset)
      Method for reading as many bytes from the underlying stream as possible (that fit in the buffer considering offset), to the specified offset.
      protected void reportBounds​(char[] cbuf, int start, int len)  
      protected void reportDeferredInvalid()  
      protected void reportInvalidInitial​(int mask, int outputDecoded)  
      protected void reportInvalidOther​(int mask, int outputDecoded, int errorPosition)  
      protected void reportStrangeStream()  
      protected void reportUnexpectedEOF​(int gotBytes, int needed)  
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, ready, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

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

      • _ioContext

        private final com.fasterxml.jackson.core.io.IOContext _ioContext
        IO context to use for returning input buffer, iff buffer is to be recycled when input ends.
      • _inputSource

        private java.io.InputStream _inputSource
      • _autoClose

        private final boolean _autoClose
      • _inputBuffer

        private byte[] _inputBuffer
      • _inputPtr

        private int _inputPtr
        Pointer to the next available byte (if any), iff less than mByteBufferEnd
      • _inputEnd

        private int _inputEnd
        Pointed to the end marker, that is, position one after the last valid available byte.
      • _surrogate

        private int _surrogate
        Decoded first character of a surrogate pair, if one needs to be buffered
      • _charCount

        private int _charCount
        Total read character count; used for error reporting purposes
      • _byteCount

        private int _byteCount
        Total read byte count; used for error reporting purposes
      • _decodeErrorOffset

        private int _decodeErrorOffset
        Flag that is set when a pending decode error has been detected; needed to properly handle deferred reporting.
      • _tmpBuffer

        private char[] _tmpBuffer
    • Constructor Detail

      • UTF8Reader

        public UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt,
                          java.io.InputStream in,
                          boolean autoClose,
                          byte[] buf,
                          int ptr,
                          int len)
      • UTF8Reader

        public UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt,
                          byte[] buf,
                          int ptr,
                          int len)
      • UTF8Reader

        public UTF8Reader​(com.fasterxml.jackson.core.io.IOContext ctxt,
                          java.io.InputStream in,
                          boolean autoClose)
    • Method Detail

      • canModifyBuffer

        protected final boolean canModifyBuffer()
        Method that can be used to see if we can actually modify the underlying buffer. This is the case if we are managing the buffer, but not if it was just given to us.
      • close

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

        public int read()
                 throws java.io.IOException
        Although this method is implemented by the base class, AND it should never be called by parser code, let's still implement it bit more efficiently just in case
        Overrides:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read​(char[] cbuf)
                 throws java.io.IOException
        Overrides:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read​(char[] cbuf,
                        int start,
                        int len)
                 throws java.io.IOException
        Specified by:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • getStream

        protected final java.io.InputStream getStream()
      • readBytes

        protected final int readBytes()
                               throws java.io.IOException
        Method for reading as many bytes from the underlying stream as possible (that fit in the buffer), to the beginning of the buffer.
        Throws:
        java.io.IOException
      • readBytesAt

        protected final int readBytesAt​(int offset)
                                 throws java.io.IOException
        Method for reading as many bytes from the underlying stream as possible (that fit in the buffer considering offset), to the specified offset.
        Returns:
        Number of bytes read, if any; -1 to indicate none available (that is, end of input)
        Throws:
        java.io.IOException
      • freeBuffers

        public final void freeBuffers()
        This method should be called along with (or instead of) normal close. After calling this method, no further reads should be tried. Method will try to recycle read buffers (if any).
      • loadMore

        private boolean loadMore​(int available)
                          throws java.io.IOException
        Parameters:
        available - Number of "unused" bytes in the input buffer
        Returns:
        True, if enough bytes were read to allow decoding of at least one full character; false if EOF was encountered instead.
        Throws:
        java.io.IOException
      • reportBounds

        protected void reportBounds​(char[] cbuf,
                                    int start,
                                    int len)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • reportStrangeStream

        protected void reportStrangeStream()
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • reportInvalidInitial

        protected void reportInvalidInitial​(int mask,
                                            int outputDecoded)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • reportInvalidOther

        protected void reportInvalidOther​(int mask,
                                          int outputDecoded,
                                          int errorPosition)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • reportDeferredInvalid

        protected void reportDeferredInvalid()
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • reportUnexpectedEOF

        protected void reportUnexpectedEOF​(int gotBytes,
                                           int needed)
                                    throws java.io.IOException
        Throws:
        java.io.IOException