{"record":{"id":"3f66d183c461e61a","repo":"apache/flink","slug":"premature-eof-from-inputstream","errorCode":null,"errorMessage":"Premature EOF from inputStream","messagePattern":"Premature EOF from inputStream","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/IOUtils.java","lineNumber":124,"sourceCode":"    // ------------------------------------------------------------------------\n\n    /**\n     * Reads len bytes in a loop.\n     *\n     * @param in The InputStream to read from\n     * @param buf The buffer to fill\n     * @param off offset from the buffer\n     * @param len the length of bytes to read\n     * @throws IOException if it could not read requested number of bytes for any reason (including\n     *     EOF)\n     */\n    public static void readFully(final InputStream in, final byte[] buf, int off, final int len)\n            throws IOException {\n        int toRead = len;\n        while (toRead > 0) {\n            final int ret = in.read(buf, off, toRead);\n            if (ret < 0) {\n                throw new IOException(\"Premature EOF from inputStream\");\n            }\n            toRead -= ret;\n            off += ret;\n        }\n    }\n\n    /**\n     * Similar to {@link #readFully(InputStream, byte[], int, int)}. Returns the total number of\n     * bytes read into the buffer.\n     *\n     * @param in The InputStream to read from\n     * @param buf The buffer to fill\n     * @return The total number of bytes read into the buffer\n     * @throws IOException If the first byte cannot be read for any reason other than end of file,\n     *     or if the input stream has been closed, or if some other I/O error occurs.\n     */\n    public static int tryReadFully(final InputStream in, final byte[] buf) throws IOException {\n        int totalRead = 0;","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/IOUtils.java#L106-L142","documentation":"IOUtils.readFully loops InputStream.read(buf, off, toRead) until len bytes arrive; a negative return signals EOF before the requested count, and it throws IOException(\"Premature EOF from inputStream\"). The contract is all-or-nothing: partial data is treated as corruption, not as a short read.","triggerScenarios":"Reading a fixed-size record/header (e.g. serialized block, MAGIC bytes, length-prefixed payload) from a stream that ends early: truncated file, network socket closed mid-transfer, or a producer that wrote fewer bytes than the reader expects (version/format mismatch on record length).","commonSituations":"Truncated checkpoint/savepoint or cache files on HDFS/local disk; interrupted download of a dependency blob; a serializer change so the reader's expected length no longer matches what was written; sockets closed by timeouts during blob fetch.","solutions":["Verify the producer actually wrote the full payload — compare file sizes / record counts on both sides","Check the stream source for truncation: re-download the file, inspect disk/HDFS health, and validate checksums","If format versions may differ, read a version/length header first and only readFully for the announced size","Handle the exception where partial data is legal (e.g. tail of a mutable file) by reading incrementally instead of readFully"],"exampleFix":"// before\nbyte[] header = new byte[8];\nIOUtils.readFully(in, header, 0, 8); // assumes writer always sends 8 bytes\n\n// after\nbyte[] header = new byte[8];\ntry {\n    IOUtils.readFully(in, header, 0, 8);\n} catch (IOException e) {\n    throw new IOException(\"Corrupt record at offset \" + offset + \": \" + e.getMessage(), e);\n}","handlingStrategy":"try-catch","validationCode":"if (in.available() < expectedLen && strict) throw new IllegalStateException(\"Input likely truncated\");","typeGuard":null,"tryCatchPattern":"try {\n    IOUtils.readFully(in, buf, 0, len);\n} catch (IOException e) {\n    throw new IOException(\"Corrupt/truncated input, expected \" + len + \" bytes: \" + e.getMessage(), e);\n}","preventionTips":["Length-prefix and checksum payloads so truncation is detected deterministically","Validate file sizes before parsing fixed-size records","Re-download rather than retry-parse a truncated stream"],"tags":["io","stream","corruption","serialization"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}