Android Byte Array Randomize randomize(byte[] array)

Here you can find the source of randomize(byte[] array)

Description

Fills an array with random values.

Declaration

public static void randomize(byte[] array) 

Method Source Code

//package com.java2s;
import java.util.Random;

public class Main {
    /**/*from  w w w  .j  av a2  s . c  o  m*/
     * Fills an array with random values.
     * \param array The byte array to be filled. The entire array will be
     * filled with random values.
     **/
    public static void randomize(byte[] array) {
        Random rnd = new Random(System.currentTimeMillis());
        int len = array.length;
        int count = (len / 8);
        int rest = (len - (count * 8));
        int index = 0;

        /* Fill in the array with random 64 bits values. */
        for (index = 0; index < count; index += 8) {
            longAsLittleEnd(array, index, rnd.nextLong());
        }

        /* Fill the rest of the array. */
        if (rest >= 4) {
            intAsLittleEnd(array, index, rnd.nextInt());
            index += 4;
            rest -= 4;
        }

        if (rest >= 2) {
            shortAsLittleEnd(array, index,
                    (short) (rnd.nextInt() & 0x0000FFFF));
            index += 2;
            rest -= 2;
        }

        if (rest >= 1) {
            array[index] = (byte) (rnd.nextInt() & 0x000000FF);
        }
    }

    /**
     * Converts a long value to an array in little-endian format.
     * \param val The value to be converted.
     * \returns A byte array with the value in little-endian format.
     **/
    public static byte[] longAsLittleEnd(long val) {
        byte[] array = new byte[8];
        return longAsLittleEnd(array, 0, val);
    }

    /**
     * Convert a long value into an array in little-endian format, copying the
     * result in the passed array at the point especified.
     * \param a The array where the converted value should be copied.
     * \param i The starting point, int the array \a a where the copy should
     * start.
     * \param v The value to be converted and copied.
     * \returns \a a.
     **/
    public static byte[] longAsLittleEnd(byte[] a, int i, long v) {
        a[i + 7] = (byte) (0xFF & (v >> 56));
        a[i + 6] = (byte) (0xFF & (v >> 48));
        a[i + 5] = (byte) (0xFF & (v >> 40));
        a[i + 4] = (byte) (0xFF & (v >> 32));
        a[i + 3] = (byte) (0xFF & (v >> 24));
        a[i + 2] = (byte) (0xFF & (v >> 16));
        a[i + 1] = (byte) (0xFF & (v >> 8));
        a[i + 0] = (byte) (0xFF & v);
        return a;
    }

    /**
     * Converts an int value to an array in little-endian format.
     * \param val The value to be converted.
     * \returns A byte array with the value in little-endian format.
     **/
    public static byte[] intAsLittleEnd(int val) {
        byte[] array = new byte[4];
        return intAsLittleEnd(array, 0, val);
    }

    /**
     * Convert an int value into an array in little-endian format, copying the
     * result in the passed array at the point especified.
     * \param a The array where the converted value should be copied.
     * \param i The starting point, int the array \a a where the copy should
     * start.
     * \param v The value to be converted and copied.
     * \returns \a a.
     **/
    public static byte[] intAsLittleEnd(byte[] a, int i, int v) {
        a[i + 3] = (byte) (0xFF & (v >> 24));
        a[i + 2] = (byte) (0xFF & (v >> 16));
        a[i + 1] = (byte) (0xFF & (v >> 8));
        a[i + 0] = (byte) (0xFF & v);
        return a;
    }

    /**
     * Converts a short value to an array in little-endian format.
     * \param val The value to be converted.
     * \returns A byte array with the value in little-endian format.
     **/
    public static byte[] shortAsLittleEnd(short val) {
        byte[] array = new byte[2];
        return shortAsLittleEnd(array, 0, val);
    }

    /**
     * Convert a short value into an array in little-endian format, copying the
     * result in the passed array at the point especified.
     * \param a The array where the converted value should be copied.
     * \param i The starting point, int the array \a a where the copy should
     * start.
     * \param v The value to be converted and copied.
     * \returns \a a.
     **/
    public static byte[] shortAsLittleEnd(byte[] a, int i, short v) {
        a[i + 1] = (byte) (0xFF & (v >> 8));
        a[i + 0] = (byte) (0xFF & v);
        return a;
    }
}