| 6 | * @public |
| 7 | */ |
| 8 | export class ByteBuffer implements IWriteable, IReadable { |
| 9 | private _buffer!: Uint8Array; |
| 10 | |
| 11 | public length: number = 0; |
| 12 | public position: number = 0; |
| 13 | |
| 14 | public get bytesWritten(): number { |
| 15 | return this.position; |
| 16 | } |
| 17 | |
| 18 | public getBuffer(): Uint8Array { |
| 19 | return this._buffer; |
| 20 | } |
| 21 | |
| 22 | public static empty(): ByteBuffer { |
| 23 | return ByteBuffer.withCapacity(0); |
| 24 | } |
| 25 | |
| 26 | public static withCapacity(capacity: number): ByteBuffer { |
| 27 | const buffer: ByteBuffer = new ByteBuffer(); |
| 28 | buffer._buffer = new Uint8Array(capacity); |
| 29 | return buffer; |
| 30 | } |
| 31 | |
| 32 | public static fromBuffer(data: Uint8Array): ByteBuffer { |
| 33 | const buffer: ByteBuffer = new ByteBuffer(); |
| 34 | buffer._buffer = data; |
| 35 | buffer.length = data.length; |
| 36 | return buffer; |
| 37 | } |
| 38 | |
| 39 | public static fromString(contents: string): ByteBuffer { |
| 40 | const byteArray: Uint8Array = IOHelper.stringToBytes(contents); |
| 41 | return ByteBuffer.fromBuffer(byteArray); |
| 42 | } |
| 43 | |
| 44 | public reset(): void { |
| 45 | this.position = 0; |
| 46 | } |
| 47 | |
| 48 | public skip(offset: number): void { |
| 49 | this.position += offset; |
| 50 | } |
| 51 | |
| 52 | public readByte(): number { |
| 53 | const n: number = this.length - this.position; |
| 54 | if (n <= 0) { |
| 55 | return -1; |
| 56 | } |
| 57 | return this._buffer[this.position++]; |
| 58 | } |
| 59 | |
| 60 | public read(buffer: Uint8Array, offset: number, count: number): number { |
| 61 | let n: number = this.length - this.position; |
| 62 | if (n > count) { |
| 63 | n = count; |
| 64 | } |
| 65 | if (n <= 0) { |
nothing calls this directly
no outgoing calls
no test coverage detected