MuntashirAkon/AppManager · error · java.io.IOException

Unexpected magic

Error message

Unexpected magic ${magic}

What it means

setInput validates the 4-byte magic header of the binary XML stream against PROTOCOL_MAGIC_VERSION_0. If the first four bytes differ, the stream is not a (v0) binary XML document and an IOException "Unexpected magic <hex>" is thrown.

Solutions

  1. Verify the InputStream actually contains binary XML (check the first bytes / how the file was produced).
  2. Use a standard XmlPullParser for plain-text XML documents.
  3. Ensure the stream position is 0 so the magic bytes are read first; rewind or re-open the stream.
  4. Upgrade the library if the file was written with a newer binary XML protocol version.

Example fix

// before
parser.setInput(new FileInputStream("doc.xml"), null); // plain XML -> bad magic
// after
if (isBinaryXml(file)) parser.setInput(new FileInputStream("doc.binxml"), null);
else parser.setInput(new InputStreamReader(new FileInputStream("doc.xml"), StandardCharsets.UTF_8));
Defensive patterns

Strategy: validation

Validate before calling

byte[] head = new byte[4];
InputStream in = new FileInputStream(file);
if (in.read(head) != 4 || !Arrays.equals(head, BinaryXmlPullParser.PROTOCOL_MAGIC_VERSION_0)) {
    // not binary XML — use a text XML parser
    in.close();
}

Try / catch

try {
    parser.setInput(is, null);
} catch (IOException e) {
    // wrong format: fall back to text XML parser or report corrupt file
}

Prevention

When it happens

Trigger: Calling setInput(InputStream, encoding) on a stream whose first 4 bytes are not the binary XML magic: plain-text XML, ZIP/APK data, a truncated or corrupted file, or a binary XML produced by a different/protocol-version writer.

Common situations: Pointing the parser at a regular XML file by mistake; reading a resource that was compressed or transformed; version drift where the writer emitted a newer magic than this parser understands; reading from an offset that skips the header.

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/bca8e84179757abf. Report an issue: GitHub.

Appendix: source

Thrown at libcore/compat/src/main/java/io/github/muntashirakon/compat/xml/BinaryXmlPullParser.java:106

        mIn = FastDataInput.obtainUsing4ByteSequences(is);

        mCurrentToken = START_DOCUMENT;
        mCurrentDepth = 0;
        mCurrentName = null;
        mCurrentText = null;

        mAttributeCount = 0;
        mAttributes = new Attribute[8];
        for (int i = 0; i < mAttributes.length; i++) {
            mAttributes[i] = new Attribute();
        }

        try {
            final byte[] magic = new byte[4];
            mIn.readFully(magic);
            if (!Arrays.equals(magic, PROTOCOL_MAGIC_VERSION_0)) {
                throw new IOException("Unexpected magic " + bytesToHexString(magic));
            }

            // We're willing to immediately consume a START_DOCUMENT if present,
            // but we're okay if it's missing
            if (peekNextExternalToken() == START_DOCUMENT) {
                consumeToken();
            }
        } catch (IOException e) {
            throw new XmlPullParserException(e.toString());
        }
    }

    @Override
    public void setInput(Reader in) throws XmlPullParserException {
        throw new UnsupportedOperationException();
    }

    @Override

View on GitHub (pinned to 0152f468fc)