@@ -22,47 +22,55 @@ actual class DataBuffer {
2222 get() = _readPosition
2323
2424 actual constructor (size: Int ) {
25- actualBuf = NSMutableData .dataWithLength(size.toULong())!!
26- actualBuf.setLength(size.toULong())
25+ actualBuf = NSMutableData .dataWithCapacity(size.toULong())!!
2726 }
2827
2928 actual constructor (bytes: UByteArray ) {
3029 actualBuf = NSMutableData ()
31- actualBuf.setData(
32- NSString .create(string = bytes.toString())
33- .dataUsingEncoding(NSUTF8StringEncoding , false )!!
34- )
30+ memScoped {
31+ actualBuf.setData(
32+ NSData .create(bytes = allocArrayOf(bytes.toByteArray()), length = bytes.size.toULong())
33+ )
34+ }
35+ }
36+
37+ private fun shouldReverse (): Boolean {
38+ return if (isPlatformBigEndian() && ! littleEndian) {
39+ false
40+ }else if (isPlatformBigEndian() && littleEndian) {
41+ true
42+ }else ! isPlatformBigEndian() && ! littleEndian
3543 }
3644
3745 actual fun putUShort (short : UShort ) {
3846 memScoped {
3947 val pShort = alloc<UShortVar >()
40- pShort.value = short
48+ pShort.value = if (shouldReverse()) reverseOrd(short) else short
4149 actualBuf.appendBytes(pShort.ptr, UShort .SIZE_BYTES .toULong())
4250 }
4351 }
4452 actual fun getUShort (): UShort {
4553 memScoped {
4654 val pShort = alloc<UShortVar >()
47- actualBuf.getBytes(pShort.ptr, UShort .SIZE_BYTES .toULong())
55+ actualBuf.getBytes(pShort.ptr, NSMakeRange ( _readPosition .toULong(), UShort .SIZE_BYTES .toULong() ))
4856 _readPosition + = UShort .SIZE_BYTES
49- return pShort.value
57+ return if (shouldReverse()) reverseOrd(pShort.value) else pShort.value
5058 }
5159 }
5260
5361 actual fun putShort (short : Short ) {
5462 memScoped {
5563 val pShort = alloc<ShortVar >()
56- pShort.value = short
64+ pShort.value = if (shouldReverse()) reverseOrd(short.toUShort()).toShort() else short
5765 actualBuf.appendBytes(pShort.ptr, Short .SIZE_BYTES .toULong())
5866 }
5967 }
6068 actual fun getShort (): Short {
6169 memScoped {
6270 val pShort = alloc<ShortVar >()
63- actualBuf.getBytes(pShort.ptr, Short .SIZE_BYTES .toULong())
71+ actualBuf.getBytes(pShort.ptr, NSMakeRange ( _readPosition .toULong(), Short .SIZE_BYTES .toULong() ))
6472 _readPosition + = Short .SIZE_BYTES
65- return pShort.value
73+ return if (shouldReverse()) reverseOrd(pShort.value.toUShort()).toShort() else pShort.value
6674 }
6775 }
6876
@@ -76,7 +84,7 @@ actual class DataBuffer {
7684 actual fun getUByte (): UByte {
7785 memScoped {
7886 val pByte = alloc<UByteVar >()
79- actualBuf.appendBytes (pByte.ptr, UByte .SIZE_BYTES .toULong())
87+ actualBuf.getBytes (pByte.ptr, NSMakeRange ( _readPosition .toULong(), UByte .SIZE_BYTES .toULong() ))
8088 _readPosition + = UByte .SIZE_BYTES
8189 return pByte.value
8290 }
@@ -92,7 +100,7 @@ actual class DataBuffer {
92100 actual fun getByte (): Byte {
93101 memScoped {
94102 val pByte = alloc<ByteVar >()
95- actualBuf.appendBytes (pByte.ptr, Byte .SIZE_BYTES .toULong())
103+ actualBuf.getBytes (pByte.ptr, NSMakeRange ( _readPosition .toULong(), Byte .SIZE_BYTES .toULong() ))
96104 _readPosition + = Byte .SIZE_BYTES
97105 return pByte.value
98106 }
@@ -107,7 +115,7 @@ actual class DataBuffer {
107115 actual fun getBytes (count : Int ): UByteArray {
108116 memScoped {
109117 val pBytes = allocArray<UByteVar >(count)
110- actualBuf.getBytes(pBytes.getPointer(this ), length = count.toULong())
118+ actualBuf.getBytes(pBytes.getPointer(this ), NSMakeRange ( _readPosition .toULong(), count.toULong() ))
111119 _readPosition + = count
112120 return pBytes.readBytes(count).toUByteArray()
113121 }
@@ -117,54 +125,53 @@ actual class DataBuffer {
117125
118126 actual fun setEndian (endian : Char ) {
119127 littleEndian = endian == ' <'
120- if (littleEndian) TODO (" iOS little endian" )
121128 }
122129
123130 actual fun putUInt (uint : UInt ) {
124131 memScoped {
125132 val pUInt = alloc<UIntVar >()
126- pUInt.value = uint
133+ pUInt.value = if (shouldReverse()) reverseOrd(uint) else uint
127134 actualBuf.appendBytes(pUInt.ptr, UInt .SIZE_BYTES .toULong())
128135 }
129136 }
130137 actual fun getUInt (): UInt {
131138 memScoped {
132139 val pUInt = alloc<UIntVar >()
133- actualBuf.getBytes(pUInt.ptr, UInt .SIZE_BYTES .toULong())
140+ actualBuf.getBytes(pUInt.ptr, NSMakeRange ( _readPosition .toULong(), UInt .SIZE_BYTES .toULong() ))
134141 _readPosition + = UInt .SIZE_BYTES
135- return pUInt.value
142+ return if (shouldReverse()) reverseOrd(pUInt.value) else pUInt.value
136143 }
137144 }
138145
139146 actual fun putInt (int : Int ) {
140147 memScoped {
141148 val pInt = alloc<IntVar >()
142- pInt.value = int
149+ pInt.value = if (shouldReverse()) reverseOrd(int.toUInt()).toInt() else int
143150 actualBuf.appendBytes(pInt.ptr, Int .SIZE_BYTES .toULong())
144151 }
145152 }
146153 actual fun getInt (): Int {
147154 memScoped {
148155 val pInt = alloc<IntVar >()
149- actualBuf.getBytes(pInt.ptr, Int .SIZE_BYTES .toULong())
156+ actualBuf.getBytes(pInt.ptr, NSMakeRange ( _readPosition .toULong(), Int .SIZE_BYTES .toULong() ))
150157 _readPosition + = Int .SIZE_BYTES
151- return pInt.value
158+ return if (shouldReverse()) reverseOrd(pInt.value.toUInt()).toInt() else pInt.value
152159 }
153160 }
154161
155162 actual fun putULong (ulong : ULong ) {
156163 memScoped {
157164 val pULong = alloc<ULongVar >()
158- pULong.value = ulong
165+ pULong.value = if (shouldReverse()) reverseOrd(ulong) else ulong
159166 actualBuf.appendBytes(pULong.ptr, ULong .SIZE_BYTES .toULong())
160167 }
161168 }
162169 actual fun getULong (): ULong {
163170 memScoped {
164171 val pULong = alloc<ULongVar >()
165- actualBuf.getBytes(pULong.ptr, ULong .SIZE_BYTES .toULong())
172+ actualBuf.getBytes(pULong.ptr, NSMakeRange ( _readPosition .toULong(), ULong .SIZE_BYTES .toULong() ))
166173 _readPosition + = ULong .SIZE_BYTES
167- return pULong.value
174+ return if (shouldReverse()) reverseOrd(pULong.value) else pULong.value
168175 }
169176 }
170177
0 commit comments