quarkusio/quarkus · error · IllegalArgumentException

Count (${count}) must be >= 0: ${file}

Error message

Count (${count}) must be >= 0: ${file}

What it means

IllegalArgumentException from the FilePart constructor: a partial-file response was requested with a negative byte count. The constructor validates that the file exists, is a regular readable file, and that offset/count are non-negative and within the file; here the count argument failed the >= 0 check.

Source

Thrown at independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/FilePart.java:42

    /**
     * Create a new partial {@link File} object.
     *
     * @param file The file to send
     * @param offset The starting byte of the file (must be >= 0)
     * @param count The number of bytes to send (must be >= 0 and offset+count <= file size)
     */
    public FilePart(File file, long offset, long count) {
        if (!file.exists())
            throw new IllegalArgumentException("File does not exist: " + file);
        if (!file.isFile())
            throw new IllegalArgumentException("File is not a regular file: " + file);
        if (!file.canRead())
            throw new IllegalArgumentException("File cannot be read: " + file);
        if (offset < 0)
            throw new IllegalArgumentException("Offset (" + offset + ") must be >= 0: " + file);
        if (count < 0)
            throw new IllegalArgumentException("Count (" + count + ") must be >= 0: " + file);
        if ((offset + count) > file.length())
            throw new IllegalArgumentException(
                    "Offset + count (" + (offset + count) + ") larger than file size (" + file.length() + "): " + file);
        this.file = file;
        this.offset = offset;
        this.count = count;
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Clamp count with Math.max(0, remaining) before constructing the part.
  2. Fix the remaining-bytes computation to be monotonic (never decrement below zero).
  3. Validate computed sizes before creating parts.

Example fix

// before
long remaining = total - sent;
new FilePart(file, offset, remaining); // can be negative
// after
long remaining = Math.max(0, total - sent);
if (remaining > 0) new FilePart(file, offset, remaining);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new IllegalStateException("Invalid count " + count + " for " + file);
FilePart part = new FilePart(file, offset, count);

Type guard

static boolean isValidRange(File f, long offset, long count) {
    return f != null && offset >= 0 && count >= 0 && offset + count <= f.length();
}

Try / catch

try {
    return new FilePart(file, offset, count);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Count (")) {
        log.warn("Bad count {} for {}, skipping empty part", count, file);
        return null;
    }
    throw e;
}

Prevention

When it happens

Trigger: new FilePart(file, offset, count) with count < 0, typically from arithmetic like remaining = total - alreadySent going negative.

Common situations: Chunked upload logic that subtracts more than the remaining size; misparsed Content-Length or range values; sending the same chunk twice so 'remaining' underflows.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/e42c8f44d912e4e3. Report an issue: GitHub.