{"record":{"id":"ff79dfb7d2da0e99","repo":"TeamNewPipe/NewPipe","slug":"the-chunk-is-empty-or-invalid","errorCode":null,"errorMessage":"The chunk is empty or invalid","messagePattern":"The chunk is empty or invalid","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java","lineNumber":28,"sourceCode":"    private SharpStream source;\n    private final long offset;\n    private final long length;\n    private long position;\n\n    private long progressReport;\n    private final ProgressReport onProgress;\n\n    public ChunkFileInputStream(SharpStream target, long start, long end, ProgressReport callback) throws IOException {\n        source = target;\n        offset = start;\n        length = end - start;\n        position = 0;\n        onProgress = callback;\n        progressReport = REPORT_INTERVAL;\n\n        if (length < 1) {\n            source.close();\n            throw new IOException(\"The chunk is empty or invalid\");\n        }\n        if (source.length() < end) {\n            try {\n                throw new IOException(String.format(\"invalid file length. expected = %s  found = %s\", end, source.length()));\n            } finally {\n                source.close();\n            }\n        }\n\n        source.seek(offset);\n    }\n\n    /**\n     * Get absolute position on file\n     *\n     * @return the position\n     */\n    public long getFilePointer() {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java#L10-L46","documentation":"Thrown by ChunkFileInputStream's constructor when the requested chunk has a non-positive length (end - start < 1). The stream wraps a region [start, end) of a SharpStream for ranged reading; a length less than 1 means start >= end, i.e. an empty or inverted byte range. The constructor closes the source and throws to prevent constructing a useless/corrupt stream.","triggerScenarios":"new ChunkFileInputStream(target, start, end, callback) is called with start >= end (length = end - start < 1). Happens when a download's chunk allocation logic computes a zero-size or negative range, when a server's Content-Range header yields end <= start, or when the total file size is smaller than the number of requested chunks.","commonSituations":"Multi-threaded download where the file size divides evenly leaving a final zero-length chunk; a Content-Range parsing bug that swaps start/end; requesting a chunk beyond EOF so end is clamped below start; integer overflow in chunk-size arithmetic.","solutions":["Validate start < end before constructing each ChunkFileInputStream in the chunk-allocation logic.","Ensure the last chunk in a multi-part download is skipped when the remaining bytes are zero.","Check the server's Content-Range/Content-Length headers and reject ranges where end <= start.","Guard against division that yields more chunks than bytes."],"exampleFix":"// before\nnew ChunkFileInputStream(stream, start, end, callback);\n\n// after — skip empty trailing chunks\nif (end <= start) {\n    return; // no bytes in this chunk, skip\n}\nnew ChunkFileInputStream(stream, start, end, callback);","handlingStrategy":"validation","validationCode":"// Validate chunk bounds before constructing the stream:\nif (end <= start) {\n    // skip empty chunk\n    return null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    ChunkFileInputStream in = new ChunkFileInputStream(stream, start, end, callback);\n} catch (IOException e) {\n    if (\"The chunk is empty or invalid\".equals(e.getMessage())) {\n        // skip this zero-length chunk in the download loop\n        continue;\n    } else throw e;\n}","preventionTips":["Compute chunk sizes so the last chunk is skipped if zero bytes remain.","Always enforce start < end in chunk-allocation arithmetic.","Validate Content-Range header values before using them as chunk bounds."],"tags":["io","download","chunking","byte-range","validation"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}