All files / src/Ice Buffer.js

83.75% Statements 232/277
69.69% Branches 46/66
96.77% Functions 30/31
83.75% Lines 232/277

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 27841x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 11629x 41x 41x 41x 11629x 11588x 11588x 11588x 11588x 11629x 11629x 11629x 41x 41x 3799x 3799x 41x 41x 106214x 237x 106214x 8756x 8756x 106214x 106214x 41x 41x 879x 879x 879x 879x 879x 41x 41x 41x 41x 41x 41x 41x 41x 104597x 104597x 98776x 98776x 104597x 41x 41x 8756x 8756x 8756x 8515x 8756x 241x 241x 241x 241x 8756x 8756x       8756x 41x 41x 47852x     47852x 47852x 47852x 41x 41x 105x     105x 105x 41x 41x 5026x 5026x     5026x 5026x     5026x 5026x 5026x 5026x 41x 41x 1855x     1855x 1855x 1855x 41x 41x 38018x     38018x 38018x 38018x 41x 41x 4172x     4172x 4172x 41x 41x 754x     754x 754x 754x 41x 41x 763x     763x 763x 763x 41x 41x 828x     828x 828x 828x 41x 41x 9853x 9853x 9853x 9853x 9853x 9853x 9853x 41x 41x 149831x 1x 1x 149830x 149830x 149830x 149831x 41x 41x           41x 41x 426x     426x 426x 426x 426x 41x 41x 14x     14x 14x   14x 14x 14x 14x 41x 41x 1939x     1939x 1939x 1939x 1939x 41x 41x 48903x     48903x 48903x 48903x 48903x 41x 41x 637x     637x 637x 637x 637x 41x 41x 650x     650x 650x 650x 650x 41x 41x 684x     684x 684x 684x 684x 41x 41x 10313x     10313x 10313x 10313x 10313x 86995x 86995x 10313x 10313x 10313x 41x 41x 77997x 77997x 41x 41x 62584x 62584x 62584x 62584x 41x 41x 21769x 21769x 41x 41x 228086x 228086x 41x 41x 64815x 64815x 41x  
// Copyright (c) ZeroC, Inc.
 
const bufferOverflowExceptionMsg = "BufferOverflowException";
const bufferUnderflowExceptionMsg = "BufferUnderflowException";
const indexOutOfBoundsExceptionMsg = "IndexOutOfBoundsException";
 
// Singleton TextEncoder for UTF-8 string encoding
const textEncoder = new TextEncoder();
 
export class Buffer {
    constructor(buffer) {
        if (buffer !== undefined) {
            this.b = buffer;
            this.v = new DataView(this.b);
            this._limit = this.b.byteLength;
        } else {
            this.b = null; // ArrayBuffer
            this.v = null; // DataView
            this._limit = 0;
        }
        this._position = 0;
        this._shrinkCounter = 0;
    }
 
    empty() {
        return this._limit === 0;
    }
 
    resize(n) {
        if (n === 0) {
            this.clear();
        } else if (n > this.capacity) {
            this.reserve(n);
        }
        this._limit = n;
    }
 
    clear() {
        this.b = null;
        this.v = null;
        this._position = 0;
        this._limit = 0;
    }
 
    //
    // Call expand(n) to add room for n additional bytes. Note that expand()
    // examines the current position of the buffer first; we don't want to
    // expand the buffer if the caller is writing to a location that is
    // already in the buffer.
    //
    expand(n) {
        const sz = this.capacity === 0 ? n : this._position + n;
        if (sz > this._limit) {
            this.resize(sz);
        }
    }
 
    reserve(n) {
        if (n > this.capacity) {
            const capacity = Math.max(1024, Math.max(n, 2 * this.capacity));
            if (!this.b) {
                this.b = new ArrayBuffer(capacity);
            } else {
                const b = new Uint8Array(capacity);
                b.set(new Uint8Array(this.b, 0, this._limit));
                this.b = b.buffer;
            }
            this.v = new DataView(this.b);
        } else if (n < this.capacity) {
            this.b = this.b.slice(0, n);
            this.v = new DataView(this.b);
        }
    }
 
    put(v) {
        if (this._position === this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setUint8(this._position, v);
        this._position++;
    }
 
    putAt(i, v) {
        if (i >= this._limit) {
            throw new RangeError(indexOutOfBoundsExceptionMsg);
        }
        this.v.setUint8(i, v);
    }
 
    putArray(v) {
        // Expects an Uint8Array
        if (!(v instanceof Uint8Array)) {
            throw new TypeError("argument is not a Uint8Array");
        }
        if (v.byteLength > 0) {
            if (this._position + v.length > this._limit) {
                throw new RangeError(bufferOverflowExceptionMsg);
            }
            new Uint8Array(this.b, this._position, v.byteLength).set(v);
            this._position += v.byteLength;
        }
    }
 
    putShort(v) {
        if (this._position + 2 > this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setInt16(this._position, v, true);
        this._position += 2;
    }
 
    putInt(v) {
        if (this._position + 4 > this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setInt32(this._position, v, true);
        this._position += 4;
    }
 
    putIntAt(i, v) {
        if (i + 4 > this._limit || i < 0) {
            throw new RangeError(indexOutOfBoundsExceptionMsg);
        }
        this.v.setInt32(i, v, true);
    }
 
    putFloat(v) {
        if (this._position + 4 > this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setFloat32(this._position, v, true);
        this._position += 4;
    }
 
    putDouble(v) {
        if (this._position + 8 > this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setFloat64(this._position, v, true);
        this._position += 8;
    }
 
    putLong(v) {
        if (this._position + 8 > this._limit) {
            throw new RangeError(bufferOverflowExceptionMsg);
        }
        this.v.setBigInt64(this._position, v, true);
        this._position += 8;
    }
 
    writeString(stream, v) {
        const encoded = textEncoder.encode(v);
        stream.writeSize(encoded.length);
        this.expand(encoded.length);
        new Uint8Array(this.b, this._position, encoded.length).set(encoded);
        this._position += encoded.length;
        this._limit = this._position;
    }
 
    get() {
        if (this._position >= this._limit) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getUint8(this._position);
        this._position++;
        return v;
    }
 
    getAt(i) {
        if (i < 0 || i >= this._limit) {
            throw new RangeError(indexOutOfBoundsExceptionMsg);
        }
        return this.v.getUint8(i);
    }
 
    getArray(length) {
        if (this._position + length > this._limit) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const buffer = this.b.slice(this._position, this._position + length);
        this._position += length;
        return new Uint8Array(buffer);
    }
 
    getArrayAt(position, length) {
        if (position + length > this._limit) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
 
        if (length === undefined) {
            return new Uint8Array(this.b.slice(position));
        } else {
            return new Uint8Array(this.b.slice(position, position + length));
        }
    }
 
    getShort() {
        if (this._limit - this._position < 2) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getInt16(this._position, true);
        this._position += 2;
        return v;
    }
 
    getInt() {
        if (this._limit - this._position < 4) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getInt32(this._position, true);
        this._position += 4;
        return v;
    }
 
    getFloat() {
        if (this._limit - this._position < 4) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getFloat32(this._position, true);
        this._position += 4;
        return v;
    }
 
    getDouble() {
        if (this._limit - this._position < 8) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getFloat64(this._position, true);
        this._position += 8;
        return v;
    }
 
    getLong() {
        if (this._limit - this._position < 8) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
        const v = this.v.getBigInt64(this._position, true);
        this._position += 8;
        return v;
    }
 
    getString(length) {
        if (this._position + length > this._limit) {
            throw new RangeError(bufferUnderflowExceptionMsg);
        }
 
        const data = new DataView(this.b, this._position, length);
        let s = "";
        for (let i = 0; i < length; ++i) {
            s += String.fromCharCode(data.getUint8(i));
        }
        this._position += length;
        return decodeURIComponent(escape(s));
    }
 
    get position() {
        return this._position;
    }
 
    set position(value) {
        if (value >= 0 && value <= this._limit) {
            this._position = value;
        }
    }
 
    get limit() {
        return this._limit;
    }
 
    get capacity() {
        return this.b === null ? 0 : this.b.byteLength;
    }
 
    get remaining() {
        return this._limit - this._position;
    }
}