UNPKG

openfl

Version:

A fast, productive library for 2D cross-platform development.

504 lines (489 loc) 17.4 kB
// Class: openfl.utils.ByteArrayData var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $import = require("./../../import_stub").default; var $extend = require("./../../extend_stub").default; function openfl_utils_IDataOutput() {return require("./../../openfl/utils/IDataOutput");} function openfl_utils_IDataInput() {return require("./../../openfl/utils/IDataInput");} function haxe_io_Bytes() {return require("./../../haxe/io/Bytes");} function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../openfl/utils/_ByteArray/ByteArray_Impl_");} function lime_utils__$Bytes_Bytes_$Impl_$() {return require("./../../lime/utils/_Bytes/Bytes_Impl_");} function lime_utils_CompressionAlgorithm() {return require("./../../lime/utils/CompressionAlgorithm");} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_errors_EOFError() {return require("./../../openfl/errors/EOFError");} function haxe_io_FPHelper() {return require("./../../haxe/io/FPHelper");} function haxe__$Int64__$_$_$Int64() {return require("./../../haxe/_Int64/___Int64");} function haxe_io_BytesInput() {return require("./../../haxe/io/BytesInput");} function openfl_utils__$internal_format_amf_AMFReader() {return require("./../../openfl/utils/_internal/format/amf/AMFReader");} function openfl_utils__$internal_format_amf_AMFTools() {return require("./../../openfl/utils/_internal/format/amf/AMFTools");} function openfl_utils__$internal_format_amf3_AMF3Reader() {return require("./../../openfl/utils/_internal/format/amf3/AMF3Reader");} function openfl_utils__$internal_format_amf3_AMF3Tools() {return require("./../../openfl/utils/_internal/format/amf3/AMF3Tools");} function haxe_Unserializer() {return require("./../../haxe/Unserializer");} function haxe_io_BytesOutput() {return require("./../../haxe/io/BytesOutput");} function openfl_utils__$internal_format_amf_AMFWriter() {return require("./../../openfl/utils/_internal/format/amf/AMFWriter");} function openfl_utils__$internal_format_amf3_AMF3Writer() {return require("./../../openfl/utils/_internal/format/amf3/AMF3Writer");} function openfl_utils__$internal_format_amf3_AMF3Value() {return require("./../../openfl/utils/_internal/format/amf3/AMF3Value");} function haxe_Serializer() {return require("./../../haxe/Serializer");} function lime_system_System() {return require("./../../lime/system/System");} function lime_system_Endian() {return require("./../../lime/system/Endian");} // Constructor var ByteArrayData = function(length) { if(length == null) { length = 0; } var bytes = (haxe_io_Bytes().default).alloc(length); (haxe_io_Bytes().default).call(this,bytes.b.buffer); this.__length = length; this.__allocated = length; this.set_endian(ByteArrayData.get_defaultEndian()); this.objectEncoding = ByteArrayData.defaultObjectEncoding; this.position = 0; } // Meta ByteArrayData.__name__ = "openfl.utils.ByteArrayData"; ByteArrayData.__isInterface__ = false; ByteArrayData.__interfaces__ = [(openfl_utils_IDataOutput().default),(openfl_utils_IDataInput().default)]; ByteArrayData.__super__ = (haxe_io_Bytes().default); ByteArrayData.prototype = $extend((haxe_io_Bytes().default).prototype, { openfljs_get_length: function() { return this.__length; }, openfljs_set_length: function(value) { return (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).set_length(this,value); }, clear: function() { this.__length = 0; this.position = 0; }, compress: function(algorithm) { if(algorithm == null) { algorithm = "zlib"; } if(this.__allocated > this.__length) { var cacheLength = this.__length; this.__length = this.__allocated; var data = (haxe_io_Bytes().default).alloc(cacheLength); data.blit(0,this,0,cacheLength); this.__setData(data); this.__length = cacheLength; } var limeBytes = this; var bytes; switch(algorithm) { case "deflate": bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).compress(limeBytes,(lime_utils_CompressionAlgorithm().default).DEFLATE); break; case "lzma": bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).compress(limeBytes,(lime_utils_CompressionAlgorithm().default).LZMA); break; default: bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).compress(limeBytes,(lime_utils_CompressionAlgorithm().default).ZLIB); } if(bytes != null) { this.__setData(bytes); this.__length = this.__allocated; this.position = this.__length; } }, deflate: function() { this.compress("deflate"); }, inflate: function() { this.uncompress("deflate"); }, readBoolean: function() { if(this.position < this.__length) { return this.get(this.position++) != 0; } else { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } }, readByte: function() { var value = this.readUnsignedByte(); if((value & 128) != 0) { return value - 256; } else { return value; } }, readBytes: function(bytes,offset,length) { if(length == null) { length = 0; } if(offset == null) { offset = 0; } if(length == 0) { length = this.__length - this.position; } if(this.position + length > this.__length) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } if(bytes.__length < offset + length) { bytes.__resize(offset + length); } bytes.blit(offset,this,this.position,length); this.position += length; }, readDouble: function() { if(this.get_endian() == "littleEndian") { if(this.position + 8 > this.__length) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } this.position += 8; return this.getDouble(this.position - 8); } else { var ch1 = this.readInt(); var ch2 = this.readInt(); return (haxe_io_FPHelper().default).i64ToDouble(ch2,ch1); } }, readFloat: function() { if(this.get_endian() == "littleEndian") { if(this.position + 4 > this.__length) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } this.position += 4; return this.getFloat(this.position - 4); } else { return (haxe_io_FPHelper().default).i32ToFloat(this.readInt()); } }, readInt: function() { var ch1 = this.readUnsignedByte(); var ch2 = this.readUnsignedByte(); var ch3 = this.readUnsignedByte(); var ch4 = this.readUnsignedByte(); if(this.get_endian() == "littleEndian") { return ch4 << 24 | ch3 << 16 | ch2 << 8 | ch1; } else { return ch1 << 24 | ch2 << 16 | ch3 << 8 | ch4; } }, readInt64: function() { if(this.position + 8 > this.length) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } var high; var low; if(this.get_endian() == "littleEndian") { low = this.readUnsignedInt(); high = this.readUnsignedInt(); } else { high = this.readUnsignedInt(); low = this.readUnsignedInt(); } var this1 = new (haxe__$Int64__$_$_$Int64().default)(high,low); return this1; }, readMultiByte: function(length,charSet) { return this.readUTFBytes(length); }, readObject: function() { switch(this.objectEncoding) { case 0: var input = new (haxe_io_BytesInput().default)(this,this.position); var reader = new (openfl_utils__$internal_format_amf_AMFReader().default)(input); var data = (openfl_utils__$internal_format_amf_AMFTools().default).unwrapValue(reader.read()); this.position = input.get_position(); return data; case 3: var input1 = new (haxe_io_BytesInput().default)(this,this.position); var reader1 = new (openfl_utils__$internal_format_amf3_AMF3Reader().default)(input1,this.__amf3Reader); var data1 = (openfl_utils__$internal_format_amf3_AMF3Tools().default).decode(reader1.read()); this.position = input1.get_position(); return data1; case 10: var data2 = this.readUTF(); return (haxe_Unserializer().default).run(data2); case 12: var data3 = this.readUTF(); return JSON.parse(data3); default: return null; } }, readShort: function() { var ch1 = this.readUnsignedByte(); var ch2 = this.readUnsignedByte(); var value; if(this.get_endian() == "littleEndian") { value = ch2 << 8 | ch1; } else { value = ch1 << 8 | ch2; } if((value & 32768) != 0) { return value - 65536; } else { return value; } }, readUnsignedByte: function() { if(this.position < this.__length) { return this.get(this.position++); } else { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } }, readUnsignedInt: function() { var ch1 = this.readUnsignedByte(); var ch2 = this.readUnsignedByte(); var ch3 = this.readUnsignedByte(); var ch4 = this.readUnsignedByte(); if(this.get_endian() == "littleEndian") { return ch4 << 24 | ch3 << 16 | ch2 << 8 | ch1; } else { return ch1 << 24 | ch2 << 16 | ch3 << 8 | ch4; } }, readUnsignedShort: function() { var ch1 = this.readUnsignedByte(); var ch2 = this.readUnsignedByte(); if(this.get_endian() == "littleEndian") { return (ch2 << 8) + ch1; } else { return ch1 << 8 | ch2; } }, readUTF: function() { var bytesCount = this.readUnsignedShort(); return this.readUTFBytes(bytesCount); }, readUTFBytes: function(length) { if(this.position + length > this.__length) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_EOFError().default)()); } this.position += length; return this.getString(this.position - length,length); }, uncompress: function(algorithm) { if(algorithm == null) { algorithm = "zlib"; } if(this.__allocated > this.__length) { var cacheLength = this.__length; this.__length = this.__allocated; var data = (haxe_io_Bytes().default).alloc(cacheLength); data.blit(0,this,0,cacheLength); this.__setData(data); this.__length = cacheLength; } var limeBytes = this; var bytes; switch(algorithm) { case "deflate": bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).decompress(limeBytes,(lime_utils_CompressionAlgorithm().default).DEFLATE); break; case "lzma": bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).decompress(limeBytes,(lime_utils_CompressionAlgorithm().default).LZMA); break; default: bytes = (lime_utils__$Bytes_Bytes_$Impl_$().default).decompress(limeBytes,(lime_utils_CompressionAlgorithm().default).ZLIB); } if(bytes != null) { this.__setData(bytes); this.__length = this.__allocated; } this.position = 0; }, writeBoolean: function(value) { this.writeByte(value ? 1 : 0); }, writeByte: function(value) { this.__resize(this.position + 1); this.set(this.position++,value & 255); }, writeBytes: function(bytes,offset,length) { if(length == null) { length = 0; } if(offset == null) { offset = 0; } if((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).get_length(bytes) == 0) { return; } if(length == 0) { length = (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).get_length(bytes) - offset; } this.__resize(this.position + length); this.blit(this.position,bytes,offset,length); this.position = this.position + length; }, writeDouble: function(value) { var int64 = (haxe_io_FPHelper().default).doubleToI64(value); if(this.get_endian() == "littleEndian") { this.writeInt(int64.low); this.writeInt(int64.high); } else { this.writeInt(int64.high); this.writeInt(int64.low); } }, writeFloat: function(value) { if(this.get_endian() == "littleEndian") { this.__resize(this.position + 4); this.setFloat(this.position,value); this.position += 4; } else { var int = (haxe_io_FPHelper().default).floatToI32(value); this.writeInt(int); } }, writeInt: function(value) { this.__resize(this.position + 4); if(this.get_endian() == "littleEndian") { this.set(this.position++,value & 255); this.set(this.position++,value >> 8 & 255); this.set(this.position++,value >> 16 & 255); this.set(this.position++,value >> 24 & 255); } else { this.set(this.position++,value >> 24 & 255); this.set(this.position++,value >> 16 & 255); this.set(this.position++,value >> 8 & 255); this.set(this.position++,value & 255); } }, writeInt64: function(value) { if(this.get_endian() == "littleEndian") { this.writeUnsignedInt(value.low); this.writeUnsignedInt(value.high); } else { this.writeUnsignedInt(value.high); this.writeUnsignedInt(value.low); } }, writeMultiByte: function(value,charSet) { this.writeUTFBytes(value); }, writeObject: function(object) { switch(this.objectEncoding) { case 0: var value = (openfl_utils__$internal_format_amf_AMFTools().default).encode(object); var output = new (haxe_io_BytesOutput().default)(); var writer = new (openfl_utils__$internal_format_amf_AMFWriter().default)(output); writer.write(value); this.writeBytes((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).fromBytes(output.getBytes())); break; case 3: var output1 = new (haxe_io_BytesOutput().default)(); var writer1 = new (openfl_utils__$internal_format_amf3_AMF3Writer().default)(output1); if(((object) instanceof ByteArrayData)) { writer1.write((openfl_utils__$internal_format_amf3_AMF3Value().default).AByteArray(object)); } else { var value1 = (openfl_utils__$internal_format_amf3_AMF3Tools().default).encode(object); writer1.write(value1); } this.writeBytes((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).fromBytes(output1.getBytes())); break; case 10: var value2 = (haxe_Serializer().default).run(object); this.writeUTF(value2); break; case 12: var value3 = JSON.stringify(object); this.writeUTF(value3); break; default: return; } }, writeShort: function(value) { this.__resize(this.position + 2); if(this.get_endian() == "littleEndian") { this.set(this.position++,value); this.set(this.position++,value >> 8); } else { this.set(this.position++,value >> 8); this.set(this.position++,value); } }, writeUnsignedInt: function(value) { this.writeInt(value); }, writeUTF: function(value) { var bytes = (haxe_io_Bytes().default).ofString(value); this.writeShort(bytes.length); this.writeBytes((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).fromBytes(bytes)); }, writeUTFBytes: function(value) { var bytes = (haxe_io_Bytes().default).ofString(value); this.writeBytes((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).fromBytes(bytes)); }, __fromBytes: function(bytes) { this.__setData(bytes); this.__length = bytes.length; }, __resize: function(size) { if(size > this.__allocated) { var bytes = (haxe_io_Bytes().default).alloc((size + 1) * 3 >> 1); if(this.__allocated > 0) { var cacheLength = this.__length; this.__length = this.__allocated; bytes.blit(0,this,0,this.__allocated); this.__length = cacheLength; } this.__setData(bytes); } if(this.__length < size) { this.__length = size; } }, __setData: function(bytes) { this.b = bytes.b; this.__allocated = bytes.length; this.data = bytes.data; }, get_bytesAvailable: function() { return this.__length - this.position; }, get_endian: function() { return this.__endian; }, set_endian: function(value) { return this.__endian = value; } }); ByteArrayData.prototype.__class__ = ByteArrayData.prototype.constructor = $hxClasses["openfl.utils.ByteArrayData"] = ByteArrayData; // Init { global.Object.defineProperty(ByteArrayData,"defaultEndian",{ get : ByteArrayData.get_defaultEndian, set : ByteArrayData.set_defaultEndian}); global.Object.defineProperties(ByteArrayData.prototype,{ bytesAvailable : { get : ByteArrayData.prototype.get_bytesAvailable}, endian : { get : ByteArrayData.prototype.get_endian, set : ByteArrayData.prototype.set_endian}, length : { get : ByteArrayData.prototype.openfljs_get_length, set : ByteArrayData.prototype.openfljs_set_length}}); }; // Statics ByteArrayData.fromArrayBuffer = function(buffer) { return (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).fromArrayBuffer(buffer); } ByteArrayData.fromBytes = function(bytes) { var result = new ByteArrayData(); result.__fromBytes(bytes); return result; } ByteArrayData.loadFromBytes = function(bytes) { return (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).loadFromBytes(bytes); } ByteArrayData.loadFromFile = function(path) { return (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).loadFromFile(path); } ByteArrayData.get_defaultEndian = function() { if(ByteArrayData.__defaultEndian == null) { if((lime_system_System().default).get_endianness() == (lime_system_Endian().default).LITTLE_ENDIAN) { ByteArrayData.__defaultEndian = "littleEndian"; } else { ByteArrayData.__defaultEndian = "bigEndian"; } } return ByteArrayData.__defaultEndian; } ByteArrayData.set_defaultEndian = function(value) { return ByteArrayData.__defaultEndian = value; } ByteArrayData.__meta__ = { obj : { SuppressWarnings : ["checkstyle:FieldDocComment"]}} ByteArrayData.defaultObjectEncoding = 10 ByteArrayData.__defaultEndian = null // Export exports.default = ByteArrayData;