-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFs.ts
73 lines (65 loc) · 2.55 KB
/
Fs.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
namespace Fs {
export class ByteStream {
position: number = 0;
length: number = 0;
constructor(public buffer: Uint8Array, private startPosition: number = 0, private end: number = 0) {
if (end == 0)
this.end = this.buffer.byteLength;
this.length = this.end - this.startPosition;
}
readUint8(): number {
return this.buffer[this.startPosition + this.position++];
}
readUint16(littleEndian: boolean = true): number {
var b1: number = this.buffer[this.startPosition + this.position++];
var b2: number = this.buffer[this.startPosition + this.position++];
if (littleEndian) {
return (b2 << 8) + b1;
}
return (b1 << 8) + b2;
}
readInt16(littleEndian: boolean = true): number {
var b1: number = this.buffer[this.startPosition + this.position++];
var b2: number = this.buffer[this.startPosition + this.position++];
if (littleEndian) {
return ((((b2 << 8) | b1) << 16) >> 16);
}
return ((((b1 << 8) | b2) << 16) >> 16);
}
}
export interface IByteStreamDict {
[index: string]: Fs.ByteStream;
}
export function downloadAllFiles(path: string, files: string[], done: (buffers: IByteStreamDict) => void) {
var buffers: IByteStreamDict = {};
var leftToDownload: number = files.length;
function getBinary(url: string, success: (data: ArrayBuffer) => void): void {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (xhr.response === null) {
throw "Fatal error downloading '" + url + "'";
} else {
console.log("Successfully downloaded '" + url + "'");
success(xhr.response);
}
}
};
xhr.send();
}
function handleFile(num: number) {
getBinary(path + files[num], (buffer: ArrayBuffer) => {
buffers[files[num]] = new ByteStream(new Uint8Array(buffer));
leftToDownload--;
if (leftToDownload === 0) {
done(buffers);
}
});
}
for (var i = 0; i < files.length; i++) {
handleFile(i);
}
}
}