{"record":{"id":"8957d1a8abb9d768","repo":"MuntashirAkon/AppManager","slug":"request-to-write-numtowrite-bytes-exceeds-size-in-header-of","errorCode":null,"errorMessage":"Request to write '${numToWrite}' bytes exceeds size in header of '${currSize}' bytes for entry '${currName}'","messagePattern":"Request to write '(.+?)' bytes exceeds size in header of '(.+?)' bytes for entry '(.+?)'","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java","lineNumber":442,"sourceCode":"    }\n\n    /**\n     * Writes bytes to the current tar archive entry. This method is aware of the current entry and\n     * will throw an exception if you attempt to write bytes past the length specified for the\n     * current entry.\n     *\n     * @param wBuf The buffer to write to the archive.\n     * @param wOffset The offset in the buffer from which to get bytes.\n     * @param numToWrite The number of bytes to write.\n     * @throws IOException on error\n     */\n    @Override\n    public void write(final byte[] wBuf, final int wOffset, final int numToWrite) throws IOException {\n        if (!haveUnclosedEntry) {\n            throw new IllegalStateException(\"No current tar entry\");\n        }\n        if (currBytes + numToWrite > currSize) {\n            throw new IOException(\"Request to write '\" + numToWrite\n                + \"' bytes exceeds size in header of '\"\n                + currSize + \"' bytes for entry '\"\n                + currName + \"'\");\n        }\n        out.write(wBuf, wOffset, numToWrite);\n        currBytes += numToWrite;\n    }\n\n    /**\n     * Writes a PAX extended header with the given map as contents.\n     *\n     * @since 1.4\n     */\n    void writePaxHeaders(final TarArchiveEntry entry,\n        final String entryName,\n        final Map<String, String> headers) throws IOException {\n        String name = \"./PaxHeaders.X/\" + stripTo7Bits(entryName);\n        if (name.length() >= TarConstants.NAMELEN) {","sourceCodeStart":424,"sourceCodeEnd":460,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java#L424-L460","documentation":"TarArchiveOutputStream.write() tracks how many bytes have been written for the current entry (currBytes) against the entry's declared size in its tar header (currSize). If a single write call would push the total beyond currSize, the stream refuses to write, since a tar entry's content length is fixed in its header and cannot grow. This protects the archive from corruption: subsequent entries would otherwise be misaligned with block boundaries.","triggerScenarios":"Calling write(byte[], int, int) with more bytes than remain for the current entry — i.e. currBytes + numToWrite > currSize — while an entry opened via putArchiveEntry is still unclosed. Happens when the caller writes data that differs in length from the size the TarArchiveEntry was created with.","commonSituations":"Writing a byte array that was mutated or re-read after creating the TarArchiveEntry from a File (file changed size between entry creation and write); reusing an entry object with different content; writing a String's bytes whose length differs from an earlier computed size; wrapping streams that report size at creation but are later updated.","solutions":["Ensure the total bytes written match the size returned by TarArchiveEntry.getSize(): buffer the content and create the entry from the exact same data you write.","Recreate the TarArchiveEntry immediately before writing (e.g. new TarArchiveEntry(file, name)) so the header size reflects current file content.","If streaming unknown-length data, write it to a temp file or ByteArrayOutputStream first, size the entry from that, then write the bytes.","Check for double-writes of the same data (e.g. calling write() twice with overlapping buffers)."],"exampleFix":"// before\nTarArchiveEntry entry = new TarArchiveEntry(file, file.getName());\ntarOut.putArchiveEntry(entry);\ntarOut.write(getUpdatedContent()); // length may differ from file size\n// after\nbyte[] content = getUpdatedContent();\nTarArchiveEntry entry = new TarArchiveEntry(file, file.getName());\nentry.setSize(content.length); // header size matches actual write\ntarOut.putArchiveEntry(entry);\ntarOut.write(content);","handlingStrategy":"validation","validationCode":"byte[] content = readAllBytes(file);\nif (content.length != entry.getSize()) {\n    entry.setSize(content.length); // realign header before writing\n}\ntarOut.putArchiveEntry(entry);\ntarOut.write(content);\ntarOut.closeArchiveEntry();","typeGuard":null,"tryCatchPattern":"try {\n    tarOut.putArchiveEntry(entry);\n    tarOut.write(data);\n    tarOut.closeArchiveEntry();\n} catch (IOException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"exceeds size in header\")) {\n        // rebuild entry with correct size and retry\n    } else {\n        throw e;\n    }\n}","preventionTips":["Create the TarArchiveEntry from the exact data you will write, right before writing it.","Track written byte count against entry.getSize() in your archiving loop.","Never reuse entry objects across different content."],"tags":["tar","archive","io","size-mismatch"],"backgroundTag":"payload-too-large","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}