@@ -2,7 +2,6 @@ package io.rebble.libpebblecommon.util
22
33import kotlinx.cinterop.*
44import platform.Foundation.*
5- import platform.darwin.UInt8Var
65
76actual class DataBuffer {
87 private val actualBuf: NSMutableData
@@ -23,14 +22,14 @@ actual class DataBuffer {
2322 get() = _readPosition
2423
2524 actual constructor (size: Int ) {
26- actualBuf = NSMutableData .dataWithCapacity(size.toULong( ))!!
25+ actualBuf = NSMutableData .dataWithCapacity(castToNativeSize(size ))!!
2726 }
2827
2928 actual constructor (bytes: UByteArray ) {
3029 actualBuf = NSMutableData ()
3130 memScoped {
3231 actualBuf.setData(
33- NSData .create(bytes = allocArrayOf(bytes.asByteArray()), length = bytes.size.toULong( ))
32+ NSData .create(bytes = allocArrayOf(bytes.asByteArray()), length = castToNativeSize( bytes.size))
3433 )
3534 }
3635 }
@@ -47,13 +46,13 @@ actual class DataBuffer {
4746 memScoped {
4847 val pShort = alloc<UShortVar >()
4948 pShort.value = if (shouldReverse()) reverseOrd(short) else short
50- actualBuf.appendBytes(pShort.ptr, UShort .SIZE_BYTES .toULong( ))
49+ actualBuf.appendBytes(pShort.ptr, castToNativeSize( UShort .SIZE_BYTES ))
5150 }
5251 }
5352 actual fun getUShort (): UShort {
5453 memScoped {
5554 val pShort = alloc<UShortVar >()
56- actualBuf.getBytes(pShort.ptr, NSMakeRange (_readPosition .toULong( ), UShort .SIZE_BYTES .toULong( )))
55+ actualBuf.getBytes(pShort.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( UShort .SIZE_BYTES )))
5756 _readPosition + = UShort .SIZE_BYTES
5857 return if (shouldReverse()) reverseOrd(pShort.value) else pShort.value
5958 }
@@ -63,13 +62,13 @@ actual class DataBuffer {
6362 memScoped {
6463 val pShort = alloc<ShortVar >()
6564 pShort.value = if (shouldReverse()) reverseOrd(short.toUShort()).toShort() else short
66- actualBuf.appendBytes(pShort.ptr, Short .SIZE_BYTES .toULong( ))
65+ actualBuf.appendBytes(pShort.ptr, castToNativeSize( Short .SIZE_BYTES ))
6766 }
6867 }
6968 actual fun getShort (): Short {
7069 memScoped {
7170 val pShort = alloc<ShortVar >()
72- actualBuf.getBytes(pShort.ptr, NSMakeRange (_readPosition .toULong( ), Short .SIZE_BYTES .toULong( )))
71+ actualBuf.getBytes(pShort.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( Short .SIZE_BYTES )))
7372 _readPosition + = Short .SIZE_BYTES
7473 return if (shouldReverse()) reverseOrd(pShort.value.toUShort()).toShort() else pShort.value
7574 }
@@ -79,13 +78,13 @@ actual class DataBuffer {
7978 memScoped {
8079 val pByte = alloc<UByteVar >()
8180 pByte.value = byte
82- actualBuf.appendBytes(pByte.ptr, UByte .SIZE_BYTES .toULong( ))
81+ actualBuf.appendBytes(pByte.ptr, castToNativeSize( UByte .SIZE_BYTES ))
8382 }
8483 }
8584 actual fun getUByte (): UByte {
8685 memScoped {
8786 val pByte = alloc<UByteVar >()
88- actualBuf.getBytes(pByte.ptr, NSMakeRange (_readPosition .toULong( ), UByte .SIZE_BYTES .toULong( )))
87+ actualBuf.getBytes(pByte.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( UByte .SIZE_BYTES )))
8988 _readPosition + = UByte .SIZE_BYTES
9089 return pByte.value
9190 }
@@ -95,13 +94,13 @@ actual class DataBuffer {
9594 memScoped {
9695 val pByte = alloc<ByteVar >()
9796 pByte.value = byte
98- actualBuf.appendBytes(pByte.ptr, Byte .SIZE_BYTES .toULong( ))
97+ actualBuf.appendBytes(pByte.ptr, castToNativeSize( Byte .SIZE_BYTES ))
9998 }
10099 }
101100 actual fun getByte (): Byte {
102101 memScoped {
103102 val pByte = alloc<ByteVar >()
104- actualBuf.getBytes(pByte.ptr, NSMakeRange (_readPosition .toULong( ), Byte .SIZE_BYTES .toULong( )))
103+ actualBuf.getBytes(pByte.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( Byte .SIZE_BYTES )))
105104 _readPosition + = Byte .SIZE_BYTES
106105 return pByte.value
107106 }
@@ -110,13 +109,13 @@ actual class DataBuffer {
110109 actual fun putBytes (bytes : UByteArray ) {
111110 memScoped {
112111 val pBytes = allocArrayOf(bytes.toByteArray())
113- actualBuf.appendBytes(pBytes, bytes.size.toULong( ))
112+ actualBuf.appendBytes(pBytes, castToNativeSize( bytes.size))
114113 }
115114 }
116115 actual fun getBytes (count : Int ): UByteArray {
117116 memScoped {
118117 val pBytes = allocArray<UByteVar >(count)
119- actualBuf.getBytes(pBytes.getPointer(this ), NSMakeRange (_readPosition .toULong( ), count.toULong( )))
118+ actualBuf.getBytes(pBytes.getPointer(this ), NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize(count )))
120119 _readPosition + = count
121120 return pBytes.readBytes(count).toUByteArray()
122121 }
@@ -132,13 +131,13 @@ actual class DataBuffer {
132131 memScoped {
133132 val pUInt = alloc<UIntVar >()
134133 pUInt.value = if (shouldReverse()) reverseOrd(uint) else uint
135- actualBuf.appendBytes(pUInt.ptr, UInt .SIZE_BYTES .toULong( ))
134+ actualBuf.appendBytes(pUInt.ptr, castToNativeSize( UInt .SIZE_BYTES ))
136135 }
137136 }
138137 actual fun getUInt (): UInt {
139138 memScoped {
140139 val pUInt = alloc<UIntVar >()
141- actualBuf.getBytes(pUInt.ptr, NSMakeRange (_readPosition .toULong( ), UInt .SIZE_BYTES .toULong( )))
140+ actualBuf.getBytes(pUInt.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( UInt .SIZE_BYTES )))
142141 _readPosition + = UInt .SIZE_BYTES
143142 return if (shouldReverse()) reverseOrd(pUInt.value) else pUInt.value
144143 }
@@ -148,13 +147,13 @@ actual class DataBuffer {
148147 memScoped {
149148 val pInt = alloc<IntVar >()
150149 pInt.value = if (shouldReverse()) reverseOrd(int.toUInt()).toInt() else int
151- actualBuf.appendBytes(pInt.ptr, Int .SIZE_BYTES .toULong( ))
150+ actualBuf.appendBytes(pInt.ptr, castToNativeSize( Int .SIZE_BYTES ))
152151 }
153152 }
154153 actual fun getInt (): Int {
155154 memScoped {
156155 val pInt = alloc<IntVar >()
157- actualBuf.getBytes(pInt.ptr, NSMakeRange (_readPosition .toULong( ), Int .SIZE_BYTES .toULong( )))
156+ actualBuf.getBytes(pInt.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( Int .SIZE_BYTES )))
158157 _readPosition + = Int .SIZE_BYTES
159158 return if (shouldReverse()) reverseOrd(pInt.value.toUInt()).toInt() else pInt.value
160159 }
@@ -164,13 +163,13 @@ actual class DataBuffer {
164163 memScoped {
165164 val pULong = alloc<ULongVar >()
166165 pULong.value = if (shouldReverse()) reverseOrd(ulong) else ulong
167- actualBuf.appendBytes(pULong.ptr, ULong .SIZE_BYTES .toULong( ))
166+ actualBuf.appendBytes(pULong.ptr, castToNativeSize( ULong .SIZE_BYTES ))
168167 }
169168 }
170169 actual fun getULong (): ULong {
171170 memScoped {
172171 val pULong = alloc<ULongVar >()
173- actualBuf.getBytes(pULong.ptr, NSMakeRange (_readPosition .toULong( ), ULong .SIZE_BYTES .toULong( )))
172+ actualBuf.getBytes(pULong.ptr, NSMakeRange (castToNativeSize( _readPosition ), castToNativeSize( ULong .SIZE_BYTES )))
174173 _readPosition + = ULong .SIZE_BYTES
175174 return if (shouldReverse()) reverseOrd(pULong.value) else pULong.value
176175 }
0 commit comments