UNPKG

5.04 kBJavaScriptView Raw
1(function() {
2 var Data;
3
4 Data = (function() {
5 function Data(data) {
6 this.data = data != null ? data : [];
7 this.pos = 0;
8 this.length = this.data.length;
9 }
10
11 Data.prototype.readByte = function() {
12 return this.data[this.pos++];
13 };
14
15 Data.prototype.writeByte = function(byte) {
16 return this.data[this.pos++] = byte;
17 };
18
19 Data.prototype.byteAt = function(index) {
20 return this.data[index];
21 };
22
23 Data.prototype.readBool = function() {
24 return !!this.readByte();
25 };
26
27 Data.prototype.writeBool = function(val) {
28 return this.writeByte(val ? 1 : 0);
29 };
30
31 Data.prototype.readUInt32 = function() {
32 var b1, b2, b3, b4;
33 b1 = this.readByte() * 0x1000000;
34 b2 = this.readByte() << 16;
35 b3 = this.readByte() << 8;
36 b4 = this.readByte();
37 return b1 + b2 + b3 + b4;
38 };
39
40 Data.prototype.writeUInt32 = function(val) {
41 this.writeByte((val >>> 24) & 0xff);
42 this.writeByte((val >> 16) & 0xff);
43 this.writeByte((val >> 8) & 0xff);
44 return this.writeByte(val & 0xff);
45 };
46
47 Data.prototype.readInt32 = function() {
48 var int;
49 int = this.readUInt32();
50 if (int >= 0x80000000) {
51 return int - 0x100000000;
52 } else {
53 return int;
54 }
55 };
56
57 Data.prototype.writeInt32 = function(val) {
58 if (val < 0) {
59 val += 0x100000000;
60 }
61 return this.writeUInt32(val);
62 };
63
64 Data.prototype.readUInt16 = function() {
65 var b1, b2;
66 b1 = this.readByte() << 8;
67 b2 = this.readByte();
68 return b1 | b2;
69 };
70
71 Data.prototype.writeUInt16 = function(val) {
72 this.writeByte((val >> 8) & 0xff);
73 return this.writeByte(val & 0xff);
74 };
75
76 Data.prototype.readInt16 = function() {
77 var int;
78 int = this.readUInt16();
79 if (int >= 0x8000) {
80 return int - 0x10000;
81 } else {
82 return int;
83 }
84 };
85
86 Data.prototype.writeInt16 = function(val) {
87 if (val < 0) {
88 val += 0x10000;
89 }
90 return this.writeUInt16(val);
91 };
92
93 Data.prototype.readString = function(length) {
94 var i, j, ref, ret;
95 ret = [];
96 for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
97 ret[i] = String.fromCharCode(this.readByte());
98 }
99 return ret.join('');
100 };
101
102 Data.prototype.writeString = function(val) {
103 var i, j, ref, results;
104 results = [];
105 for (i = j = 0, ref = val.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
106 results.push(this.writeByte(val.charCodeAt(i)));
107 }
108 return results;
109 };
110
111 Data.prototype.stringAt = function(pos, length) {
112 this.pos = pos;
113 return this.readString(length);
114 };
115
116 Data.prototype.readShort = function() {
117 return this.readInt16();
118 };
119
120 Data.prototype.writeShort = function(val) {
121 return this.writeInt16(val);
122 };
123
124 Data.prototype.readLongLong = function() {
125 var b1, b2, b3, b4, b5, b6, b7, b8;
126 b1 = this.readByte();
127 b2 = this.readByte();
128 b3 = this.readByte();
129 b4 = this.readByte();
130 b5 = this.readByte();
131 b6 = this.readByte();
132 b7 = this.readByte();
133 b8 = this.readByte();
134 if (b1 & 0x80) {
135 return ((b1 ^ 0xff) * 0x100000000000000 + (b2 ^ 0xff) * 0x1000000000000 + (b3 ^ 0xff) * 0x10000000000 + (b4 ^ 0xff) * 0x100000000 + (b5 ^ 0xff) * 0x1000000 + (b6 ^ 0xff) * 0x10000 + (b7 ^ 0xff) * 0x100 + (b8 ^ 0xff) + 1) * -1;
136 }
137 return b1 * 0x100000000000000 + b2 * 0x1000000000000 + b3 * 0x10000000000 + b4 * 0x100000000 + b5 * 0x1000000 + b6 * 0x10000 + b7 * 0x100 + b8;
138 };
139
140 Data.prototype.writeLongLong = function(val) {
141 var high, low;
142 high = Math.floor(val / 0x100000000);
143 low = val & 0xffffffff;
144 this.writeByte((high >> 24) & 0xff);
145 this.writeByte((high >> 16) & 0xff);
146 this.writeByte((high >> 8) & 0xff);
147 this.writeByte(high & 0xff);
148 this.writeByte((low >> 24) & 0xff);
149 this.writeByte((low >> 16) & 0xff);
150 this.writeByte((low >> 8) & 0xff);
151 return this.writeByte(low & 0xff);
152 };
153
154 Data.prototype.readInt = function() {
155 return this.readInt32();
156 };
157
158 Data.prototype.writeInt = function(val) {
159 return this.writeInt32(val);
160 };
161
162 Data.prototype.slice = function(start, end) {
163 return this.data.slice(start, end);
164 };
165
166 Data.prototype.read = function(bytes) {
167 var buf, i, j, ref;
168 buf = [];
169 for (i = j = 0, ref = bytes; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
170 buf.push(this.readByte());
171 }
172 return buf;
173 };
174
175 Data.prototype.write = function(bytes) {
176 var byte, j, len, results;
177 results = [];
178 for (j = 0, len = bytes.length; j < len; j++) {
179 byte = bytes[j];
180 results.push(this.writeByte(byte));
181 }
182 return results;
183 };
184
185 return Data;
186
187 })();
188
189 module.exports = Data;
190
191}).call(this);