@@ -22,47 +22,55 @@ actual class DataBuffer {
22
22
get() = _readPosition
23
23
24
24
actual constructor (size: Int ) {
25
- actualBuf = NSMutableData .dataWithLength(size.toULong())!!
26
- actualBuf.setLength(size.toULong())
25
+ actualBuf = NSMutableData .dataWithCapacity(size.toULong())!!
27
26
}
28
27
29
28
actual constructor (bytes: UByteArray ) {
30
29
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
35
43
}
36
44
37
45
actual fun putUShort (short : UShort ) {
38
46
memScoped {
39
47
val pShort = alloc<UShortVar >()
40
- pShort.value = short
48
+ pShort.value = if (shouldReverse()) reverseOrd(short) else short
41
49
actualBuf.appendBytes(pShort.ptr, UShort .SIZE_BYTES .toULong())
42
50
}
43
51
}
44
52
actual fun getUShort (): UShort {
45
53
memScoped {
46
54
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() ))
48
56
_readPosition + = UShort .SIZE_BYTES
49
- return pShort.value
57
+ return if (shouldReverse()) reverseOrd(pShort.value) else pShort.value
50
58
}
51
59
}
52
60
53
61
actual fun putShort (short : Short ) {
54
62
memScoped {
55
63
val pShort = alloc<ShortVar >()
56
- pShort.value = short
64
+ pShort.value = if (shouldReverse()) reverseOrd(short.toUShort()).toShort() else short
57
65
actualBuf.appendBytes(pShort.ptr, Short .SIZE_BYTES .toULong())
58
66
}
59
67
}
60
68
actual fun getShort (): Short {
61
69
memScoped {
62
70
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() ))
64
72
_readPosition + = Short .SIZE_BYTES
65
- return pShort.value
73
+ return if (shouldReverse()) reverseOrd(pShort.value.toUShort()).toShort() else pShort.value
66
74
}
67
75
}
68
76
@@ -76,7 +84,7 @@ actual class DataBuffer {
76
84
actual fun getUByte (): UByte {
77
85
memScoped {
78
86
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() ))
80
88
_readPosition + = UByte .SIZE_BYTES
81
89
return pByte.value
82
90
}
@@ -92,7 +100,7 @@ actual class DataBuffer {
92
100
actual fun getByte (): Byte {
93
101
memScoped {
94
102
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() ))
96
104
_readPosition + = Byte .SIZE_BYTES
97
105
return pByte.value
98
106
}
@@ -107,7 +115,7 @@ actual class DataBuffer {
107
115
actual fun getBytes (count : Int ): UByteArray {
108
116
memScoped {
109
117
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() ))
111
119
_readPosition + = count
112
120
return pBytes.readBytes(count).toUByteArray()
113
121
}
@@ -117,54 +125,53 @@ actual class DataBuffer {
117
125
118
126
actual fun setEndian (endian : Char ) {
119
127
littleEndian = endian == ' <'
120
- if (littleEndian) TODO (" iOS little endian" )
121
128
}
122
129
123
130
actual fun putUInt (uint : UInt ) {
124
131
memScoped {
125
132
val pUInt = alloc<UIntVar >()
126
- pUInt.value = uint
133
+ pUInt.value = if (shouldReverse()) reverseOrd(uint) else uint
127
134
actualBuf.appendBytes(pUInt.ptr, UInt .SIZE_BYTES .toULong())
128
135
}
129
136
}
130
137
actual fun getUInt (): UInt {
131
138
memScoped {
132
139
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() ))
134
141
_readPosition + = UInt .SIZE_BYTES
135
- return pUInt.value
142
+ return if (shouldReverse()) reverseOrd(pUInt.value) else pUInt.value
136
143
}
137
144
}
138
145
139
146
actual fun putInt (int : Int ) {
140
147
memScoped {
141
148
val pInt = alloc<IntVar >()
142
- pInt.value = int
149
+ pInt.value = if (shouldReverse()) reverseOrd(int.toUInt()).toInt() else int
143
150
actualBuf.appendBytes(pInt.ptr, Int .SIZE_BYTES .toULong())
144
151
}
145
152
}
146
153
actual fun getInt (): Int {
147
154
memScoped {
148
155
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() ))
150
157
_readPosition + = Int .SIZE_BYTES
151
- return pInt.value
158
+ return if (shouldReverse()) reverseOrd(pInt.value.toUInt()).toInt() else pInt.value
152
159
}
153
160
}
154
161
155
162
actual fun putULong (ulong : ULong ) {
156
163
memScoped {
157
164
val pULong = alloc<ULongVar >()
158
- pULong.value = ulong
165
+ pULong.value = if (shouldReverse()) reverseOrd(ulong) else ulong
159
166
actualBuf.appendBytes(pULong.ptr, ULong .SIZE_BYTES .toULong())
160
167
}
161
168
}
162
169
actual fun getULong (): ULong {
163
170
memScoped {
164
171
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() ))
166
173
_readPosition + = ULong .SIZE_BYTES
167
- return pULong.value
174
+ return if (shouldReverse()) reverseOrd(pULong.value) else pULong.value
168
175
}
169
176
}
170
177
0 commit comments