TheAlgorithms/Java · error · IllegalAccessError
Cannot peek %s, maximum upto %s (Buffer Limit)
Error message
Cannot peek %s, maximum upto %s (Buffer Limit)
What it means
BufferedReader.peek(n) throws IllegalAccessError (note: an Error, not an Exception) when n >= bufferSize, because the internal buffer is a fixed-size byte array of length bufferSize (default 5). You cannot peek beyond the buffer's capacity even if more bytes are available in the underlying stream. This is a hard limit imposed by the buffer dimension.
Source
Thrown at src/main/java/com/thealgorithms/io/BufferedReader.java:104
*/
public int peek() throws IOException {
return peek(1);
}
/**
* Peeks and returns a value located at next {n}
*/
public int peek(int n) throws IOException {
int available = available();
if (n >= available) {
throw new IOException("Out of range, available %d, but trying with %d".formatted(available, n));
}
pushRefreshData();
if (n >= bufferSize) {
throw new IllegalAccessError("Cannot peek %s, maximum upto %s (Buffer Limit)".formatted(n, bufferSize));
}
return buffer[n];
}
/**
* Removes the already read bytes from the buffer
* in-order to make space for new bytes to be filled up.
* <p>
* This may also do the job to read first time data (the whole buffer is empty)
*/
private void pushRefreshData() throws IOException {
for (int i = posRead, j = 0; i < bufferSize; i++, j++) {
buffer[j] = buffer[i];
}
bufferPos -= posRead;
posRead = 0;View on GitHub (pinned to fdfb9a395b)
Solutions
- Construct with a larger buffer: new BufferedReader(stream, 8192) then peek up to 8191.
- Keep peek requests within the buffer size you configured; check n < bufferSize before calling.
- For large lookaheads, read bytes into your own array instead of using peek.
Example fix
// before (default buffer size 5) BufferedReader r = new BufferedReader(stream); int b = r.peek(10); // IllegalAccessError // after BufferedReader r = new BufferedReader(stream, 8192); int b = r.peek(10); // ok, within buffer
Defensive patterns
Strategy: validation
Validate before calling
if (n >= bufferSize) {
throw new IllegalArgumentException("peek " + n + " exceeds buffer size " + bufferSize);
}
reader.peek(n); Prevention
- Construct BufferedReader with an explicit bufferSize sized to your max lookahead (e.g. new BufferedReader(stream, 8192)).
- Remember the default buffer is only 5 bytes — far smaller than typical expectations.
- For large lookaheads, read into your own byte array instead of relying on peek.
When it happens
Trigger: Calling peek(n) with n >= bufferSize (default 5), e.g. peek(5) or peek(100). Even if the stream has plenty of data, the buffer cannot hold that lookahead.
Common situations: Using the default constructor (bufferSize=5) and trying to peek more than 4 bytes ahead, or assuming the buffer is large enough for a full token/header. The tiny default (5) surprises developers expecting a typical 8KB buffer.
Related errors
- Out of range, available %d, but trying with %d
- Empty or already closed stream provided
- Input Stream already closed!
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/5f617c2ad1f9f42b.
Report an issue: GitHub.