集册 Java实例教程 在ByteBuffer和Byte数组之间转换

在ByteBuffer和Byte数组之间转换

欢马劈雪     最近更新时间:2020-01-02 10:19:05

5207
在ByteBuffer和Byte数组之间转换

import java.nio.ByteBuffer;
/**来自 时代Java - nowjava.com**/

public class Main {

  public static void main(String[] args) {

    byte[] bytes = new byte[10];

    ByteBuffer buf = ByteBuffer.wrap(bytes);


    bytes = new byte[buf.remaining()];

    buf.get(bytes, 0, bytes.length);


    // Retrieve all bytes in the buffer

    buf.clear();

    bytes = new byte[buf.capacity()];

    buf.get(bytes, 0, bytes.length);

  }

}