1 |
|
---|
2 | Mad.ArrayBuffers.ByteStream = Class.extend({
|
---|
3 | available: function(n) {
|
---|
4 | return this.absoluteAvailable(this.offset + n);
|
---|
5 | },
|
---|
6 |
|
---|
7 | strEquals: function (offset, string) {
|
---|
8 | for (var i = 0; i < string.length; i++) {
|
---|
9 | if(this.getU8(offset + i, false) != string.charCodeAt(i)) return false;
|
---|
10 | }
|
---|
11 | return true;
|
---|
12 | },
|
---|
13 |
|
---|
14 | getU8: function(offset, bigEndian) {
|
---|
15 | var bytes = this.get(offset, 1);
|
---|
16 | throw 'Wtf bytestream?'
|
---|
17 | return bytes[0];
|
---|
18 | },
|
---|
19 |
|
---|
20 | getU16: function(offset, bigEndian) {
|
---|
21 | var bytes = this.get(offset, 2);
|
---|
22 | if (!bigEndian) {
|
---|
23 | bytes = bytes.reverse();
|
---|
24 | }
|
---|
25 | return (bytes[0] << 8) | bytes[1];
|
---|
26 | },
|
---|
27 |
|
---|
28 | getU24: function(offset, bigEndian) {
|
---|
29 | var bytes = this.get(offset, 3);
|
---|
30 | if (!bigEndian) {
|
---|
31 | bytes = bytes.reverse();
|
---|
32 | }
|
---|
33 | return (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
|
---|
34 | },
|
---|
35 |
|
---|
36 | getU32: function(offset, bigEndian) {
|
---|
37 | var bytes = this.get(offset, 4);
|
---|
38 | if (!bigEndian) {
|
---|
39 | bytes = bytes.reverse();
|
---|
40 | }
|
---|
41 | return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
|
---|
42 | },
|
---|
43 |
|
---|
44 | getI8: function(offset, bigEndian) {
|
---|
45 | return this.getU8(offset, bigEndian) - 128; // 2 ** 7
|
---|
46 | },
|
---|
47 |
|
---|
48 | getI16: function(offset, bigEndian) {
|
---|
49 | return this.getU16(offset, bigEndian) - 65536; // 2 ** 15
|
---|
50 | },
|
---|
51 |
|
---|
52 | getI32: function(offset, bigEndian) {
|
---|
53 | return this.getU32(offset, bigEndian) - 2147483648; // 2 ** 31
|
---|
54 | },
|
---|
55 |
|
---|
56 | getSyncInteger: function(offset) {
|
---|
57 | var bytes = this.get(offset, 4);
|
---|
58 | return (bytes[0] << 21) | (bytes[1] << 14) | (bytes[2] << 7) | bytes[3];
|
---|
59 | },
|
---|
60 |
|
---|
61 | peekU8: function(bigEndian) {
|
---|
62 | return this.getU8(this.offset, bigEndian);
|
---|
63 | },
|
---|
64 |
|
---|
65 | peekU16: function(bigEndian) {
|
---|
66 | return this.getU16(this.offset, bigEndian);
|
---|
67 | },
|
---|
68 |
|
---|
69 | peekU24: function(bigEndian) {
|
---|
70 | return this.getU24(this.offset, bigEndian);
|
---|
71 | },
|
---|
72 |
|
---|
73 | peekU32: function(bigEndian) {
|
---|
74 | return this.getU32(this.offset, bigEndian);
|
---|
75 | },
|
---|
76 |
|
---|
77 | peekI8: function(bigEndian) {
|
---|
78 | return this.getI8(this.offset, bigEndian);
|
---|
79 | },
|
---|
80 |
|
---|
81 | peekI16: function(bigEndian) {
|
---|
82 | return this.getI16(this.offset, bigEndian);
|
---|
83 | },
|
---|
84 |
|
---|
85 | peekI32: function(bigEndian) {
|
---|
86 | return this.getI32(this.offset, bigEndian);
|
---|
87 | },
|
---|
88 |
|
---|
89 | peekSyncInteger: function() {
|
---|
90 | return this.getSyncInteger(this.offset);
|
---|
91 | },
|
---|
92 |
|
---|
93 | readU8: function(bigEndian) {
|
---|
94 | var result = this.peekU8(bigEndian);
|
---|
95 | this.seek(1);
|
---|
96 | return result;
|
---|
97 | },
|
---|
98 |
|
---|
99 | readU16: function(bigEndian) {
|
---|
100 | var result = this.peekU16(bigEndian);
|
---|
101 | this.seek(2);
|
---|
102 | return result;
|
---|
103 | },
|
---|
104 |
|
---|
105 | readU24: function(bigEndian) {
|
---|
106 | var result = this.peekU24(bigEndian);
|
---|
107 | this.seek(3);
|
---|
108 | return result;
|
---|
109 | },
|
---|
110 |
|
---|
111 | readU32: function(bigEndian) {
|
---|
112 | var result = this.peekU32(bigEndian);
|
---|
113 | this.seek(4);
|
---|
114 | return result;
|
---|
115 | },
|
---|
116 |
|
---|
117 | readI8: function(bigEndian) {
|
---|
118 | var result = this.peekI8(bigEndian);
|
---|
119 | this.seek(1);
|
---|
120 | return result;
|
---|
121 | },
|
---|
122 |
|
---|
123 | readI16: function(bigEndian) {
|
---|
124 | var result = this.peekI16(bigEndian);
|
---|
125 | this.seek(2);
|
---|
126 | return result;
|
---|
127 | },
|
---|
128 |
|
---|
129 | readI32: function(bigEndian) {
|
---|
130 | var result = this.peekI32(bigEndian);
|
---|
131 | this.seek(4);
|
---|
132 | return result;
|
---|
133 | },
|
---|
134 |
|
---|
135 | readSyncInteger: function() {
|
---|
136 | var result = this.getSyncInteger(this.offset);
|
---|
137 | this.seek(4);
|
---|
138 | return result;
|
---|
139 | },
|
---|
140 |
|
---|
141 | readAsString: function(n) {
|
---|
142 | var arr = this.read(n);
|
---|
143 | var str = "";
|
---|
144 | for (var i = 0; i < n; i++) {
|
---|
145 | str += String.fromCharCode(arr[i]);
|
---|
146 | }
|
---|
147 | return str;
|
---|
148 | }
|
---|
149 | });
|
---|