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.
  • Field Summary

    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
    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.

    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
  • 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
    • 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
    • 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