summaryrefslogtreecommitdiff
path: root/runtime/nativeMain/src/kotlinx/io/Buffers.kt
blob: 6acaefa5bb4592fab8390c5cd3fdc9497bdcce2c (plain)
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
/*
 *  Copyright 2018 JetBrains s.r.o.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package kotlinx.io

actual class ByteBuffer private constructor(private var backingArray: ByteArray) {

    private var idx = 0

    actual companion object {
        actual fun allocate(capacity: Int): ByteBuffer = ByteBuffer(ByteArray(capacity))
    }

    private var order: ByteOrder = ByteOrder.BIG_ENDIAN

    actual fun order(order: ByteOrder): ByteBuffer {
        this.order = order
        return this
    }

    actual fun clear(): ByteBuffer {
        backingArray = ByteArray(backingArray.size)
        idx = 0
        return this
    }

    actual fun flip(): ByteBuffer {
        idx = 0
        return this
    }

    actual fun get(): Byte {
        return backingArray[idx++]
    }

    actual fun getChar() = transformToInt(backingArray[idx++], backingArray[idx++], bytesToRead = 2).toChar()
    actual fun getShort() = transformToInt(backingArray[idx++], backingArray[idx++], bytesToRead = 2).toShort()
    actual fun getInt() = transformToInt(
            backingArray[idx++],
            backingArray[idx++],
            backingArray[idx++],
            backingArray[idx++],
            bytesToRead = 4
    )

    actual fun getFloat() = Float.fromBits(
            transformToInt(
                    backingArray[idx++],
                    backingArray[idx++],
                    backingArray[idx++],
                    backingArray[idx++],
                    bytesToRead = 4
            )
    )

    private fun transformToInt(vararg bytes: Byte, bytesToRead: Int): Int {
        if (order != ByteOrder.LITTLE_ENDIAN) {
            var r: Int = 0
            if (bytesToRead > 2) {
                r = r or (((bytes.getOrNull(0) ?: 0).toInt() and 0xFF) shl 24)
                r = r or (((bytes.getOrNull(1) ?: 0).toInt() and 0xFF) shl 16)
            }
            val offset = if (bytesToRead == 2) 0 else 2
            r = r or (((bytes.getOrNull(offset) ?: 0).toInt() and 0xFF) shl 8)
            r = r or (((bytes.getOrNull(offset + 1) ?: 0).toInt() and 0xFF) shl 0)
            return r
        } else {
            var r: Int = 0
            r = r or (((bytes.getOrNull(0) ?: 0).toInt() and 0xFF) shl 0)
            r = r or (((bytes.getOrNull(1) ?: 0).toInt() and 0xFF) shl 8)
            r = r or (((bytes.getOrNull(2) ?: 0).toInt() and 0xFF) shl 16)
            r = r or (((bytes.getOrNull(3) ?: 0).toInt() and 0xFF) shl 24)
            return r
        }
    }

    private fun transformFromInt(i: Int, bytesToWrite: Int) {
        val b0 = (i ushr 24).toByte()
        val b1 = (i ushr 16).toByte()
        val b2 = (i ushr 8).toByte()
        val b3 = (i).toByte()
        if (order != ByteOrder.LITTLE_ENDIAN) {
            if (bytesToWrite > 2) {
                backingArray[idx++] = b0
                backingArray[idx++] = b1
            }
            backingArray[idx++] = b2
            backingArray[idx++] = b3
        } else {
            backingArray[idx++] = b3
            if (bytesToWrite < 2) return
            backingArray[idx++] = b2
            if (bytesToWrite < 4) return
            backingArray[idx++] = b1
            backingArray[idx++] = b0
        }
    }

    actual fun put(value: Byte): ByteBuffer {
        backingArray[idx++] = value
        return this
    }

    actual fun putChar(value: Char): ByteBuffer {
        transformFromInt(value.toInt() and 0xFFFF, 2)
        return this
    }

    actual fun putShort(value: Short): ByteBuffer {
        transformFromInt(value.toInt() and 0xFFFF, 2)
        return this
    }

    actual fun putInt(value: Int): ByteBuffer {
        transformFromInt(value, 4)
        return this
    }

    actual fun putFloat(value: Float): ByteBuffer {
        transformFromInt(value.toBits(), 4)
        return this
    }

    actual fun array(): ByteArray {
        return backingArray
    }

    actual fun put(src: ByteArray): ByteBuffer {
        for (i in src) backingArray[idx++] = i
        return this
    }

    actual fun getLong(): Long {
        val baseOffset = idx
        var bytes: Long = 0
        if (order != ByteOrder.LITTLE_ENDIAN) {
            for (i in 0..7) {
                bytes = bytes shl 8
                bytes = bytes or (backingArray[baseOffset + i].toInt() and 0xFF).toLong()
            }
        } else {
            for (i in 7 downTo 0) {
                bytes = bytes shl 8
                bytes = bytes or (backingArray[baseOffset + i].toInt() and 0xFF).toLong()
            }
        }
        idx += 8
        return bytes
    }

    actual fun getDouble(): Double {
        return Double.fromBits(getLong())
    }

    @Suppress("NAME_SHADOWING")
    actual fun putLong(value: Long): ByteBuffer {
        var value = value
        val baseOffset = idx
        if (order != ByteOrder.LITTLE_ENDIAN) {
            for (i in 7 downTo 0) {
                backingArray[baseOffset + i] = (value and 0xFF).toByte()
                value = value shr 8
            }
        } else {
            for (i in 0..7) {
                backingArray[baseOffset + i] = (value and 0xFF).toByte()
                value = value shr 8
            }
        }
        idx += 8
        return this
    }

    actual fun putDouble(value: Double): ByteBuffer {
        return putLong(value.toBits())
    }

    actual fun get(index: Int): Byte {
        TODO("Native is not supported yet")
    }

    actual fun get(dst: ByteArray, offset: Int, cnt: Int) {
        TODO("Native is not supported yet")
    }

    actual fun getChar(index: Int): Char {
        TODO("Native is not supported yet")
    }

    actual fun getShort(index: Int): Short {
        TODO("Native is not supported yet")
    }

    actual fun getInt(index: Int): Int {
        TODO("Native is not supported yet")
    }

    actual fun getLong(index: Int): Long {
        TODO("Native is not supported yet")
    }

    actual fun getFloat(index: Int): Float {
        TODO("Native is not supported yet")
    }

    actual fun getDouble(index: Int): Double {
        TODO("Native is not supported yet")
    }

    actual fun put(value: Byte, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun put(src: ByteArray, offset: Int, cnt: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putChar(value: Char, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putShort(value: Short, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putInt(value: Int, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putLong(value: Long, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putFloat(value: Float, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }

    actual fun putDouble(value: Double, index: Int): ByteBuffer {
        TODO("Native is not supported yet")
    }
}