TooTallNate/Java-WebSocket · error · IllegalArgumentException

Cannot have length offset:

Error message

Cannot have length offset: 

What it means

encodeBytesToBytes validates its (source, off, len) slice before Base64-encoding. A negative length is meaningless for an array slice, so the library fails fast with IllegalArgumentException. This is a caller-contract violation, not a data corruption issue.

Solutions

  1. Check len >= 0 before calling encodeBytesToBytes and clamp or reject invalid values.
  2. If len is computed as a difference, ensure the end offset is >= the start offset (e.g. Math.max(0, end - start)).
  3. Catch IllegalArgumentException at the boundary if negative lengths are expected from untrusted input, and return a validation error to the caller instead.

Example fix

// before
int len = end - start;
byte[] out = Base64.encodeBytesToBytes(data, start, len);

// after
int len = Math.max(0, end - start);
if (end < start) throw new IllegalArgumentException("end must be >= start");
byte[] out = Base64.encodeBytesToBytes(data, start, len);
Defensive patterns

Strategy: validation

Validate before calling

if (data == null || off < 0 || len < 0 || off + len > data.length) {
    throw new IllegalArgumentException("invalid Base64 slice: off=" + off + " len=" + len);
}
byte[] out = Base64.encodeBytesToBytes(data, off, len);

Type guard

static boolean isValidSlice(byte[] a, int off, int len) {
    return a != null && off >= 0 && len >= 0 && off + len <= a.length;
}

Try / catch

try {
    out = Base64.encodeBytesToBytes(data, off, len);
} catch (IllegalArgumentException e) {
    // reject request: bad offset/length
    throw new BadRequestException(e.getMessage());
}

Prevention

When it happens

Trigger: Calling encodeBytesToBytes(source, off, len) (or a wrapper like encoded()) with len < 0, typically when len comes from an unvalidated subtraction such as (end - start) or a computed size.

Common situations: Computing length as difference of two offsets where end < start; passing -1 as a sentinel 'unknown length'; integer arithmetic on parsed config values that can go negative.

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 TooTallNate/Java-WebSocket@afeacbf8c0 (2026-09-09). Data as JSON: /api/errors/f2b61dca18d1bd8a. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/java_websocket/util/Base64.java:660

   * @throws IllegalArgumentException if source array is null, if source array, offset, or length
   *                                  are invalid
   * @see Base64#GZIP
   * @see Base64#DO_BREAK_LINES
   * @since 2.3.1
   */
  public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options)
      throws java.io.IOException {

    if (source == null) {
      throw new IllegalArgumentException("Cannot serialize a null array.");
    }   // end if: null

    if (off < 0) {
      throw new IllegalArgumentException("Cannot have negative offset: " + off);
    }   // end if: off < 0

    if (len < 0) {
      throw new IllegalArgumentException("Cannot have length offset: " + len);
    }   // end if: len < 0

    if (off + len > source.length) {
      throw new IllegalArgumentException(
          String
              .format("Cannot have offset of %d and length of %d with array of length %d", off, len,
                  source.length));
    }   // end if: off < 0

    // Compress?
    if ((options & GZIP) != 0) {
      java.io.ByteArrayOutputStream baos = null;
      java.util.zip.GZIPOutputStream gzos = null;
      Base64.OutputStream b64os = null;

      try {
        // GZip -> Base64 -> ByteArray
        baos = new java.io.ByteArrayOutputStream();

View on GitHub (pinned to afeacbf8c0)