我一直在尝试从 Bytes 对象中检索 ByteBuffer (Direct) 对象引用。以下是代码:
TestMessageWire testMessageWire = new TestMessageWire();
testMessageWire.data('H');
testMessageWire.setIntData(100);
//Below does not give ByteBuffer with correct position & offset
Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer(10);
Wire wire = new RawWire(bytes);
testMessageWire.writeMarshallable(wire);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes.toByteArray());
//Another approach, but still does not populate "byteBuffer" object correctly.
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(6);
Bytes<?> bytes = Bytes.wrapForWrite(byteBuffer);
Wire wire = new RawWire(bytes);
testMessageWire.writeMarshallable(wire);
我想在创建 ByteBuffer 对象时避免多次分配。因此,想要重用由 Bytes 包装的相同 Bitebuffer。我怎样才能做到这一点?