Converting Between a ByteBuffer an a Byte Array - Java File Path IO

Java examples for File Path IO:ByteBuffer

Description

Converting Between a ByteBuffer an a Byte Array

Demo Code

import java.nio.ByteBuffer;

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);//from  ww w . j  av  a  2 s  .  c o m

    // Retrieve all bytes in the buffer
    buf.clear();
    bytes = new byte[buf.capacity()];
    buf.get(bytes, 0, bytes.length);
  }
}

Related Tutorials