{"record":{"id":"7e68671d3e4937a2","repo":"MuntashirAkon/AppManager","slug":"premature-end-of-data","errorCode":null,"errorMessage":"Premature end of data","messagePattern":"Premature end of data","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/utils/ByteUtils.java","lineNumber":97,"sourceCode":"    }\n\n    /**\n     * Reads the given number of bytes from the given stream as a little endian long.\n     * @param in the stream to read from\n     * @param length the number of bytes representing the value\n     * @return the number read\n     * @throws IllegalArgumentException if len is bigger than eight\n     * @throws IOException if reading fails or the stream doesn't\n     * contain the given number of bytes anymore\n     */\n    public static long fromLittleEndian(final InputStream in, final int length) throws IOException {\n        // somewhat duplicates the ByteSupplier version in order to save the creation of a wrapper object\n        checkReadLength(length);\n        long l = 0;\n        for (int i = 0; i < length; i++) {\n            final long b = in.read();\n            if (b == -1) {\n                throw new IOException(\"Premature end of data\");\n            }\n            l |= (b << (i * 8));\n        }\n        return l;\n    }\n\n    /**\n     * Reads the given number of bytes from the given supplier as a little endian long.\n     *\n     * <p>Typically used by our InputStreams that need to count the\n     * bytes read as well.</p>\n     *\n     * @param supplier the supplier for bytes\n     * @param length the number of bytes representing the value\n     * @return the number read\n     * @throws IllegalArgumentException if len is bigger than eight\n     * @throws IOException if the supplier fails or doesn't supply the\n     * given number of bytes anymore","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/utils/ByteUtils.java#L79-L115","documentation":"ByteUtils.fromLittleEndian(InputStream,int) reads `length` bytes little-endian from a stream and throws this IOException when the stream ends (read() returns -1) before all requested bytes are consumed — i.e. the data is truncated.","triggerScenarios":"Calling fromLittleEndian(InputStream, length) when fewer than `length` bytes remain before EOF, e.g. reading a fixed-size header from a truncated or corrupt file.","commonSituations":"Parsing archive headers from truncated downloads; reading past the last entry in a stream; files cut off by transfer interruption.","solutions":["Check the stream has at least `length` bytes available before reading (or use DataInputStream.readFully with try/catch for EOF)","Verify the source file/stream integrity and re-transfer if truncated","Catch IOException and treat as end-of-data / corrupt input, aborting the parse"],"exampleFix":"// before\nlong size = ByteUtils.fromLittleEndian(in, 8); // throws on truncated stream\n// after\nbyte[] buf = new byte[8];\nint n = in.readNBytes(buf, 0, 8);\nif (n < 8) throw new TruncatedDataException(\"expected 8 bytes, got \" + n);\nlong size = ByteUtils.fromLittleEndian(buf);","handlingStrategy":"try-catch","validationCode":"if (in.available() > 0 && in.available() < length) { /* likely truncated: handle before reading */ }","typeGuard":null,"tryCatchPattern":"try { long v = ByteUtils.fromLittleEndian(in, length); } catch (IOException e) { if (e.getMessage().equals(\"Premature end of data\")) { throw new TruncatedArchiveException(\"stream ended before \" + length + \" bytes\"); } throw e; }","preventionTips":["Verify file size/transfer completeness (checksum) before parsing","Use readNBytes/readFully and check the returned count for clearer diagnostics","Expect EOF on last-entry boundaries and handle gracefully"],"tags":["io","truncated-data","little-endian"],"backgroundTag":"unexpected-eof","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}