diff options
author | Dico Karssiens <dico.karssiens@gmail.com> | 2019-01-10 18:52:51 +0000 |
---|---|---|
committer | Dico Karssiens <dico.karssiens@gmail.com> | 2019-01-10 18:52:51 +0000 |
commit | 37fa1f9bbfc852dfa67862cde3d11a4e5415db11 (patch) | |
tree | 9350ab43d11b294cd7218fec3b61fa83ea3beda8 /src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt | |
parent | d80f69488656485a8b93b4982e678ed62c5df96b (diff) | |
parent | 778e301efc09eb8326146a084bb6788be748e6f1 (diff) |
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt b/src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt index 80f41b2..54f7677 100644 --- a/src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt +++ b/src/main/kotlin/io/dico/parcels2/storage/DataConverters.kt @@ -1,38 +1,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) - } - } +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)
+ }
+ }
}
\ No newline at end of file |