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