MuntashirAkon/AppManager · error · IndexOutOfBoundsException

offs(" + offs + ") + len(" + len + ") > dest.length(" + dest

Error message

offs(" + offs + ") + len(" + len + ") > dest.length(" + dest.length + ").

What it means

read(byte[], int, int) throws IndexOutOfBoundsException when offs + len exceeds dest.length, i.e. the caller asked to fill more bytes than the buffer can hold at the given offset. This is a fail-fast bounds check identical to the one in InputStream's contract.

Source

Thrown at app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java:143

        throw new IOException("Stream closed");
    }

    /*
     * (non-Javadoc)
     *
     * @see java.io.InputStream#read(byte[], int, int)
     */
    @Override
    public int read(final byte[] dest, final int offs, final int len)
            throws IOException {
        if (offs < 0) {
            throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
        }
        if (len < 0) {
            throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
        }
        if (offs + len > dest.length) {
            throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
                    + len + ") > dest.length(" + dest.length + ").");
        }
        if (this.bin == null) {
            throw new IOException("Stream closed");
        }
        if (len == 0) {
            return 0;
        }

        final int hi = offs + len;
        int destOffs = offs;
        int b;
        while (destOffs < hi && ((b = read0()) >= 0)) {
            dest[destOffs++] = (byte) b;
            count(1);
        }

        return (destOffs == offs) ? -1 : (destOffs - offs);

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Compute len as dest.length - offs, or clamp: len = Math.min(len, dest.length - offs).
  2. Verify the buffer is large enough for the requested read before the call.
  3. Prefer the simpler read(byte[]) or a fixed-size chunk loop with len = buf.length and offs = 0.

Example fix

// before
int n = stream.read(buf, 10, buf.length);
// after
int n = stream.read(buf, 10, buf.length - 10);
Defensive patterns

Strategy: validation

Validate before calling

if (offs < 0 || len < 0 || offs + len > dest.length) {
    throw new IllegalArgumentException("bad buffer bounds: offs=" + offs + " len=" + len);
}

Type guard

static boolean fitsInBuffer(int offs, int len, byte[] dest) {
    return dest != null && offs >= 0 && len >= 0 && offs <= dest.length - len;
}

Try / catch

try {
    int n = in.read(dest, offs, len);
} catch (IndexOutOfBoundsException e) {
    // fix caller-side bounds; do not retry same args
}

Prevention

When it happens

Trigger: Calling read(dest, offs, len) where offs + len > dest.length, e.g. read(buf, 5, buf.length) or read(buf, 10, buf.length - 5).

Common situations: Reusing buffer code written for a different buffer size; passing offs without shrinking len accordingly; copying logic between byte[] buffers of different capacities.

Related errors


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