{"record":{"id":"65b626296942eb93","repo":"karatelabs/karate","slug":"uint8array-is-fixed-size","errorCode":null,"errorMessage":"Uint8Array is fixed-size","messagePattern":"Uint8Array is fixed-size","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java","lineNumber":220,"sourceCode":"    }\n\n    @Override\n    public int lastIndexOf(Object o) {\n        if (o instanceof Number n) {\n            int val = n.intValue() & 0xFF;\n            for (int i = buffer.length - 1; i >= 0; i--) {\n                if ((buffer[i] & 0xFF) == val) {\n                    return i;\n                }\n            }\n        }\n        return -1;\n    }\n\n    // Fixed-size array - these operations throw UnsupportedOperationException\n    @Override\n    public boolean add(Object o) {\n        throw new UnsupportedOperationException(\"Uint8Array is fixed-size\");\n    }\n\n    @Override\n    public boolean remove(Object o) {\n        throw new UnsupportedOperationException(\"Uint8Array is fixed-size\");\n    }\n\n    @Override\n    public void add(int index, Object element) {\n        throw new UnsupportedOperationException(\"Uint8Array is fixed-size\");\n    }\n\n    @Override\n    public Object remove(int index) {\n        throw new UnsupportedOperationException(\"Uint8Array is fixed-size\");\n    }\n\n    @Override","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java#L202-L238","documentation":"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.","triggerScenarios":"Calling u8.add(value) (List API) or any code path that routes to the List add operation on a Uint8Array instance.","commonSituations":"Treating a Uint8Array like a regular mutable list when accumulating bytes; generic Java code that appends to any Collection it receives.","solutions":["Pre-allocate the required size: new Uint8Array(totalLength) and assign by index.","Build bytes in a plain array and construct the Uint8Array from it.","If you need a growable wrapper, use subarray/splice-style semantics via new copies instead of add()."],"exampleFix":"// before\nu8.add(0xFF); // throws\n// after\nvar tmp = [];\nfor (var i = 0; i < u8.length; i++) tmp.push(u8[i]);\ntmp.push(0xFF);\nvar u8b = new Uint8Array(tmp.length);\nfor (var i = 0; i < tmp.length; i++) u8b[i] = tmp[i];","handlingStrategy":"type-guard","validationCode":"if (typeof arr.add === 'function' && arr instanceof Java.type ? false : false) {}\n// simpler: only call add() on plain arrays\nif (!Array.isArray(bytes) ) throw new Error('use index assignment on Uint8Array');","typeGuard":"function isUint8Array(x) { return x != null && x.length != null && typeof x.get === 'function'; }","tryCatchPattern":"try { u8.add(v); } catch (e) { /* fixed-size: build new array */ }","preventionTips":["Treat Uint8Array as fixed-size: replace slots, never append.","Pre-size the array from the expected payload length.","Do incremental byte accumulation in a plain JS array."],"tags":["javascript","typed-array","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}