Reading and writing to byteBuffer in Chronicle Queue

240 Views Asked by At

Given that chronicle queue provides b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit()); for writing from a ByteBuffer to Chronicle, what is used to read it?

I currently use the following in Java 8:

private final WriteBytesMarshallable marshallable = (bytes) -> bytes.writeLong(this.millis)
            .writeLong(this.nanos)
            .writeInt(this.remaining)
            .write(bytes.writePosition(), buffer, 0, this.remaining);

together with the following for writing.

this.APPENDER.writeBytes(marshallable);

However when reading, both of the following options does not read anything at all, and there is nothing read into bytebuffer. Option 1 would throw an IndexOutofBounds exception because the byte array has length of 0.

private final ReadBytesMarshallable marshallable = (b) -> {
        currentMillis = b.readLong();
        currentNano = b.readLong();
        remaining = b.readInt();
        byteBuffer.put(b.bytesForRead().toByteArray(), 0, remaining);  //option1
        //b.read(byteBuffer)  //option 2
    };

while (TAILER.readBytes(marshallable)) {
    //do something
}

I would previously copy all the bytes in the direct bytebuffer into an array buffer.get(byteArr, 0, remaining); before writing into chronicle queue and reading using option 1, but I think this might be inefficient due to needing to copy the bytes to a byte array. Is it possible to directly write from the Direct ByteBuffer to chronicle queue without an intermediate byte[]?

0

There are 0 best solutions below