{"record":{"id":"5f77fbc87b659173","repo":"MuntashirAkon/AppManager","slug":"entry-currname-closed-at-currbytes-before-the-currsize-bytes","errorCode":null,"errorMessage":"Entry '${currName}' closed at '${currBytes}' before the '${currSize}' bytes specified in the header were written","messagePattern":"Entry '(.+?)' closed at '(.+?)' before the '(.+?)' bytes specified in the header were written","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java","lineNumber":414,"sourceCode":"    /**\n     * Close an entry. This method MUST be called for all file entries that contain data. The reason\n     * is that we must buffer data written to the stream in order to satisfy the buffer's record\n     * based writes. Thus, there may be data fragments still being assembled that must be written to\n     * the output stream before this entry is closed and the next entry written.\n     *\n     * @throws IOException on error\n     */\n    @Override\n    public void closeArchiveEntry() throws IOException {\n        if (finished) {\n            throw new IOException(\"Stream has already been finished\");\n        }\n        if (!haveUnclosedEntry) {\n            throw new IOException(\"No current entry to close\");\n        }\n        out.flushBlock();\n        if (currBytes < currSize) {\n            throw new IOException(\"Entry '\" + currName + \"' closed at '\"\n                + currBytes\n                + \"' before the '\" + currSize\n                + \"' bytes specified in the header were written\");\n        }\n        recordsWritten += (int) (currSize / RECORD_SIZE);\n        if (0 != currSize % RECORD_SIZE) {\n            recordsWritten++;\n        }\n        haveUnclosedEntry = false;\n    }\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.","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java#L396-L432","documentation":"closeArchiveEntry verifies that the number of bytes written for the current entry equals the size declared in the tar header (setSize). If fewer bytes were written (currBytes < currSize), the archive would contain an entry whose data does not match its header, so the library throws with details of the shortfall.","triggerScenarios":"Closing an entry after writing fewer bytes than TarArchiveEntry.getSize(); passing a smaller InputStream to copy than the declared size; setting an inflated size manually before writing.","commonSituations":"Hard-coded entry sizes out of sync with actual file content; interrupted file reads; computing size with one encoding/unit and writing with another; copying from a truncated source stream.","solutions":["Set entry size from the actual data: entry.setSize(data.length) or file.length(), and write exactly that many bytes.","Copy the whole source stream and verify byte counts before closing.","If the source may be truncated, read it fully into memory or adjust the size before putArchiveEntry (size is fixed once the header is written)."],"exampleFix":"// before\nentry.setSize(1024); // guessed\ntar.putArchiveEntry(entry);\ntar.write(data); // data.length == 900\n// after\nentry.setSize(data.length);\ntar.putArchiveEntry(entry);\ntar.write(data);\ntar.closeArchiveEntry();","handlingStrategy":"validation","validationCode":"long actual = data.length; // or Files.size(path)\nif (actual != entry.getSize()) entry.setSize(actual);","typeGuard":null,"tryCatchPattern":"try {\n    tar.closeArchiveEntry();\n} catch (IOException e) {\n    if (e.getMessage().contains(\"before the '\")) {\n        // entry under-written: mark archive invalid, rewrite with correct size\n    } else throw e;\n}","preventionTips":["Always setSize from the real data length","Count bytes copied and compare to entry size","Avoid hard-coded sizes","Read sources fully (or verify availability) before writing the header"],"tags":["tar","entry-size","data-truncation","header-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-23T08:17:48.524Z"}