Gets byte array from a ByteBuffer in java

前端 未结 6 712
庸人自扰
庸人自扰 2020-11-30 01:31

Is this the recommended way to get the bytes from the ByteBuffer

ByteBuffer bb =..

byte[] b = new byte[bb.remaining()]
bb.get(b, 0, b.length);
6条回答
  •  一整个雨季
    2020-11-30 02:13

    Depends what you want to do.

    If what you want is to retrieve the bytes that are remaining (between position and limit), then what you have will work. You could also just do:

    ByteBuffer bb =..
    
    byte[] b = new byte[bb.remaining()];
    bb.get(b);
    

    which is equivalent as per the ByteBuffer javadocs.

提交回复
热议问题