source: gs3-extensions/web-audio/trunk/js-mad/script/binarystring/filestream.js@ 28388

Last change on this file since 28388 was 28388, checked in by davidb, 11 years ago

Set of JS, CSS, PNG etc web resources to support a mixture of audio player/document display capabilities

File size: 1.6 KB
Line 
1Mad.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
22Mad.BinaryStrings.FileStream.prototype = new Mad.BinaryStrings.ByteStream();
23
24Mad.BinaryStrings.FileStream.prototype.absoluteAvailable = function(n, updated) {
25 return n < this['amountRead'];
26}
27
28Mad.BinaryStrings.FileStream.prototype.substream = function (offset, length) {
29 return new Mad.BinaryStrings.SubStream(this, offset, length);
30}
31
32Mad.BinaryStrings.FileStream.prototype.seek = function(n) {
33 this['offset'] += n;
34}
35
36Mad.BinaryStrings.FileStream.prototype.read = function(n) {
37 var result = this.peek(n);
38
39 this.seek(n);
40
41 return result;
42}
43
44Mad.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
56Mad.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}
Note: See TracBrowser for help on using the repository browser.