Class FastReader

java.lang.Object
java.io.Reader
com.cedarsoftware.util.FastReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public final class FastReader extends Reader
Buffered, Pushback, Reader that does not use synchronization. Much faster than the JDK variants because they use synchronization. Typically, this class is used with a separate instance per thread, so synchronization is not needed.

Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Borrowed view of a contiguous range inside FastReader's internal buffers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Returned by borrowed-slice methods when the requested token/line is not fully available in the current internal buffer and the caller should fall back to the copying API.

    Fields inherited from class java.io.Reader

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    FastReader(Reader in, char[] buffer, char[] pushbackBuffer)
    Create reader using caller-provided buffers.
    FastReader(Reader in, int bufferSize, int pushbackBufferSize)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    int
    Deprecated.
    Column tracking removed for performance optimization
    Returns the last portion of the buffer that has been read, useful for error context.
    int
    Deprecated.
    Line tracking removed for performance optimization
    void
    pushback(char ch)
     
    int
     
    int
    read(char[] cbuf, int off, int len)
     
    int
    readLine(char[] dest, int off, int maxLen)
    Reads a complete line into dest, handling \n, \r, and \r\n line endings.
    int
    Borrow a complete line from the current internal buffer without copying.
    int
    readUntil(char[] dest, int off, int maxLen, char delim1, char delim2)
    Reads characters into the destination array until one of the two delimiter characters is found.
    int
    readUntilBorrowed(FastReader.BufferSlice slice, int maxLen, char delim1, char delim2)
    Borrow a contiguous slice from the internal buffer up to either delimiter.

    Methods inherited from class java.io.Reader

    mark, markSupported, nullReader, read, read, ready, reset, skip, transferTo

    Methods inherited from class java.lang.Object

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

    • COPY_REQUIRED

      public static final int COPY_REQUIRED
      Returned by borrowed-slice methods when the requested token/line is not fully available in the current internal buffer and the caller should fall back to the copying API.
      See Also:
  • Constructor Details

    • FastReader

      public FastReader(Reader in)
    • FastReader

      public FastReader(Reader in, int bufferSize, int pushbackBufferSize)
    • FastReader

      public FastReader(Reader in, char[] buffer, char[] pushbackBuffer)
      Create reader using caller-provided buffers. Arrays are used directly (no copy).
  • Method Details

    • pushback

      public void pushback(char ch)
    • read

      public int read()
      Overrides:
      read in class Reader
    • read

      public int read(char[] cbuf, int off, int len)
      Specified by:
      read in class Reader
    • readUntil

      public int readUntil(char[] dest, int off, int maxLen, char delim1, char delim2)
      Reads characters into the destination array until one of the two delimiter characters is found. The delimiter character is NOT consumed - it remains available for the next read() call. This method is optimized for scanning strings where we want to read until we hit a quote or backslash.
      Parameters:
      dest - the destination buffer to read characters into
      off - the offset in the destination buffer to start writing
      maxLen - the maximum number of characters to read
      delim1 - first delimiter character to stop at (typically quote char)
      delim2 - second delimiter character to stop at (typically backslash)
      Returns:
      the number of characters read (not including delimiter), or -1 if EOF reached before any chars read
    • readUntilBorrowed

      public int readUntilBorrowed(FastReader.BufferSlice slice, int maxLen, char delim1, char delim2)
      Borrow a contiguous slice from the internal buffer up to either delimiter.

      On success, the delimiter is not consumed and the returned length matches slice.getLength(). The caller must consume or copy the borrowed contents and call FastReader.BufferSlice.release() before the next read operation on this reader. If pushback is active or the requested range crosses the current buffer boundary before a delimiter or maxLen is reached, this method returns COPY_REQUIRED without consuming any characters; callers should then use readUntil(char[], int, int, char, char).

      Parameters:
      slice - destination for the borrowed buffer, offset, and length
      maxLen - maximum number of characters to borrow
      delim1 - first delimiter character to stop at
      delim2 - second delimiter character to stop at
      Returns:
      borrowed length, -1 on EOF before any chars, or COPY_REQUIRED
    • readLine

      public int readLine(char[] dest, int off, int maxLen)
      Reads a complete line into dest, handling \n, \r, and \r\n line endings. The line ending is consumed but NOT included in the output.

      Returns the number of characters in the line (excluding the line ending). If maxLen is reached before a line ending is found, returns maxLen and the line ending is NOT consumed — the caller should grow the buffer and call again. Returns -1 on EOF with no data read.

      Optimized for the common case: no pushback active, line fits within the current buffer. Uses a c <= '\r' range guard so that printable characters (the vast majority) require only one comparison per character instead of two.

      Parameters:
      dest - destination buffer
      off - offset in dest to start writing
      maxLen - maximum number of characters to read
      Returns:
      line length (excluding ending), or -1 on EOF
    • readLineBorrowed

      public int readLineBorrowed(FastReader.BufferSlice slice)
      Borrow a complete line from the current internal buffer without copying.

      The line ending is consumed but not included in the borrowed slice. The slice must be consumed or copied and then released via FastReader.BufferSlice.release() before the next read operation on this reader. If pushback is active, the line crosses the current buffer boundary, or CRLF handling would require looking into the next buffer, this method returns COPY_REQUIRED without consuming any characters; callers should then use readLine(char[], int, int).

      Parameters:
      slice - destination for the borrowed buffer, offset, and length
      Returns:
      line length, -1 on EOF before any chars, or COPY_REQUIRED
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
    • getLine

      @Deprecated public int getLine()
      Deprecated.
      Line tracking removed for performance optimization
      Returns:
      0 - line tracking removed for performance. Use getLastSnippet() for error context.
    • getCol

      @Deprecated public int getCol()
      Deprecated.
      Column tracking removed for performance optimization
      Returns:
      0 - column tracking removed for performance. Use getLastSnippet() for error context.
    • getLastSnippet

      public String getLastSnippet()
      Returns the last portion of the buffer that has been read, useful for error context.
      Returns:
      up to the last 200 characters read from the current buffer