{"record":{"id":"ac2972536b77b338","repo":"pxb1988/dex2jar","slug":"generated-different-file-size-planned-size","errorCode":null,"errorMessage":"generated different file size, planned \" + size + \", but is \" + buffer.position()","messagePattern":"generated different file size, planned \" \\+ size \\+ \", but is \" \\+ buffer\\.position\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"dex-writer/src/main/java/com/googlecode/d2j/dex/writer/DexFileWriter.java","lineNumber":242,"sourceCode":"    public byte[] toByteArray() {\n\n        // init structure for writing\n        buildMapListItem();\n\n        // place all item into file, we can know the size now\n        final int size = place();\n\n        ByteBuffer buffer = ByteBuffer.allocate(size);\n        DataOut out = new ByteBufferOut(buffer);\n\n        if (DEBUG) {\n            out = wrapDumpOut(out);\n        }\n        // write it\n        write(out);\n\n        if (size != buffer.position()) {\n            throw new RuntimeException(\"generated different file size, planned \" + size + \", but is \" + buffer.position());\n        }\n\n        // update the CRC/ sha1 checksum in dex header\n        updateChecksum(buffer, size);\n\n        return buffer.array();\n    }\n\n    public static void updateChecksum(ByteBuffer buffer, int size) {\n        byte[] data = buffer.array();\n        MessageDigest digest;\n        try {\n            digest = MessageDigest.getInstance(\"SHA-1\");\n        } catch (NoSuchAlgorithmException e) {\n            throw new AssertionError();\n        }\n\n        digest.update(data, 32, size - 32);","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/pxb1988/dex2jar/blob/b5bda4fb4935ae8b3869b422454ae3b3896c7bc1/dex-writer/src/main/java/com/googlecode/d2j/dex/writer/DexFileWriter.java#L224-L260","documentation":"DexFileWriter.toByteArray() pre-computes the dex file size, writes all sections, then asserts the output buffer position matches the plan. A mismatch means internal size bookkeeping (padding, offsets, item sizes) drifted from what was written, so it throws before fixing the header checksum.","triggerScenarios":"Calling toDex()/toByteArray() after adding items whose write() output does not match their calculated size (custom BaseItem subclasses, wrong alignment padding, duplicate/unregistered items).","commonSituations":"Extending the dex-writer with custom items or patched section writers; library bugs after adding unusual constructs (e.g. very large string/type pools, annotation with odd-length values).","solutions":["Check that every custom item's calcSize/write and padding logic matches BaseItem conventions","Update the dex-writer version — this is usually an internal invariant bug","Dump the buffer (wrapDumpOut path) and compare section offsets against the plan to find the offending section"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { byte[] dex = dexFileWriter.toByteArray(); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"generated different file size\")) { /* enable dump, isolate offending item */ } throw e; }","preventionTips":["Never mutate items after planning offsets","Follow BaseItem size/padding conventions for custom items","Keep dex-writer up to date; regression-test full toDex() output"],"tags":["dex","writer","size-mismatch"],"backgroundTag":"internal-invariant-violation","analyzedSha":"b5bda4fb4935ae8b3869b422454ae3b3896c7bc1","analyzedAt":"2026-09-08T00:44:01.258Z","contentChangedAt":"2026-09-08T00:44:01.258Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}