1 |
|
---|
2 | Mad.ArrayBuffers.FileStream = Mad.ArrayBuffers.ByteStream.extend({
|
---|
3 | init: function (file, callback) {
|
---|
4 | this.offset = 0;
|
---|
5 |
|
---|
6 | // Check for the various File API support.
|
---|
7 | if (window.File && window.FileReader && window.FileList && window.Blob) {
|
---|
8 | // Great success! All the File APIs are supported.
|
---|
9 | } else {
|
---|
10 | alert('The File APIs are not fully supported in this browser.');
|
---|
11 | return null;
|
---|
12 | }
|
---|
13 |
|
---|
14 | var self = this;
|
---|
15 | var reader = new FileReader();
|
---|
16 | reader.onload = function () {
|
---|
17 | self.buffer = new Uint8Array(reader.result);
|
---|
18 | self.amountRead = self.buffer.length;
|
---|
19 | self.contentLength = self.buffer.length;
|
---|
20 | self.length = self.amountRead;
|
---|
21 |
|
---|
22 | callback(self);
|
---|
23 | }
|
---|
24 |
|
---|
25 | reader.onerror = function () {
|
---|
26 | console.log("Error loading file " + file);
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Only supported from Firefox 7 and Chrome 'Something'
|
---|
30 | reader.readAsArrayBuffer(file);
|
---|
31 | },
|
---|
32 |
|
---|
33 | substream: function (offset, length) {
|
---|
34 | return new Mad.ArrayBuffers.SubStream(this, offset, length);
|
---|
35 | },
|
---|
36 |
|
---|
37 | absoluteAvailable: function(n, updated) {
|
---|
38 | return n < this.amountRead;
|
---|
39 | },
|
---|
40 |
|
---|
41 | getU8: function(offset, bigEndian) {
|
---|
42 | return this.buffer[offset];
|
---|
43 | },
|
---|
44 |
|
---|
45 | seek: function(n) {
|
---|
46 | this.offset += n;
|
---|
47 | },
|
---|
48 |
|
---|
49 | read: function(n) {
|
---|
50 | var result = this.peek(n);
|
---|
51 | this.seek(n);
|
---|
52 | return result;
|
---|
53 | },
|
---|
54 |
|
---|
55 | peek: function(n) {
|
---|
56 | if (this.available(n)) {
|
---|
57 | var offset = this.offset;
|
---|
58 |
|
---|
59 | var result = this.get(offset, n);
|
---|
60 |
|
---|
61 | return result;
|
---|
62 | } else {
|
---|
63 | throw 'TODO: THROW PEEK ERROR!';
|
---|
64 | }
|
---|
65 | },
|
---|
66 |
|
---|
67 | get: function(offset, length) {
|
---|
68 | if (offset + length < this.contentLength) {
|
---|
69 | var subarr = this.buffer.subarray(offset, offset + length);
|
---|
70 | return subarr;
|
---|
71 | } else {
|
---|
72 | throw 'TODO: THROW GET ERROR!';
|
---|
73 | }
|
---|
74 | }
|
---|
75 | });
|
---|