blob: 54f76772879d16bdf2ea915d236e45cf9d538cc5 (
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
|
package io.dico.parcels2.storage
import java.lang.IllegalArgumentException
import java.nio.ByteBuffer
import java.util.UUID
/* For putting it into the database */
fun UUID.toByteArray(): ByteArray =
ByteBuffer.allocate(16).apply {
putLong(mostSignificantBits)
putLong(leastSignificantBits)
}.array()
/* For getting it out of the database */
fun ByteArray.toUUID(): UUID =
ByteBuffer.wrap(this).run {
val mostSignificantBits = getLong()
val leastSignificantBits = getLong()
UUID(mostSignificantBits, leastSignificantBits)
}
/* For putting it into the database */
fun IntArray.toByteArray(): ByteArray =
ByteBuffer.allocate(size * Int.SIZE_BYTES).also { buf ->
buf.asIntBuffer().put(this)
}.array()
/* For getting it out of the database */
fun ByteArray.toIntArray(): IntArray {
if (this.size % Int.SIZE_BYTES != 0)
throw IllegalArgumentException("Size must be divisible by ${Int.SIZE_BYTES}")
return ByteBuffer.wrap(this).run {
IntArray(remaining() / 4).also { array ->
asIntBuffer().get(array)
}
}
}
|