{"record":{"id":"5f617c2ad1f9f42b","repo":"TheAlgorithms/Java","slug":"cannot-peek-s-maximum-upto-s-buffer-limit","errorCode":null,"errorMessage":"Cannot peek %s, maximum upto %s (Buffer Limit)","messagePattern":"Cannot peek (.+?), maximum upto (.+?) \\(Buffer Limit\\)","errorType":"exception","errorClass":"IllegalAccessError","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/io/BufferedReader.java","lineNumber":104,"sourceCode":"     */\n\n    public int peek() throws IOException {\n        return peek(1);\n    }\n\n    /**\n     * Peeks and returns a value located at next {n}\n     */\n\n    public int peek(int n) throws IOException {\n        int available = available();\n        if (n >= available) {\n            throw new IOException(\"Out of range, available %d, but trying with %d\".formatted(available, n));\n        }\n        pushRefreshData();\n\n        if (n >= bufferSize) {\n            throw new IllegalAccessError(\"Cannot peek %s, maximum upto %s (Buffer Limit)\".formatted(n, bufferSize));\n        }\n        return buffer[n];\n    }\n\n    /**\n     * Removes the already read bytes from the buffer\n     * in-order to make space for new bytes to be filled up.\n     * <p>\n     * This may also do the job to read first time data (the whole buffer is empty)\n     */\n\n    private void pushRefreshData() throws IOException {\n        for (int i = posRead, j = 0; i < bufferSize; i++, j++) {\n            buffer[j] = buffer[i];\n        }\n\n        bufferPos -= posRead;\n        posRead = 0;","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/io/BufferedReader.java#L86-L122","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (default buffer size 5)\nBufferedReader r = new BufferedReader(stream);\nint b = r.peek(10); // IllegalAccessError\n\n// after\nBufferedReader r = new BufferedReader(stream, 8192);\nint b = r.peek(10); // ok, within buffer","handlingStrategy":"validation","validationCode":"if (n >= bufferSize) {\n    throw new IllegalArgumentException(\"peek \" + n + \" exceeds buffer size \" + bufferSize);\n}\nreader.peek(n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["io","stream","peek","buffer-limit","buffered-reader","illegal-access-error"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}