1 | Mad.BinaryStrings.FileStream = function(file, callback) {
|
---|
2 | this.offset = 0;
|
---|
3 | var self = this, reader = new FileReader();
|
---|
4 |
|
---|
5 | reader.onload = function () {
|
---|
6 | self['buffer'] = reader.result;
|
---|
7 | self['amountRead'] = self['buffer'].length;
|
---|
8 | self['contentLength'] = self['buffer'].length;
|
---|
9 |
|
---|
10 | self.length = self['amountRead'];
|
---|
11 |
|
---|
12 | callback(self);
|
---|
13 | }
|
---|
14 |
|
---|
15 | reader.onerror = function () {
|
---|
16 | console.log("Error!");
|
---|
17 | }
|
---|
18 |
|
---|
19 | reader.readAsBinaryString(file);
|
---|
20 | }
|
---|
21 |
|
---|
22 | Mad.BinaryStrings.FileStream.prototype = new Mad.BinaryStrings.ByteStream();
|
---|
23 |
|
---|
24 | Mad.BinaryStrings.FileStream.prototype.absoluteAvailable = function(n, updated) {
|
---|
25 | return n < this['amountRead'];
|
---|
26 | }
|
---|
27 |
|
---|
28 | Mad.BinaryStrings.FileStream.prototype.substream = function (offset, length) {
|
---|
29 | return new Mad.BinaryStrings.SubStream(this, offset, length);
|
---|
30 | }
|
---|
31 |
|
---|
32 | Mad.BinaryStrings.FileStream.prototype.seek = function(n) {
|
---|
33 | this['offset'] += n;
|
---|
34 | }
|
---|
35 |
|
---|
36 | Mad.BinaryStrings.FileStream.prototype.read = function(n) {
|
---|
37 | var result = this.peek(n);
|
---|
38 |
|
---|
39 | this.seek(n);
|
---|
40 |
|
---|
41 | return result;
|
---|
42 | }
|
---|
43 |
|
---|
44 | Mad.BinaryStrings.FileStream.prototype.peek = function(n) {
|
---|
45 | if (this.available(n)) {
|
---|
46 | var offset = this['offset'];
|
---|
47 |
|
---|
48 | var result = this.get(offset, n);
|
---|
49 |
|
---|
50 | return result;
|
---|
51 | } else {
|
---|
52 | throw 'TODO: THROW PEEK ERROR!';
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | Mad.BinaryStrings.FileStream.prototype.get = function(offset, length) {
|
---|
57 | if (this.absoluteAvailable(offset + length)) {
|
---|
58 | return this['buffer'].slice(offset, offset + length);
|
---|
59 | } else {
|
---|
60 | throw 'TODO: THROW GET ERROR!';
|
---|
61 | }
|
---|
62 | }
|
---|