-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryfile.ts
180 lines (171 loc) · 3.52 KB
/
memoryfile.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import type {
BufferView,
File,
FileReadStats,
FileStats,
FileWriteStats,
} from './type.ts';
import { BLK_LIMIT, INT_LIMIT } from './const.ts';
import { ranged } from './util.ts';
/**
* In-memory file-like object.
*/
export class MemoryFile implements File {
declare public readonly ['constructor']: Omit<typeof MemoryFile, 'new'>;
/**
* Data blocks.
*/
readonly #blocks: (Uint8Array | undefined)[] = [];
/**
* Block size.
*/
readonly #blksize: number;
/**
* Data size.
*/
#size: number;
/**
* Create file with optional initial size and custom block size.
*
* @param size File size.
* @param blksize Block size.
*/
constructor(size = 0, blksize = 4096) {
ranged(size, 0, INT_LIMIT);
ranged(blksize, 1, BLK_LIMIT);
this.#size = size;
this.#blksize = blksize;
const o = size % blksize;
this.#blocks.length = (size - o) / blksize + (o ? 1 : 0);
}
/**
* @inheritdoc
*/
// deno-lint-ignore require-await
public async stat(): Promise<FileStats> {
return {
blocks: this.#blocks.length,
blksize: this.#blksize,
size: this.#size,
};
}
/**
* @inheritdoc
*/
// deno-lint-ignore require-await
public async truncate(size: number): Promise<void> {
ranged(size, 0, INT_LIMIT);
const s = this.#size;
if (s === size) {
return;
}
const blksize = this.#blksize;
const blocks = this.#blocks;
const o = size % blksize;
const b = (size - o) / blksize;
if (size > s) {
blocks.length = b + (o ? 1 : 0);
} else if (o) {
blocks.length = b + 1;
blocks[b]?.fill(0, o);
} else {
blocks.length = b;
}
this.#size = size;
}
/**
* @inheritdoc
*/
// deno-lint-ignore require-await
public async read(
buffer: BufferView,
offset: number,
length: number,
position: number,
): Promise<FileReadStats> {
const { buffer: bd, byteOffset: bo, byteLength: bl } = buffer;
ranged(offset, 0, bl);
ranged(length, 0, bl - offset);
ranged(position, 0, INT_LIMIT - bl);
const size = this.#size;
if (position + length > size) {
length = size - position;
}
if (length) {
const r = new Uint8Array(bd, bo + offset, length);
const blksize = this.#blksize;
const blocks = this.#blocks;
let o = position % blksize;
let b = (position - o) / blksize;
for (let i = 0, l = length, s = blksize - o;; b++) {
if (l < s) {
s = l;
}
const d = blocks[b];
if (d) {
r.set(d.subarray(o, o + s), i);
} else {
r.fill(0, i, i + s);
}
l -= s;
if (l <= 0) {
break;
}
i += s;
o = 0;
s = blksize;
}
}
return {
bytesRead: length,
};
}
/**
* @inheritdoc
*/
// deno-lint-ignore require-await
public async write(
buffer: Readonly<BufferView>,
offset: number,
length: number,
position: number,
): Promise<FileWriteStats> {
const { buffer: bd, byteOffset: bo, byteLength: bl } = buffer;
ranged(offset, 0, bl);
ranged(length, 0, bl - offset);
ranged(position, 0, INT_LIMIT - bl);
if (length) {
const blksize = this.#blksize;
const blocks = this.#blocks;
const size = this.#size;
let p = position;
let o = p % blksize;
let b = (p - o) / blksize;
for (let i = bo, l = length, s = blksize - o;; b++) {
if (l < s) {
s = l;
}
const w = new Uint8Array(bd, i, s);
let d = blocks[b];
if (!d) {
blocks[b] = d = new Uint8Array(blksize);
}
d.set(w, o);
p += s;
if (p > size) {
this.#size = p;
}
l -= s;
if (l <= 0) {
break;
}
i += s;
o = 0;
s = blksize;
}
}
return {
bytesWritten: length,
};
}
}