java - 如何将 UUID 的 ArrayList 转换为 ByteArray?
问题描述
object UUIDConversion {
fun UUID.toByteArray() : ByteArray {
val byteBuffer = ByteBuffer.wrap(ByteArray(16))
byteBuffer.putLong(this.mostSignificantBits)
byteBuffer.putLong(this.leastSignificantBits)
return byteBuffer.array()
}
fun ByteArray.toUUID() : UUID {
val byteBuffer = ByteBuffer.wrap(this)
val mostSignificantBits = byteBuffer.long
val leastSignificantBits = byteBuffer.long
return UUID(mostSignificantBits, leastSignificantBits)
}
}
我有上面的代码将 UUID 转换为 ByteArray 并将其转换回来,但我还需要能够将 ArrayList 转换为 ByteArray 并将其转换回来。我该怎么做呢?如果您想知道为什么我需要这样做,那是因为我需要将 HashMap<UUID, ArrayList 存储在键值数据库中,并且我需要将其转换为 ByteArray 并重新使用它。
Java 或 Kotlin 的答案都很好。
解决方案
您可以只使用+
运算符来连接独立的 UUID 字节数组,如下所示:
val allUUIDs : ByteArray = listOfUUID.fold(ByteArray(0)) { buffer, uuid -> buffer + uuid.toByteArray() }
但是,如果您要转换的链很长,则性能可能会显着下降。相反,您可以使用专用方法来读取/写入字节缓冲区:
import java.nio.ByteBuffer
import java.util.*
import kotlin.RuntimeException
fun ByteBuffer.readUUIDs(nbUUIDs : Int = remaining()/16) : Sequence<UUID> {
if (nbUUIDs <= 0) return emptySequence()
val nbBytes = nbUUIDs * 16
// Defensive copy -> resulting sequence becomes independant from receiver buffer
val defCpy = ByteArray(nbBytes)
// slice is required to left source buffer untouched
slice().get(defCpy)
val view = ByteBuffer.wrap(defCpy)
return (1..nbUUIDs).asSequence()
.map { UUID(view.long, view.long) }
}
fun List<UUID>?.write() : ByteBuffer? {
if (this == null || isEmpty()) return null;
val buffer = ByteBuffer.allocate(Math.multiplyExact(size, 16))
forEach { uuid ->
buffer.putLong(uuid.mostSignificantBits)
buffer.putLong(uuid.leastSignificantBits)
}
buffer.rewind()
return buffer
}
fun main() {
val uuids = (0..3).asSequence().map { UUID.randomUUID() }.toList()
val writtenUUIDs = uuids.write()
val uuidSequence = writtenUUIDs ?.readUUIDs() ?: throw RuntimeException("Bug")
// Note that now, we can do whatever we want with the buffer. The sequence is not impacted
writtenUUIDs.getLong()
writtenUUIDs.putLong(-1)
val readBack = uuidSequence?.toList()
assert(uuids == readBack) { throw RuntimeException("Bug") }
println(uuids)
println(readBack)
}
推荐阅读
- c - 循环遍历 C 结构中的元素以提取单个元素的值和数据类型
- java - 如何减少 libgdx 中 2D 精灵的高 RAM 使用率
- dc.js - dc.js 中的失败百分比
- python - 如何沿轴使用 argmax 来获取 numpy 二维数组每一列的最大值?
- c - linux设备驱动和insmod
- php - 管理社交登录验证数据
- c# - C# int.ParseInt() 性能问题
- file-io - 在 Kotlin 中使用 BiPredicate 的 Files.find()
- r - R 正则表达式:找到一个字母作为一个词,没有别的
- php - Codeigniter - JSON 格式的 Img 值