Code example for CharBuffer

Methods: position, put

0
    } 
 
    public void toByteBuffer(ByteBuffer b) {
        //symbol 
        CharBuffer cb = b.asCharBuffer();
        cb.put(ticker);//we know ticker is a fixed length string
        b.position(b.position() + cb.position() * 2);
        //price, volume 
        b.putDouble(price);
        b.putInt(volume);
        // current time ms for end to end latency 
        b.putLong(System.currentTimeMillis());
    } 
 
    public static MarketData fromByteBuffer(ByteBuffer byteBuffer) {
        MarketData md = new MarketData();
        //symbol 
        char[] ticker = new char[Symbols.LENGTH];
        CharBuffer cb = byteBuffer.asCharBuffer();
        cb.get(ticker);
        md.setTicker(String.valueOf(ticker));