Java ByteBuffer store byte array

Description

Java ByteBuffer store byte array


import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
  public static void main(String[] args) throws Exception {
    ByteBuffer bb = arr2ByteBuffer("demo2s.com".getBytes());
    System.out.println(bb);/* w ww  .j  a  v  a2  s . c om*/
  }

  public static ByteBuffer arr2ByteBuffer(byte[] arr) {

    ByteBuffer ibb = ByteBuffer.allocateDirect(arr.length);
    ibb.order(ByteOrder.nativeOrder());
    ibb.put(arr);

    ibb.position(0);
    return ibb;
  }
}
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
  public static void main(String[] args) throws Exception {
    ByteBuffer bb = ConvToByteBuffer("demo2s.com".getBytes());
    System.out.println(bb);/*  w ww . j  a  v a 2s  . c  o m*/
  }
  public static ByteBuffer ConvToByteBuffer(byte buf[]) {
    ByteBuffer ReturnBuffer = ByteBuffer.allocateDirect(buf.length);

    ReturnBuffer.order(ByteOrder.nativeOrder());
    ReturnBuffer.put(buf);
    ReturnBuffer.position(0);

    return ReturnBuffer;
  }
}



PreviousNext

Related