FileDocCategorySizeDatePackage
ByteArrayHelpers.javaAPI DocAndroid 5.1 API9147Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.unit

ByteArrayHelpers

public class ByteArrayHelpers extends Object

Fields Summary
private static final String
TAG
private static boolean
VERBOSE
Constructors Summary
private ByteArrayHelpers()

        throw new AssertionError();
    
Methods Summary
public static byte[]toByteArray(byte[] array)
Convert an array of byte primitives to a {@code byte[]} using native endian order.

This function is a pass-through; it's here only to provide overloads for every single type of primitive arrays.

param
array array of primitives
return
array


                                                
         
        return array;
    
private static byte[]toByteArray(T array, int sizeOfTBits)

        @SuppressWarnings("unchecked")
        Class<T> klass = (Class<T>) array.getClass();

        if (!klass.isArray()) {
            throw new IllegalArgumentException("array class must be an array");
        }

        int sizeOfT = sizeOfTBits / Byte.SIZE;
        int byteLength = Array.getLength(array) * sizeOfT;

        if (klass == byte[].class) {
            // Always return a copy
            return Arrays.copyOf((byte[])array, byteLength);
        }

        ByteBuffer byteBuffer = ByteBuffer.allocate(byteLength).order(ByteOrder.nativeOrder());

        if (klass == int[].class) {
            byteBuffer.asIntBuffer().put((int[])array);
        } else if (klass == float[].class) {
            byteBuffer.asFloatBuffer().put((float[])array);
        } else if (klass == double[].class) {
            byteBuffer.asDoubleBuffer().put((double[])array);
        } else if (klass == short[].class) {
            byteBuffer.asShortBuffer().put((short[])array);
        } else if (klass == char[].class) {
            byteBuffer.asCharBuffer().put((char[])array);
        } else if (klass == long[].class) {
            byteBuffer.asLongBuffer().put((long[])array);
        } else {
            throw new IllegalArgumentException("array class invalid; must be a primitive array");
        }

        return byteBuffer.array();
    
public static byte[]toByteArray(short[] array)
Convert an array of shorts to a {@code byte[]} using native endian order.

param
array array of shorts
return
array converted into byte array using native endian order

        return toByteArray(array, Short.SIZE);
    
public static byte[]toByteArray(char[] array)
Convert an array of chars to a {@code byte[]} using native endian order.

param
array array of chars
return
array converted into byte array using native endian order

        return toByteArray(array, Character.SIZE);
    
public static byte[]toByteArray(int[] array)
Convert an array of ints to a {@code byte[]} using native endian order.

param
array array of ints
return
array converted into byte array using native endian order

        return toByteArray(array, Integer.SIZE);
    
public static byte[]toByteArray(long[] array)
Convert an array of longs to a {@code byte[]} using native endian order.

param
array array of longs
return
array converted into byte array using native endian order

        return toByteArray(array, Long.SIZE);
    
public static byte[]toByteArray(float[] array)
Convert an array of floats to a {@code byte[]} using native endian order.

param
array array of floats
return
array converted into byte array using native endian order

        return toByteArray(array, Float.SIZE);
    
public static byte[]toByteArray(double[] array)
Convert an array of doubles to a {@code byte[]} using native endian order.

param
array array of doubles
return
array converted into byte array using native endian order

        return toByteArray(array, Double.SIZE);
    
public static byte[]toByteArray(T array)
Convert an array of primitives to a {@code byte[]} using native endian order.

Arguments other than arrays are not supported. The array component must be primitive, the wrapper class is not allowed (e.g. {@code int[]} is ok, but {@code Integer[]} is not.

param
array array of primitives
return
array converted into byte array using native endian order
throws
IllegalArgumentException if {@code array} was not an array of primitives

        @SuppressWarnings("unchecked")
        Class<T> klass = (Class<T>) array.getClass();

        if (!klass.isArray()) {
            throw new IllegalArgumentException("array class must be an array");
        }

        Class<?> componentClass = klass.getComponentType();

        if (!componentClass.isPrimitive()) {
            throw new IllegalArgumentException("array's component must be a primitive");
        }

        int sizeInBits;
        if (klass == int.class) {
            sizeInBits = Integer.SIZE;
        } else if (klass == float.class) {
            sizeInBits = Float.SIZE;
        } else if (klass == double.class) {
            sizeInBits = Double.SIZE;
        } else if (klass == short.class) {
            sizeInBits = Short.SIZE;
        } else if (klass == char.class) {
            sizeInBits = Character.SIZE;
        } else if (klass == long.class) {
            sizeInBits = Long.SIZE;
        } else if (klass == byte.class) {
            sizeInBits = Byte.SIZE;
        } else {
            throw new AssertionError();
        }

        return toByteArray(array, sizeInBits);
    
public static byte[]toByteArray(java.lang.Number numbers)
Convert a variadic list of {@code Number}s into a byte array using native endian order.

Each {@link Number} must be an instance of a primitive wrapper class (e.g. {@link Integer} is OK, since it wraps {@code int}, but {@code BigInteger} is not.

param
numbers variadic list of numeric values
return
array converted into byte array using native endian order
throws
IllegalArgumentException if {@code numbers} contained a class that wasn't a primitive wrapper

        if (numbers.length == 0) {
            throw new IllegalArgumentException("too few numbers");
        }

        if (VERBOSE) Log.v(TAG, "toByteArray - input: " + Arrays.toString(numbers));

        // Have a large enough capacity to fit in every number as a double
        ByteBuffer byteBuffer = ByteBuffer.allocate(numbers.length * (Double.SIZE / Byte.SIZE))
                .order(ByteOrder.nativeOrder());

        for (int i = 0; i < numbers.length; ++i) {
            Number value = numbers[i];
            Class<? extends Number> klass = value.getClass();

            if (VERBOSE) Log.v(TAG, "toByteArray - number " + i + ", class " + klass);

            if (klass == Integer.class) {
                byteBuffer.putInt((Integer)value);
            } else if (klass == Float.class) {
                byteBuffer.putFloat((Float)value);
            } else if (klass == Double.class) {
                byteBuffer.putDouble((Double)value);
            } else if (klass == Short.class) {
                byteBuffer.putShort((Short)value);
            } else if (klass == Long.class) {
                byteBuffer.putLong((Long)value);
            } else if (klass == Byte.class) {
                byteBuffer.put((Byte)value);
            } else {
                throw new IllegalArgumentException(
                        "number class invalid; must be wrapper around primitive class");
            }
        }

        if (VERBOSE) Log.v(TAG, "toByteArray - end of loop");

        // Each number written is at least 1 byte, so the position should be at least length
        if (numbers.length != 0 && byteBuffer.position() < numbers.length) {
            throw new AssertionError(String.format(
                    "Had %d numbers, but byte buffer position was only %d",
                    numbers.length, byteBuffer.position()));
        }

        byteBuffer.flip();

        byte[] bytes = new byte[byteBuffer.limit()];
        byteBuffer.get(bytes);

        if (VERBOSE) Log.v(TAG, "toByteArray - output: " + Arrays.toString(bytes));

        return bytes;