{"record":{"id":"9b80ee788578cd4b","repo":"TheAlgorithms/Java","slug":"empty-or-already-closed-stream-provided","errorCode":null,"errorMessage":"Empty or already closed stream provided","messagePattern":"Empty or already closed stream provided","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/io/BufferedReader.java","lineNumber":48,"sourceCode":"    private int posRead = 0;\n    private int bufferPos = 0;\n\n    private boolean foundEof = false;\n\n    private InputStream input;\n\n    public BufferedReader(byte[] input) throws IOException {\n        this(new ByteArrayInputStream(input));\n    }\n\n    public BufferedReader(InputStream input) throws IOException {\n        this(input, DEFAULT_BUFFER_SIZE);\n    }\n\n    public BufferedReader(InputStream input, int bufferSize) throws IOException {\n        this.input = input;\n        if (input.available() == -1) {\n            throw new IOException(\"Empty or already closed stream provided\");\n        }\n\n        this.bufferSize = bufferSize;\n        buffer = new byte[bufferSize];\n    }\n\n    /**\n     * Reads a single byte from the stream\n     */\n    public int read() throws IOException {\n        if (needsRefill()) {\n            if (foundEof) {\n                return -1;\n            }\n            // the buffer is empty, or the buffer has\n            // been completely read and needs to be refilled\n            refill();\n        }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/io/BufferedReader.java#L30-L66","documentation":"BufferedReader's constructor checks input.available() == -1 to detect a stream that is already exhausted or closed before any read begins. Note: this is an unusual use of available() (the InputStream contract says available() returns 0, not -1, when no bytes are available; -1 is non-standard), so this check primarily catches custom InputStream implementations that return -1 to signal closed/empty state. Thrown as IOException from the constructor.","triggerScenarios":"Constructing new BufferedReader(inputStream) where inputStream.available() returns -1 — typically a custom or already-closed stream. Passing a stream that was previously closed, or a custom InputStream whose available() is implemented to return -1.","commonSituations":"Wrapping a stream that was already closed elsewhere (double-close), using a custom InputStream subclass that returns -1 from available() when empty, or reusing a stream object after exhaustion.","solutions":["Do not close the underlying stream before wrapping it; ensure single ownership.","If using a custom InputStream, have available() return 0 (per java.io.InputStream contract) rather than -1 when no bytes are available.","Check the stream is not already consumed before constructing the reader."],"exampleFix":"// before\ncustomStream.close();\nnew BufferedReader(customStream); // available() == -1\n\n// after\n// do not close beforehand; let BufferedReader own it\nnew BufferedReader(customStream);","handlingStrategy":"try-catch","validationCode":"// Limited pre-check possible; available()==-1 is non-standard.\n// Best: ensure the stream is open and not pre-closed before wrapping.\nif (input == null) throw new IOException(\"stream is null\");\nnew BufferedReader(input);","typeGuard":null,"tryCatchPattern":"try {\n    BufferedReader reader = new BufferedReader(input);\n} catch (IOException e) {\n    // stream was empty/closed; fall back to an empty source or rethrow with context\n    throw new IOException(\"failed to open buffered reader: \" + e.getMessage(), e);\n}","preventionTips":["Never close the underlying stream before wrapping it in BufferedReader; transfer ownership.","If you implement a custom InputStream, follow the contract: available() returns 0 (not -1) when no bytes.","Avoid reusing stream references after they have been consumed or closed."],"tags":["io","stream","constructor","closed-stream","buffered-reader"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}