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 the read side of Record's serialization buffer (an InputStream view over the record's memory). It is an IndexOutOfBoundsException raised before any bytes are copied: the caller supplied a target byte array b where the remaining space after the offset (b.length - off) is smaller than the requested read length len. The library validates arguments eagerly instead of letting System.arraycopy fail deeper in.

Source

Thrown at flink-core/src/main/java/org/apache/flink/types/Record.java:1587

            skipBytes(numBytes);
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            if (b == null) {
                throw new NullPointerException("Byte array b cannot be null.");
            }

            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.memory, this.position, b, off, toRead);
                this.position += toRead;

                return toRead;
            }
        }

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

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Size the target array to at least off + len before the call
  2. Clamp the request: int n = Math.min(len, b.length - off);
  3. Check the arguments up front and reject/log malformed requests instead of catching the exception

Example fix

// before
byte[] buf = new byte[64];
in.read(buf, 0, 128); // IndexOutOfBoundsException

// after
byte[] buf = new byte[128];
in.read(buf, 0, 128);
Defensive patterns

Strategy: validation

Validate before calling

if (b == null) throw new NullPointerException("b");
if (off < 0 || len < 0 || b.length - off < len) {
    throw new IllegalArgumentException("read range [" + off + "," + (off + len) + ") exceeds buffer " + b.length);
}

Try / catch

catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException("Malformed read request", e); }

Prevention

When it happens

Trigger: Calling Record's deserialization input view read(byte[] b, int off, int len) (or an InputStream wrapper over it) with len larger than b.length - off, e.g. read(buf, 10, 100) on a 50-byte buffer; also any off > b.length with a positive len.

Common situations: Reusable scratch buffers that were shrunk or allocated with the wrong size between records; off/len swapped by mistake; code that assumes read fills the whole array regardless of remaining capacity.

Related errors


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