我尝试了一个非常简单的示例,我尝试编写 int -> ByteBuffer 并在从映射中读取值时重用数组。
try (ChronicleMap<Integer, ByteBuffer> map = ChronicleMap
.of(Integer.class, ByteBuffer.class)
.entries(1)
.averageValueSize(30)
.createPersistedTo(new File("e:/temp/test.obj"))) {
byte[] out = new byte[] { 1, 2, 3};
map.put(1, ByteBuffer.wrap(out)); // ClassCastException
ByteBuffer buff = ByteBuffer.allocate(30);
ByteBuffer in = map.getUsing(1, buff); // hoping that 'ByteBuffer in'
// is redundant
}
不幸的是,我得到了一个意想不到的结果ClassCastException
,因为 Chronicle 期望byte[]
Exception in thread "main" java.lang.ClassCastException: ChronicleMap{name=null, file=E:\temp\test.obj, identityHashCode=809762318}: Value must be a [B but was a class java.nio.HeapByteBuffer
at net.openhft.chronicle.map.VanillaChronicleMap.checkValue(VanillaChronicleMap.java:190)
at net.openhft.chronicle.map.VanillaChronicleMap.put(VanillaChronicleMap.java:707)
at test.SerTest.main(SerTest.java:28)
我究竟做错了什么?尝试byte[]
工作,但getUsing()
没有使用我的输入数组,它仍然是空的。