All files / src/utils bite-array-output-stream.js

100% Statements 46/46
100% Branches 8/8
100% Functions 6/6
100% Lines 46/46

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 471x 2x 2x 2x 2x 2x 2x 2x 2x 602x 602x 2x 2x 2x 2x 2x 2x 6x 6x 6x 2x 2x 2x 2x 2x 2x 2x 2x 284x 284x 2x 2x 2x 2x 2x 2x 2x 3x 8x 8x 3x 2x 2x 2x 2x 2x  
export class ByteArrayOutputStream {
  /** @type {number[]} */
  #bytes = []
 
  /**
   *
   * @param {number} b - byte value
   */
  writeByte (b) {
    this.#bytes.push(b & 0xff)
  };
 
  /**
   *
   * @param {number} i - 2 byte little-endian numeric value
   */
  writeShort (i) {
    this.writeByte(i)
    this.writeByte(i >>> 8)
  };
 
  /**
   * @param {ArrayLike<number>} b - byte array
   * @param {number} [offset] - starting position
   * @param {number} [length] - array length to copy
   */
  writeBytes (b, offset = 0, length = b.length) {
    for (let i = 0; i < length; i += 1) {
      this.writeByte(b[i + offset])
    }
  };
 
  /**
   *
   * @param {string} str - char code list
   */
  writeString (str) {
    for (let i = 0, e = str.length; i < e; i += 1) {
      this.writeByte(/** @type {number} */(str.codePointAt(i)))
    }
  };
 
  toByteArray () {
    return Uint8Array.from(this.#bytes)
  };
}