【发布时间】:2017-05-04 09:52:53
【问题描述】:
我需要对Collection<Integer> 进行序列化和反序列化,以便将其存储在需要byte[] 的Redis 中。我找到了使用ByteBuffer 和IntBuffer 进行序列化的代码:
byte[] serializeIntegerCollection(Collection<Integer> collection) {
ByteBuffer byteBuffer = ByteBuffer.allocate(collection.size() * 4);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
collection.forEach(intBuffer::put);
return byteBuffer.array();
}
现在我尝试用于反序列化的代码:
Collection<Integer> deserializeIntegerCollection(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
return asList(intBuffer.array());
}
但是intBuffer.array() 抛出UnsupportedOperationException。它有什么问题以及如何处理?
【问题讨论】:
-
您考虑过查阅文档吗?
标签: java arrays serialization