-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuffer_writer.ts
64 lines (53 loc) · 1.71 KB
/
buffer_writer.ts
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
import {BufferReader} from "./buffer_reader";
export class BufferOverflowError extends Error {
constructor() {
super('BufferOverflowError');
}
}
export class BufferWriter {
public buffer: Buffer;
public offset: number;
public limit: number;
public constructor(buffer: Buffer) {
this.buffer = buffer;
this.offset = 0;
this.limit = buffer.byteLength;
}
public get remaining(): number {
return this.limit - this.offset;
}
public checkOverflow(length: number): boolean {
return (length + this.offset) > this.limit;
}
public verifyOverflow(length: number): void {
if (this.checkOverflow(length)) {
throw new BufferOverflowError();
}
}
public skip(length: number): void {
this.verifyOverflow(length);
this.offset += length;
}
public writeFromBuffer(buffer: Buffer, offset?: number, length?: number): void {
const _offset = (typeof offset === 'undefined') ? 0 : offset;
const _length = (typeof length === 'undefined') ? buffer.byteLength : length;
this.verifyOverflow(_length);
buffer.copy(this.buffer, this.offset, _offset, _offset + _length);
}
public writeFromReader(reader: BufferReader, length: number): void {
reader.readToBuffer(this.buffer, this.offset, length);
this.offset += length;
}
public writeFromReaderPartly(reader: BufferReader, maxLength: number): number {
const length = reader.readToBufferPartly(this.buffer, this.offset, maxLength);
this.offset += length;
return length;
}
public readRemaining(): Buffer {
const remaining = this.limit - this.offset;
const target = Buffer.alloc(remaining);
this.buffer.copy(target, 0, this.offset);
this.offset += length;
return target;
}
}