|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +export class VSBuffer { |
| 7 | + |
| 8 | + public static alloc(byteLength: number): VSBuffer { |
| 9 | + return new VSBuffer(Buffer.allocUnsafe(byteLength)); |
| 10 | + } |
| 11 | + |
| 12 | + public static wrap(actual: Buffer): VSBuffer { |
| 13 | + return new VSBuffer(actual); |
| 14 | + } |
| 15 | + |
| 16 | + public static fromString(source: string): VSBuffer { |
| 17 | + return new VSBuffer(Buffer.from(source)); |
| 18 | + } |
| 19 | + |
| 20 | + public static concat(buffers: VSBuffer[], totalLength?: number): VSBuffer { |
| 21 | + if (typeof totalLength === 'undefined') { |
| 22 | + totalLength = 0; |
| 23 | + for (let i = 0, len = buffers.length; i < len; i++) { |
| 24 | + totalLength += buffers[i].byteLength; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const ret = VSBuffer.alloc(totalLength); |
| 29 | + let offset = 0; |
| 30 | + for (let i = 0, len = buffers.length; i < len; i++) { |
| 31 | + const element = buffers[i]; |
| 32 | + ret.set(element, offset); |
| 33 | + offset += element.byteLength; |
| 34 | + } |
| 35 | + |
| 36 | + return ret; |
| 37 | + } |
| 38 | + |
| 39 | + private readonly _actual: Buffer; |
| 40 | + public readonly byteLength: number; |
| 41 | + |
| 42 | + private constructor(buffer: Buffer) { |
| 43 | + this._actual = buffer; |
| 44 | + this.byteLength = this._actual.byteLength; |
| 45 | + } |
| 46 | + |
| 47 | + public toBuffer(): Buffer { |
| 48 | + // TODO@Alex: deprecate this usage |
| 49 | + return this._actual; |
| 50 | + } |
| 51 | + |
| 52 | + public toString(): string { |
| 53 | + return this._actual.toString(); |
| 54 | + } |
| 55 | + |
| 56 | + public slice(start?: number, end?: number): VSBuffer { |
| 57 | + return new VSBuffer(this._actual.slice(start, end)); |
| 58 | + } |
| 59 | + |
| 60 | + public set(array: VSBuffer, offset?: number): void { |
| 61 | + this._actual.set(array._actual, offset); |
| 62 | + } |
| 63 | + |
| 64 | + public readUint32BE(offset: number): number { |
| 65 | + return readUint32BE(this._actual, offset); |
| 66 | + } |
| 67 | + |
| 68 | + public writeUint32BE(value: number, offset: number): void { |
| 69 | + writeUint32BE(this._actual, value, offset); |
| 70 | + } |
| 71 | + |
| 72 | + public readUint8(offset: number): number { |
| 73 | + return readUint8(this._actual, offset); |
| 74 | + } |
| 75 | + |
| 76 | + public writeUint8(value: number, offset: number): void { |
| 77 | + writeUint8(this._actual, value, offset); |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +function readUint32BE(source: Uint8Array, offset: number): number { |
| 83 | + return ( |
| 84 | + source[offset] * 2 ** 24 |
| 85 | + + source[offset + 1] * 2 ** 16 |
| 86 | + + source[offset + 2] * 2 ** 8 |
| 87 | + + source[offset + 3] |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function writeUint32BE(destination: Uint8Array, value: number, offset: number): void { |
| 92 | + destination[offset + 3] = value; |
| 93 | + value = value >>> 8; |
| 94 | + destination[offset + 2] = value; |
| 95 | + value = value >>> 8; |
| 96 | + destination[offset + 1] = value; |
| 97 | + value = value >>> 8; |
| 98 | + destination[offset] = value; |
| 99 | +} |
| 100 | + |
| 101 | +function readUint8(source: Uint8Array, offset: number): number { |
| 102 | + return source[offset]; |
| 103 | +} |
| 104 | + |
| 105 | +function writeUint8(destination: Uint8Array, value: number, offset: number): void { |
| 106 | + destination[offset] = value; |
| 107 | +} |
0 commit comments