TeamNewPipe/NewPipe · error · IOException

The chunk is empty or invalid

Error message

The chunk is empty or invalid

What it means

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.

Source

Thrown at app/src/main/java/us/shandian/giga/io/ChunkFileInputStream.java:28

    private SharpStream source;
    private final long offset;
    private final long length;
    private long position;

    private long progressReport;
    private final ProgressReport onProgress;

    public ChunkFileInputStream(SharpStream target, long start, long end, ProgressReport callback) throws IOException {
        source = target;
        offset = start;
        length = end - start;
        position = 0;
        onProgress = callback;
        progressReport = REPORT_INTERVAL;

        if (length < 1) {
            source.close();
            throw new IOException("The chunk is empty or invalid");
        }
        if (source.length() < end) {
            try {
                throw new IOException(String.format("invalid file length. expected = %s  found = %s", end, source.length()));
            } finally {
                source.close();
            }
        }

        source.seek(offset);
    }

    /**
     * Get absolute position on file
     *
     * @return the position
     */
    public long getFilePointer() {

View on GitHub (pinned to 9e8be09156)

Solutions

  1. Validate start < end before constructing each ChunkFileInputStream in the chunk-allocation logic.
  2. Ensure the last chunk in a multi-part download is skipped when the remaining bytes are zero.
  3. Check the server's Content-Range/Content-Length headers and reject ranges where end <= start.
  4. Guard against division that yields more chunks than bytes.

Example fix

// before
new ChunkFileInputStream(stream, start, end, callback);

// after — skip empty trailing chunks
if (end <= start) {
    return; // no bytes in this chunk, skip
}
new ChunkFileInputStream(stream, start, end, callback);
Defensive patterns

Strategy: validation

Validate before calling

// Validate chunk bounds before constructing the stream:
if (end <= start) {
    // skip empty chunk
    return null;
}

Try / catch

try {
    ChunkFileInputStream in = new ChunkFileInputStream(stream, start, end, callback);
} catch (IOException e) {
    if ("The chunk is empty or invalid".equals(e.getMessage())) {
        // skip this zero-length chunk in the download loop
        continue;
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14). Data as JSON: /api/errors/ff79dfb7d2da0e99. Report an issue: GitHub.