Buffer to Array - Java java.nio

Java examples for java.nio:ByteBuffer Array

Description

Buffer to Array

Demo Code


//package com.java2s;
import java.nio.*;

public class Main {
    public static byte[] toArray(final ByteBuffer buffer) {
        byte[] array = new byte[buffer.limit()];
        buffer.get(array);//w  w  w . ja v  a  2 s  .  c  o m
        return array;
    }

    public static char[] toArray(final CharBuffer buffer) {
        char[] array = new char[buffer.limit()];
        buffer.get(array);
        return array;
    }

    public static double[] toArray(final DoubleBuffer buffer) {
        double[] array = new double[buffer.limit()];
        buffer.get(array);
        return array;
    }

    public static float[] toArray(final FloatBuffer buffer) {
        float[] array = new float[buffer.limit()];
        buffer.get(array);
        return array;
    }

    public static int[] toArray(final IntBuffer buffer) {
        int[] array = new int[buffer.limit()];
        buffer.get(array);
        return array;
    }

    public static long[] toArray(final LongBuffer buffer) {
        long[] array = new long[buffer.limit()];
        buffer.get(array);
        return array;
    }

    public static short[] toArray(final ShortBuffer buffer) {
        short[] array = new short[buffer.limit()];
        buffer.get(array);
        return array;
    }
}

Related Tutorials