Uuids.kt

  1. package com.hexagontk.helpers

  2. import com.hexagontk.core.text.decodeBase64
  3. import com.hexagontk.core.text.encodeToBase64
  4. import java.nio.ByteBuffer
  5. import java.util.*

  6. /**
  7.  * .
  8.  *
  9.  * @receiver .
  10.  * @return .
  11.  */
  12. fun UUID.bytes(): ByteArray =
  13.     ByteBuffer.wrap(ByteArray(16)).let {
  14.         it.putLong(this.mostSignificantBits)
  15.         it.putLong(this.leastSignificantBits)
  16.         it.array()
  17.     }

  18. /**
  19.  * .
  20.  *
  21.  * @receiver .
  22.  * @return .
  23.  */
  24. fun UUID.toBase64(): String =
  25.     bytes().encodeToBase64()

  26. /**
  27.  * .
  28.  *
  29.  * @param text .
  30.  * @return .
  31.  */
  32. fun uuid(text: String): UUID =
  33.     if (text[8] == '-') UUID.fromString(text)
  34.     else uuid(text.decodeBase64())

  35. /**
  36.  * .
  37.  *
  38.  * @param bytes .
  39.  * @return .
  40.  */
  41. fun uuid(bytes: ByteArray): UUID =
  42.     ByteBuffer.wrap(bytes).let { UUID(it.long, it.long) }