-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_082.js
44 lines (40 loc) Β· 903 Bytes
/
problem_082.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
// Algorithmic Paradigm: I/O File Stream
// O(1) Time complexity
// O(1) Space complexity
class FileReader {
/**
* Initializs a file reader
*/
constructor(file) {
this.file = file;
this.offset = 0;
this.buffer = '';
}
/**
* Reads 7 characters from the file
* @return {string}
*/
read7() {
const start = this.offset;
const end = Math.min(this.offset + 7, this.file.length);
// Update the offset
this.offset = end;
return this.file.substring(start, end);
}
/**
* Reads N characters using read7
* @param {number} n
* @return {string}
*/
readN(n) {
while (this.buffer.length < n) {
const extra = this.read7();
if (extra.length === 0) break;
this.buffer += extra;
}
// Update buffer
const readResult = this.buffer.substring(0, n);
this.buffer = this.buffer.substring(n);
return readResult;
}
}