I have a Chronicle Queue where I have the following class:
public class Message extends SelfDescribingMarshallable {
private final Bytes<ByteBuffer> text;
private long timeStamp;
public Message(int bufferSize, long timeStamp) {
this.text = Bytes.elasticHeapByteBuffer(bufferSize);
this.timeStamp = timeStamp;
}
public Bytes<ByteBuffer> getText() {
return text;
}
public void setText(CharSequence text) {
this.text.clear().append(text);
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}
I use this class to read and write from queue. I need to integrate with internal library that receives this text in the following formats: ByteBuffer and byte[]. I need to send the text with the correct size and without memory alocation, in oder words i need to share the text instance because I apply this library on a Command Chain Pattern and for my poc low latency time is a big deal.
For extract the byte[] without memory allocation I am using the following strategy:
byte[] textByte = new byte[2048];
ByteBuffer textBuffer = message.getText().underlyingObject();
textBuffer.limit((int) message.getText().readLimit());
textBuffer.position(0);
textBuffer.get(textByte, 0, (int) message.getText().readLimit());
Retrieving the bytebuffer without allocation is a possibility too, but I don't now the best way to do it.
question from:
https://stackoverflow.com/questions/65906760/using-bytes-for-wrapping-bytebuffer 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…