-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-reader.class.js
44 lines (41 loc) · 1.48 KB
/
binary-reader.class.js
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
/* Copyright (c) 2022 Read Write Tools. */
import FS from 'fs';
import terminal from 'softlib/terminal.js';
import expect from 'softlib/expect.js';
export default class BinaryReader {
constructor() {
this.fd = null, this.readSize = 8192, this.buffer = new Buffer(this.readSize), this.bufferLength = null,
this.blockOffset = null, this.bufferOffset = null, Object.seal(this), this.initialize();
}
initialize() {
this.buffer.fill(0), this.bufferLength = 0, this.blockOffset = 0, this.bufferOffset = 0;
}
open(t) {
expect(t, [ 'String', 'Pfile' ]), 'Pfile' == t.constructor.name && (t = t.name);
try {
return this.fd = FS.openSync(t, 'r'), this.initialize(), !0;
} catch (t) {
return terminal.caught(t), !1;
}
}
isOpen() {
return null != this.fd;
}
close() {
if (this.isOpen()) try {
this.fd = FS.closeSync(this.fd), this.fd = null;
} catch (t) {
terminal.caught(t), this.fd = null;
}
}
readBlock() {
if (!this.isOpen()) return !1;
try {
return this.buffer.fill(0), this.bufferLength = FS.readSync(this.fd, this.buffer, 0, this.readSize, this.blockOffset),
this.blockOffset += this.bufferLength, this.bufferOffset = 0, this.bufferLength > 0;
} catch (t) {
return terminal.trace(t.message), this.bufferLength = 0, this.bufferOffset = 0,
!1;
}
}
}