apache/flink · error · IndexOutOfBoundsException

Byte array does not provide enough space to store requested

Error message

Byte array does not provide enough space to store requested data.

What it means

Thrown by DataInputDeserializer.read(byte[], int off, int len) when the destination array cannot hold the requested bytes: b.length - off < len. The destination is too small for the read.

Source

Thrown at flink-core/src/main/java/org/apache/flink/core/memory/DataInputDeserializer.java:393

        if (skippedBytes < numBytes) {
            throw new EOFException("Could not skip " + numBytes + " bytes.");
        }
    }

    @Override
    public int read(@Nonnull byte[] b, int off, int len) throws IOException {

        if (off < 0) {
            throw new IndexOutOfBoundsException("Offset cannot be negative.");
        }

        if (len < 0) {
            throw new IndexOutOfBoundsException("Length cannot be negative.");
        }

        if (b.length - off < len) {
            throw new IndexOutOfBoundsException(
                    "Byte array does not provide enough space to store requested data" + ".");
        }

        if (this.position >= this.end) {
            return len == 0 ? 0 : -1;
        } else {
            int toRead = Math.min(this.end - this.position, len);
            System.arraycopy(this.buffer, this.position, b, off, toRead);
            this.position += toRead;

            return toRead;
        }
    }

    @Override
    public int read(@Nonnull byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Size the destination array to at least off + len before calling read.
  2. Use read(byte[]) with a correctly sized array.
  3. Derive the destination length from the record's declared size, not a fixed constant.

Example fix

// before
byte[] dst = new byte[8];
input.read(dst, 0, recordLen); // recordLen > 8

// after
byte[] dst = new byte[recordLen];
input.read(dst, 0, recordLen);
Defensive patterns

Strategy: validation

Validate before calling

if (off < 0 || len < 0 || b.length - off < len) {
    throw new IndexOutOfBoundsException("dst too small: len=" + len + ", off=" + off + ", cap=" + b.length);
}
input.read(b, off, len);

Prevention

When it happens

Trigger: Calling read(b, off, len) where b.length - off < len (e.g. reading 16 bytes into an 8-byte array, or with a large offset that leaves insufficient room).

Common situations: Destination buffer sized too small for the record; an offset that eats into available space; mis-sizing a read buffer relative to the declared record length.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/7aec0400424e42dd. Report an issue: GitHub.