Repository navigation
Expand file tree
/
Copy pathByteArray.as
More file actions
296 lines (243 loc) · 7.25 KB
/
Copy pathByteArray.as
File metadata and controls
296 lines (243 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package flash.utils
{
/**
* ...
* @author lizhi http://matrix3d.github.io/
*/
//http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html
public class ByteArray
{
private static var _defaultObjectEncoding:uint;
public var dataView:DataView;
private var _endian:String = Endian.BIG_ENDIAN;
private var isLittleEndian:Boolean = false;
private var _position:int = 0;
private var _length:int = 0;
public function ByteArray()
{
dataView = new DataView(new ArrayBuffer(0));
super();
}
public static function get defaultObjectEncoding():uint { return 0 }
public static function set defaultObjectEncoding(param1:uint):void {/**/ }
public function readBytes(b:ByteArray, offset:uint = 0, length:uint = 0):void {
b.position = offset;
b.writeBytes(this, position, length);
}
public function writeBytes(b:ByteArray, offset:uint = 0, length:uint = 0):void {
if (length===0) {
length = b.length - offset;
}
beforWrite(length);
b.position = offset;
for (var i:int = 0; i < length;i++ ) {
writeByte(b.readByte());
}
}
public function writeBoolean(v:Boolean):void {
beforWrite(1);
dataView.setInt8(_position, v?1:0);
_position++;
}
public function writeByte(v:int):void {
beforWrite(1);
dataView.setInt8(_position, v);
_position++;
}
public function writeShort(v:int):void {
beforWrite(2);
dataView.setInt16(_position, v, isLittleEndian);
_position += 2;
}
public function writeInt(v:int):void {
beforWrite(4);
dataView.setInt32(_position, v, isLittleEndian);
_position += 4;
}
public function writeUnsignedInt(v:uint):void {
beforWrite(4);
dataView.setUint32(_position, v, isLittleEndian);
_position += 4;
}
public function writeFloat(v:Number):void {
beforWrite(4);
dataView.setFloat32(_position, v, isLittleEndian);
_position += 4;
}
public function writeDouble(v:Number):void {
beforWrite(8);
dataView.setFloat64(_position, v, isLittleEndian);
_position += 8;
}
public function writeMultiByte(str:String, charSet:String):void {
try{
var encoder:TextEncoder = new TextEncoder(charSet);
var u8:Uint8Array = encoder.encode(str);
}catch(err:Object){
u8 = new Uint8Array(str.split('').map(function(c:String):Number { return c.charCodeAt(0); } ));
}
for (var i:int = 0; i < u8.length;i++ ) {
writeByte(u8[i]);
}
}
public function writeUTF(v:String):void {
_position += 2;
var start:int = _position;
writeUTFBytes(v);
var end:int = _position;
_position = start - 2;
writeShort(start - end);
_position = end;
}
public function writeUTFBytes(v:String):void
{
writeMultiByte(v, "utf-8");
}
public function readBoolean():Boolean {
var v:Boolean = dataView.getInt8(_position) != 0;
_position++;
return v;
}
public function readByte():int {
var v:int = dataView.getInt8(_position);
_position++;
return v;
}
public function readUnsignedByte():uint {
var v:uint = dataView.getUint8(_position);
_position++;
return v;
}
public function readShort():int {
var v:int = dataView.getInt16(_position,isLittleEndian);
_position+=2;
return v;
}
public function readUnsignedShort():uint {
var v:uint = dataView.getUint16(_position,isLittleEndian);
_position+=2;
return v;
}
public function readInt():int {
var v:int = dataView.getInt32(_position,isLittleEndian);
_position+=4;
return v;
}
public function readUnsignedInt():uint {
var v:uint = dataView.getUint32(_position,isLittleEndian);
_position+=4;
return v;
}
public function readFloat():Number {
var v:Number = dataView.getFloat32(_position,isLittleEndian);
_position+=4;
return v;
}
public function readDouble():Number {
var v:Number = dataView.getFloat64(_position,isLittleEndian);
_position+=8;
return v;
}
public function readMultiByte(length:uint, charSet:String):String {
try{
var u8:Uint8Array = new Uint8Array(length);
u8.set(new Uint8Array(_slice(dataView.buffer,_position, _position + length)));
var decoder:TextDecoder = new TextDecoder(charSet);
var str:String = decoder.decode(u8);
}catch (err:Object){
//str = String.fromCharCode.apply(null, u8);
str = "";
for (var i:int = 0; i < length;i++ ){
str += String.fromCharCode(dataView.getUint8(_position + i));
}
}
_position += length;
return str;
}
public function readUTF():String {
return readUTFBytes(readUnsignedShort());
}
public function readUTFBytes(length:uint):String {
return readMultiByte(length, "utf-8");
}
public function get length():uint {
return _length;//dataView.byteLength;
}
public function set length(v:uint):void {
_length = v;
var u8:Uint8Array = new Uint8Array(v);
u8.set(new Uint8Array(dataView.buffer.byteLength > v?_slice(dataView.buffer,0, v):dataView.buffer));
dataView = new DataView(u8.buffer);
}
private function beforWrite(len:int):void {
if ((_position+len) > length) {
length = _position + len;
}
}
public function writeObject(param1:*):void {/**/ }
public function readObject():* { return null }
public function deflate():void
{
this._compress("deflate");
}
private function _compress(param1:String):void {/**/ }
public function compress(algorithm:String = "zlib"):void
{
this._compress(algorithm);
}
public function inflate():void
{
this._uncompress("deflate");
}
private function _uncompress(param1:String):void {/**/ }
public function uncompress(algorithm:String = "zlib"):void
{
this._uncompress(algorithm);
}
public function toString():String
{
return this._toString();
}
private function _toString():String {
position = 0;
return readUTFBytes(length);
}
public function get bytesAvailable():uint {
return length - position;
}
public function get position():uint { return _position }
public function set position(p:uint):void {
_position = p;
}
public function get objectEncoding():uint { return 0 }
public function set objectEncoding(param1:uint):void {/**/ }
public function get endian():String { return _endian; }
public function set endian(v:String):void {
_endian = v;
isLittleEndian = v === Endian.LITTLE_ENDIAN;
}
public function clear():void {
position = 0;
length = 0;
}
public function atomicCompareAndSwapIntAt(param1:int, param2:int, param3:int):int { return 0 }
public function atomicCompareAndSwapLength(param1:int, param2:int):int { return 0 }
public function get shareable():Boolean { return false }
public function set shareable(param1:Boolean):void {/**/ }
//http://stackoverflow.com/questions/21440050/arraybuffer-prototype-slice-shim-for-ie
private static function _slice(buff:ArrayBuffer, begin:Number, end:Number):ArrayBuffer{
try{
var newbuffer:ArrayBuffer = buff.slice(begin, end);
}catch (err:Object){
if (end===0){
end = buff.byteLength;
}
newbuffer = new ArrayBuffer(end - begin);
var rb:Uint8Array = new Uint8Array(newbuffer);
var sb:Uint8Array = new Uint8Array(buff, begin, end - begin);
rb.set(sb);
}
return newbuffer;
}
}
}