karatelabs/karate · error · java.lang.UnsupportedOperationException

Uint8Array is fixed-size

Error message

Uint8Array is fixed-size

What it means

JsUint8Array models a fixed-size typed byte array, so its List-style mutation method add(Object) throws UnsupportedOperationException with 'Uint8Array is fixed-size'. Typed arrays in JavaScript have a length fixed at construction; elements can be replaced but never appended. This error is by design, not a defect.

Solutions

  1. Pre-allocate the required size: new Uint8Array(totalLength) and assign by index.
  2. Build bytes in a plain array and construct the Uint8Array from it.
  3. If you need a growable wrapper, use subarray/splice-style semantics via new copies instead of add().

Example fix

// before
u8.add(0xFF); // throws
// after
var tmp = [];
for (var i = 0; i < u8.length; i++) tmp.push(u8[i]);
tmp.push(0xFF);
var u8b = new Uint8Array(tmp.length);
for (var i = 0; i < tmp.length; i++) u8b[i] = tmp[i];
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof arr.add === 'function' && arr instanceof Java.type ? false : false) {}
// simpler: only call add() on plain arrays
if (!Array.isArray(bytes) ) throw new Error('use index assignment on Uint8Array');

Type guard

function isUint8Array(x) { return x != null && x.length != null && typeof x.get === 'function'; }

Try / catch

try { u8.add(v); } catch (e) { /* fixed-size: build new array */ }

Prevention

When it happens

Trigger: Calling u8.add(value) (List API) or any code path that routes to the List add operation on a Uint8Array instance.

Common situations: Treating a Uint8Array like a regular mutable list when accumulating bytes; generic Java code that appends to any Collection it receives.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/65b626296942eb93. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java:220

    }

    @Override
    public int lastIndexOf(Object o) {
        if (o instanceof Number n) {
            int val = n.intValue() & 0xFF;
            for (int i = buffer.length - 1; i >= 0; i--) {
                if ((buffer[i] & 0xFF) == val) {
                    return i;
                }
            }
        }
        return -1;
    }

    // Fixed-size array - these operations throw UnsupportedOperationException
    @Override
    public boolean add(Object o) {
        throw new UnsupportedOperationException("Uint8Array is fixed-size");
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException("Uint8Array is fixed-size");
    }

    @Override
    public void add(int index, Object element) {
        throw new UnsupportedOperationException("Uint8Array is fixed-size");
    }

    @Override
    public Object remove(int index) {
        throw new UnsupportedOperationException("Uint8Array is fixed-size");
    }

    @Override

View on GitHub (pinned to a22eb90246)