pinpoint-apm/pinpoint · error · IllegalArgumentException

negative bufferSize

Error message

negative bufferSize

What it means

IOUtils.toByteArray copies an InputStream into a byte array using a caller-supplied buffer size. A negative bufferSize would make 'new byte[bufferSize]' throw NegativeArraySizeException with a confusing stack trace, so the method throws a clear IllegalArgumentException instead.

Solutions

  1. Pass a positive buffer size (or use the 1/2-arg overloads that apply DEFAULT_BUFFER_SIZE).
  2. Clamp the computed size: Math.max(bufferSize, DEFAULT_BUFFER_SIZE) or validate config >= 0 at load time.
  3. Fix the source of the negative value (config parsing or arithmetic underflow).

Example fix

// before
byte[] data = IOUtils.toByteArray(in, -1, true);
// after
int size = Math.max(configuredSize, IOUtils.DEFAULT_BUFFER_SIZE);
byte[] data = IOUtils.toByteArray(in, size, true);
Defensive patterns

Strategy: validation

Validate before calling

if (bufferSize < 0) {
    throw new IllegalArgumentException("bufferSize must be >= 0: " + bufferSize);
}

Type guard

boolean isValidBufferSize(int n) { return n >= 0; }

Try / catch

try {
    return IOUtils.toByteArray(in, bufferSize, true);
} catch (IllegalArgumentException e) {
    log.warn("Bad buffer size {}, falling back to default", bufferSize);
    return IOUtils.toByteArray(in);
}

Prevention

When it happens

Trigger: Calling IOUtils.toByteArray(inputStream, bufferSize, close) with bufferSize < 0, e.g. toByteArray(in, -1, true) or a bufferSize computed from a config/constant that is negative.

Common situations: Misconfigured buffer-size settings (negative value in properties/env), subtraction-based size computations that underflow, or copy-paste of the 3-arg overload expecting 0 to mean default.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pinpoint-apm/pinpoint@744c3d3075 (2026-09-07). Data as JSON: /api/errors/5b959679e9fd3553. Report an issue: GitHub.

Appendix: source

Thrown at commons/src/main/java/com/navercorp/pinpoint/common/util/IOUtils.java:52

    private static final int MAX_BUFFER_SIZE = 1024 * 1024;

    public static final int EOF = -1;

    private IOUtils() {
    }

    public static byte[] toByteArray(final InputStream inputStream) throws IOException {
        return toByteArray(inputStream, DEFAULT_BUFFER_SIZE, true);
    }

    public static byte[] toByteArray(final InputStream inputStream, boolean close) throws IOException {
        return toByteArray(inputStream, DEFAULT_BUFFER_SIZE, close);
    }

    public static byte[] toByteArray(final InputStream inputStream, int bufferSize, boolean close) throws IOException {
        Objects.requireNonNull(inputStream, "inputStream");
        if (bufferSize < 0) {
            throw new IllegalArgumentException("negative bufferSize");
        }

        bufferSize = calculateBufferSize(inputStream, bufferSize);
        final byte[] buffer = new byte[bufferSize];

        final byte[] readBuffer = bufferRead(inputStream, buffer);
        if (readBuffer != null) {
            return readBuffer;
        }

        ByteArrayOutputStream outputStream;
        try {
            outputStream = new ByteArrayOutputStream(buffer.length * 2);
            outputStream.write(buffer, 0, buffer.length);

            copy(inputStream, outputStream, buffer);

            outputStream.flush();

View on GitHub (pinned to 744c3d3075)