{"record":{"id":"b4a7747ab6446f23","repo":"MuntashirAkon/AppManager","slug":"stream-is-not-in-the-bzip2-format","errorCode":null,"errorMessage":"Stream is not in the BZip2 format","messagePattern":"Stream is not in the BZip2 format","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java","lineNumber":240,"sourceCode":"\n    private boolean init(final boolean isFirstStream) throws IOException {\n        if (null == bin) {\n            throw new IOException(\"No InputStream\");\n        }\n\n        if (!isFirstStream) {\n            bin.clearBitCache();\n        }\n\n        final int magic0 = readNextByte(this.bin);\n        if (magic0 == -1 && !isFirstStream) {\n            return false;\n        }\n        final int magic1 = readNextByte(this.bin);\n        final int magic2 = readNextByte(this.bin);\n\n        if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') {\n            throw new IOException(isFirstStream\n                    ? \"Stream is not in the BZip2 format\"\n                    : \"Garbage after a valid BZip2 stream\");\n        }\n\n        final int blockSize = readNextByte(this.bin);\n        if ((blockSize < '1') || (blockSize > '9')) {\n            throw new IOException(\"BZip2 block size is invalid\");\n        }\n\n        this.blockSize100k = blockSize - '0';\n\n        this.computedCombinedCRC = 0;\n\n        return true;\n    }\n\n    private void initBlock() throws IOException {\n        final BitInputStream bin = this.bin;","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java#L222-L258","documentation":"init() reads the first three bytes and throws IOException(\"Stream is not in the BZip2 format\") when they are not the magic 'BZh'. The library refuses to parse data that is not a BZip2 stream rather than returning garbage.","triggerScenarios":"Passing a non-BZip2 file (plain text, gzip, zip, tar) to BZip2CompressorInputStream, or a stream whose position is not at the start of a .bz2 stream.","commonSituations":"Wrong decompressor chosen by extension guess; server sends a non-bzip2 response body; file was renamed to .bz2 without actually being compressed; reading a concatenated/next stream where only garbage remains.","solutions":["Verify the input is really bzip2 by checking the first three bytes are 'B','Z','h' before wrapping.","Use CompressorStreamFactory with auto-detection (CompressorStreamFactory.detect) instead of hardcoding bzip2.","Check the InputStream position — skip any leading container/protocol headers before decompressing.","Confirm the file was actually compressed with bzip2 (e.g. `file data.bz2` shows 'bzip2 compressed data')."],"exampleFix":"// before\nInputStream in = new BZip2CompressorInputStream(new FileInputStream(anyFile));\n// after\nPushbackInputStream pb = new PushbackInputStream(new FileInputStream(anyFile), 3);\nbyte[] magic = new byte[3];\nif (pb.read(magic) != 3 || magic[0] != 'B' || magic[1] != 'Z' || magic[2] != 'h') {\n    pb.unread(magic);\n    throw new IllegalArgumentException(\"Not a bzip2 stream\");\n}\nInputStream in = new BZip2CompressorInputStream(pb);","handlingStrategy":"validation","validationCode":"byte[] head = new byte[3];\nif (in.read(head) != 3 || head[0] != 'B' || head[1] != 'Z' || head[2] != 'h') {\n    throw new IllegalArgumentException(\"not a bzip2 stream\");\n}\n// then wrap (use PushbackInputStream to unread the 3 bytes)","typeGuard":"static boolean looksLikeBzip2(byte[] head) {\n    return head != null && head.length >= 3 && head[0] == 'B' && head[1] == 'Z' && head[2] == 'h';\n}","tryCatchPattern":"try {\n    InputStream in = new BZip2CompressorInputStream(raw);\n} catch (IOException e) {\n    // sniff real format via CompressorStreamFactory.detect and retry\n}","preventionTips":["Use CompressorStreamFactory.detect() instead of hardcoding the format.","Sniff magic bytes before decompressing untrusted input.","Confirm file type out-of-band (`file`, content-type headers)."],"tags":["io","format-detection","magic-bytes"],"backgroundTag":"invalid-stream-format","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}