Class FastReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
- 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 ClassesModifier and TypeClassDescriptionstatic final classBorrowed view of a contiguous range insideFastReader's internal buffers. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intReturned 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. -
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.intBorrow a complete line from the current internal buffer without copying.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.intreadUntilBorrowed(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
-
Field Details
-
COPY_REQUIRED
public static final int COPY_REQUIREDReturned 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
-
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
-
readUntilBorrowed
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 callFastReader.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 ormaxLenis reached, this method returnsCOPY_REQUIREDwithout consuming any characters; callers should then usereadUntil(char[], int, int, char, char).- Parameters:
slice- destination for the borrowed buffer, offset, and lengthmaxLen- maximum number of characters to borrowdelim1- first delimiter character to stop atdelim2- 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 bufferoff- offset in dest to start writingmaxLen- maximum number of characters to read- Returns:
- line length (excluding ending), or -1 on EOF
-
readLineBorrowed
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 returnsCOPY_REQUIREDwithout consuming any characters; callers should then usereadLine(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() -
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
-