transferring bytes from one ByteBuffer to another

前端 未结 3 1010
无人及你
无人及你 2021-01-02 23:41

What\'s the most efficient way to put as many bytes as possible from a ByteBuffer bbuf_src into another ByteBuffer bbuf_dest (as well as know how m

3条回答
  •  孤街浪徒
    2021-01-03 00:39

    You get the BufferOverflowException because your bbuf_dest is not big enough.

    You will need to use bbuf_dest.remaining() to find out the maximum number of bytes you can transfer from bbuf_src:

    int maxTransfer = Math.min(bbuf_dest.remaining(), bbuf_src.remaining());
    bbuf_dest.put(bbuf_src.array(), 0, maxTransfer);
    

提交回复
热议问题