Package com.cedarsoftware.util
Class FastReader
java.lang.Object
java.io.Reader
com.cedarsoftware.util.FastReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
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
-
Constructor Summary
ConstructorsConstructorDescriptionFastReader(Reader in) FastReader(Reader in, char[] buffer, char[] pushbackBuffer) Create reader using caller-provided buffers.FastReader(Reader in, int bufferSize, int pushbackBufferSize) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()intgetCol()Deprecated.Column tracking removed for performance optimizationReturns the last portion of the buffer that has been read, useful for error context.intgetLine()Deprecated.Line tracking removed for performance optimizationvoidpushback(char ch) intread()intread(char[] cbuf, int off, int len) intreadLine(char[] dest, int off, int maxLen) Reads a complete line into dest, handling \n, \r, and \r\n line endings.intreadUntil(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
-
Constructor Details
-
FastReader
-
FastReader
-
FastReader
Create reader using caller-provided buffers. Arrays are used directly (no copy).
-
-
Method Details
-
pushback
public void pushback(char ch) -
read
public int read() -
read
public int read(char[] cbuf, int off, int len) -
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 intooff- the offset in the destination buffer to start writingmaxLen- the maximum number of characters to readdelim1- 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 bufferoff- offset in dest to start writingmaxLen- maximum number of characters to read- Returns:
- line length (excluding ending), or -1 on EOF
-
close
public void close() -
getLine
Deprecated.Line tracking removed for performance optimization- Returns:
- 0 - line tracking removed for performance. Use getLastSnippet() for error context.
-
getCol
Deprecated.Column tracking removed for performance optimization- Returns:
- 0 - column tracking removed for performance. Use getLastSnippet() for error context.
-
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
-