Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

java - Using Bytes for Wrapping ByteBuffer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...