Reading Byte[] from Chronicle Queue

I'm writing byte[] to chronicle queue using the following code,

excerptAppender.writeBytes(b -> b.write(data));

How can I read the same byte[] back from the queue. I found something like this,

excerptTailer.readBytes(b-> b.read(bytes));

But in this case I need the length. Do I need to write the length separately and read the same for creating the byte[].?

Or is there a way that the length will be handled by the framework itself, so that we can just read like,

excerptTailer.readBytes();

I couldn't find much docs on this.

Got this sample from github,

    assertTrue(tailer.readBytes(b -> {
    long address = b.address(b.readPosition());
    Unsafe unsafe = UnsafeMemory.UNSAFE;
    int code = unsafe.getByte(address);
    address++;
    int num = unsafe.getInt(address);
    address += 4;
    long num2 = unsafe.getLong(address);
    address += 8;
    int length = unsafe.getByte(address);
    address++;
    byte[] bytes = new byte[length];
    unsafe.copyMemory(null, address, bytes, Unsafe.ARRAY_BYTE_BASE_OFFSET, bytes.length);
    String text = new String(bytes, StandardCharsets.UTF_8);
    assertEquals("Hello World", text);
    // do something with values
}));

Is this recommended for production.?

1 Answer

Apologies for replying on so old thread, thought someone could get benefit out of it.

You can define an approx one time buffer size (fairly large) and the framework provides the actual data length. Have a look at the below code which works for me

  private byte[] readData() {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary("./temp/").build();
    ExcerptTailer tailer = queue.createTailer("my-single-tailer");
    byte[] data = null;
    Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(1024 * 128);
    boolean read = tailer.readBytes(bytes);
    if (read) {
      byte[] readData = bytes.underlyingObject().array();
      int len = (int) bytes.readRemaining();
      bytes.clear();
      data = Arrays.copyOf(readData, len);
    }
    return data;
  }

Also sample write data code would be

private void writeData(byte[] data) {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary("./temp/").rollCycle(RollCycles.HOURLY).build();
    ExcerptAppender appender = queue.acquireAppender();

    Bytes<ByteBuffer> bytes = Bytes.elasticByteBuffer(1024 * 128);
    bytes.ensureCapacity(data.length);

    ByteBuffer byteBuffer = bytes.underlyingObject();
    byteBuffer.put(data);
    bytes.readPositionRemaining(0, byteBuffer.position());
    appender.writeBytes(bytes);
    byteBuffer.clear();
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest