All files / src/io file.ts

77.77% Statements 49/63
63.63% Branches 7/11
70% Functions 14/20
76.27% Lines 45/59

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  4x 4x     5x 5x 5x     814x     80x           1x                     40x 40x 40x 185x 185x     185x 185x     40x     33x 33x 90x 90x 33x   57x         2x     10x 10x 160x 160x   10x     31x 31x 149008x 149008x   31x     3626x     149069x     2157x 2157x 2157x               34x 34x 34x               1x 1x 1x               4x                                          
 
export class BinaryFile {
  dataView: DataView;
  buffer: ArrayBuffer;
  offset: number;
 
  constructor(data: ArrayBuffer) {
    this.buffer = data;
    this.dataView = new DataView(this.buffer, 0);
    this.offset = 0;
  }
 
  tell(): number {
    return this.offset;
  }

  seek(offset: number) {
    this.offset = offset;
  }
 
  skip(bytes: number) {
    this.offset += bytes;
  }
 
  eof() {
    return this.offset >= this.buffer.byteLength; // length;
  }

  slice(offset: number, length: number) {
    return new BinaryFile(this.buffer.slice(offset, offset + length));
  }
 
  read(length: number): BinaryFile {
    const result = new BinaryFile(this.buffer.slice(this.offset, this.offset + length));
    this.offIset += length;
    return result;
  }
E
  readString(length: number): string {
    let result = '';
    let terminated = false;
    for (let i = 0; i < length; i++) {
      const byte = this.readUInt8();
      if (byte === 0x0) {
        terminated = true;
      }
      if (!terminated) {
        result += String.fromCharCode(byte);
      }
    }
    return result;
  }

  readCString() {
    let result = '';
    for (let i = 0; i < 128; i++) {
      const byte = this.readUInt8();
      if (byte === 0x0) {
        return result;
      }
      result += String.fromCharCode(byte);
    }
    return result;
  }
 
  readChars(count: number): Uint8Array {
    return this.readBytes(count);
  }
 
  readBytes(count: number): Uint8Array {
    let result = new Uint8Array(count);
    for (let i = 0; i < count; i++) {
      const byte = this.readUInt8();
      result[i] = byte;
    }
    return result;
  }
 
  readSBytes(count: number): Int8Array {
    let result = new Int8Array(count);
    for (let i = 0; i < count; i++) {
      const byte = this.readInt8();
      result[i] = byte;
    }
    return result;
  }

  readUInt8(): number {
    return this.dataView.getUint8(this.offset++);
  }
 
  readInt8() {
    return this.dataView.getInt8(this.offset++);
  }

  readUInt16(little = true) {
    const result = this.dataView.getUint16(this.offset, little);
    this.offset += 2;
    return result;
  }
 
  readInt16(little = true) {
    const result = this.dataView.getInt16(this.offset, little);
    this.offset += 2;
    return result;
  }
 
  readUInt32(little = true) {
    const result = this.dataView.getUint32(this.offset, little);
    this.offset += 4;
    return result;
  }
 
  readInt32(little = true) {
    const result = this.dataView.getInt32(this.offset, little);
    this.offset += 4;
    return result;
  }
 
  readFloat(little = true) {
    const result = this.dataView.getFloat32(this.offset, little);
    this.offset += 4;
    return result;
  }
 
  close() {
    // this.dataView = undefined;
    // this.buffer = undefined;
    this.offset = -1;
  }
}