{"record":{"id":"4e1ba034845605c9","repo":"TheAlgorithms/Java","slug":"out-of-range-available-d-but-trying-with-d","errorCode":null,"errorMessage":"Out of range, available %d, but trying with %d","messagePattern":"Out of range, available (.+?), but trying with (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/io/BufferedReader.java","lineNumber":99,"sourceCode":"        return bufferPos - posRead + available;\n    }\n\n    /**\n     * Returns the next character\n     */\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++) {","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/io/BufferedReader.java#L81-L117","documentation":"BufferedReader.peek(n) throws IOException when n >= available() — you cannot peek beyond the bytes currently readable from the stream. The check fires before any buffer refresh, so it guards against reading past EOF. Note the boundary: it triggers on n >= available (strict), so peeking the very last available byte with n == available-1 is the maximum valid call.","triggerScenarios":"Calling peek(n) where n >= the number of bytes available, e.g. peek(10) on a stream with only 5 bytes, or peek(0) when available() == 0 (note n==0 on empty triggers since 0 >= 0).","commonSituations":"Peeking ahead for a multi-byte token (e.g. a 4-byte length prefix) when the stream has fewer bytes remaining, calling peek near EOF, or passing a computed n based on expected frame size that exceeds actual data.","solutions":["Check available() before peeking: if (reader.available() > n) reader.peek(n).","Read more data first (refill) or accept that the stream is exhausted.","Use peek(1) / read() loop instead of a large fixed peek near EOF."],"exampleFix":"// before\nint b = reader.peek(8); // may exceed available\n\n// after\nif (reader.available() > 8) {\n    int b = reader.peek(8);\n} else {\n    // handle short input\n}","handlingStrategy":"validation","validationCode":"if (n >= reader.available()) {\n    // not enough bytes to peek; handle short input\n    return Optional.empty();\n}\nint b = reader.peek(n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check available() before peek(n) near EOF.","Prefer small peek values (peek(1)) and read incrementally when frame size is uncertain.","Treat 'not enough bytes' as a normal short-input condition, not an exceptional one."],"tags":["io","stream","peek","eof","buffered-reader","bounds-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}