Java Convert via ByteBuffer toBuffer(byte[] arr)

Here you can find the source of toBuffer(byte[] arr)

Description

to Buffer

License

Open Source License

Declaration

public static ByteBuffer toBuffer(byte[] arr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Main {
    public static ByteBuffer toBuffer(byte[] arr) {
        ByteBuffer buff = createByteBuffer(arr.length);
        buff.put(arr);/* w  w  w.j a va  2  s  .c  o m*/
        buff.compact();
        return buff;
    }

    public static ShortBuffer toBuffer(short[] arr) {
        return createShortBuffer(arr.length).put(arr).compact();
    }

    public static IntBuffer toBuffer(int[] arr) {
        return createIntBuffer(arr.length).put(arr).compact();
    }

    public static LongBuffer toBuffer(long[] arr) {
        return createLongBuffer(arr.length).put(arr).compact();
    }

    public static FloatBuffer toBuffer(float[] arr) {
        return createFloatBuffer(arr.length).put(arr).compact();
    }

    public static DoubleBuffer toBuffer(double[] arr) {
        return createDoubleBuffer(arr.length).put(arr).compact();
    }

    public static ByteBuffer createByteBuffer(int size) {
        return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
    }

    public static ShortBuffer createShortBuffer(int size) {
        return createByteBuffer(size << 1).asShortBuffer();
    }

    public static IntBuffer createIntBuffer(int size) {
        return createByteBuffer(size << 2).asIntBuffer();
    }

    public static LongBuffer createLongBuffer(int size) {
        return createByteBuffer(size << 3).asLongBuffer();
    }

    public static FloatBuffer createFloatBuffer(int size) {
        return createByteBuffer(size << 2).asFloatBuffer();
    }

    public static DoubleBuffer createDoubleBuffer(int size) {
        return createByteBuffer(size << 3).asDoubleBuffer();
    }
}

Related

  1. toBigInteger(byte[] bytes)
  2. toBinaryFloatingPoint(double source)
  3. toBinaryString(byte[] b)
  4. toBuffer(boolean[] src, int offset)
  5. toBuffer(byte[] arr)
  6. toBuffer(int[] array)
  7. toBuffer(Serializable obj)
  8. toBuffer(String spacedHex)
  9. toByte(char data)