MuntashirAkon/AppManager · error · IOException

count must not be negative or greater than " +…

Error message

count must not be negative or greater than " + MAXIMUM_CACHE_SIZE

What it means

BitInputStream.readBits validates the requested bit count before reading: count must be between 0 and MAXIMUM_CACHE_SIZE (63, the max bits fitting in a long cache). Out-of-range counts throw an IOException because the implementation cannot serve them.

Solutions

  1. Request at most 63 bits per call; split a 64-bit read into two calls and combine results
  2. Validate/clamp the count parameter before calling readBits
  3. Read a full byte via readBits(8) and compose larger values manually

Example fix

// before
long v = bitStream.readBits(64); // IOException
// after
long lo = bitStream.readBits(32);
long hi = bitStream.readBits(32);
long v = (hi << 32) | lo;
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0 || count > 63) throw new IllegalArgumentException("readBits count must be 0..63, got " + count);

Try / catch

try { long v = bits.readBits(count); } catch (IOException e) { if (e.getMessage().startsWith("count must not be")) throw new IllegalArgumentException(e.getMessage(), e); throw e; }

Prevention

When it happens

Trigger: Calling readBits(count) with a negative count or count > 63, e.g. requesting 64+ bits at once or passing an unvalidated field-width from a header.

Common situations: Attempting to read 64-bit values in one call from a bit-oriented format; passing a signed byte as a bit count; malformed archive format metadata specifying an oversized field.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/d11f95568eaf78d9. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/org/apache/commons/compress/utils/BitInputStream.java:68

     */
    public void clearBitCache() {
        bitsCached = 0;
        bitsCachedSize = 0;
    }

    /**
     * Returns at most 63 bits read from the underlying stream.
     *
     * @param count the number of bits to read, must be a positive
     * number not bigger than 63.
     * @return the bits concatenated as a long using the stream's byte order.
     *         -1 if the end of the underlying stream has been reached before reading
     *         the requested number of bits
     * @throws IOException on error
     */
    public long readBits(final int count) throws IOException {
        if (count < 0 || count > MAXIMUM_CACHE_SIZE) {
            throw new IOException("count must not be negative or greater than " + MAXIMUM_CACHE_SIZE);
        }
        if (ensureCache(count)) {
            return -1;
        }

        if (bitsCachedSize < count) {
            return processBitsGreater57(count);
        }
        return readCachedBits(count);
    }

    /**
     * Returns the number of bits that can be read from this input
     * stream without reading from the underlying input stream at all.
     * @return estimate of the number of bits that can be read without reading from the underlying stream
     * @since 1.16
     */
    public int bitsCached() {

View on GitHub (pinned to 0152f468fc)