{"record":{"id":"9147b036a986bf6b","repo":"MuntashirAkon/AppManager","slug":"unexpected-end-of-stream","errorCode":null,"errorMessage":"Unexpected end of stream","messagePattern":"Unexpected end of stream","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java","lineNumber":372,"sourceCode":"            try {\n                inShadow.close();\n            } finally {\n                this.data = null;\n                this.bin = null;\n            }\n        }\n    }\n\n    /**\n     * read bits from the input stream\n     * @param n the number of bits to read, must not exceed 32?\n     * @return the requested bits combined into an int\n     * @throws IOException\n     */\n    private static int bsR(final BitInputStream bin, final int n) throws IOException {\n        final long thech = bin.readBits(n);\n        if (thech < 0) {\n            throw new IOException(\"Unexpected end of stream\");\n        }\n        return (int) thech;\n    }\n\n    private static boolean bsGetBit(final BitInputStream bin) throws IOException {\n        return bsR(bin, 1) != 0;\n    }\n\n    private static char bsGetUByte(final BitInputStream bin) throws IOException {\n        return (char) bsR(bin, 8);\n    }\n\n    private static int bsGetInt(final BitInputStream bin) throws IOException {\n        return bsR(bin, 32);\n    }\n\n    private static void checkBounds(final int checkVal, final int limitExclusive, final String name)\n            throws IOException {","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java#L354-L390","documentation":"bsR() reads n bits via the underlying BitInputStream and throws IOException(\"Unexpected end of stream\") when readBits returns a negative EOF marker. The stream ended while the decoder still expected structural data (block header, selectors, Huffman tables, or CRC).","triggerScenarios":"A truncated .bz2 file (download cut off, write not flushed/closed); the underlying InputStream hit EOF mid-block or before the stream trailer; feeding a stream that is still being written concurrently.","commonSituations":"Partially downloaded files; producer not closing the OutputStream so the compressor never flushed the trailer; reading over an unstable socket connection; reading a file currently being written by another process.","solutions":["Ensure the compressed input is complete: wait for the producer to finish and close, or re-download fully.","Verify the file with `bzip2 -t` (reports 'unexpected end of file' for truncated archives).","Always close() the compressor OutputStream on the producer side so the end-of-stream trailer is written.","Catch IOException and handle truncation explicitly if partial data is acceptable, but do not retry reading the same truncated source."],"exampleFix":"// before\n// producer side: forgot to close, trailer missing\ncompressorOutputStream.finish(); // incomplete: no close()\n// after\ntry (BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(fileOut)) {\n    out.write(data);\n} // close() writes the stream trailer","handlingStrategy":"try-catch","validationCode":"// check completeness before decompress\nlong expected = manifest.get(name);\nif (file.length() < expected) throw new IOException(\"truncated download: \" + file);","typeGuard":null,"tryCatchPattern":"try {\n    readAll(in);\n} catch (IOException e) {\n    if (\"Unexpected end of stream\".equals(e.getMessage())) {\n        // source truncated: re-download or wait for producer to finish\n        throw new TruncatedInputException(e);\n    }\n    throw e;\n}","preventionTips":["Only read .bz2 files after the producer has closed them.","Close compressor OutputStreams (try-with-resources) so trailers are written.","Retry truncated downloads from scratch; do not resume bitstream reads."],"tags":["io","truncated-input","eof"],"backgroundTag":"unexpected-end-of-stream","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"}