Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

In this page you can find the example usage for java.util Arrays copyOf.

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.Sort.java

/**
 * Sorts values statelessly in ascending order
 * @param v1 the values to sort (a native backed array)
 * @return tmp the sorted values//from  ww w .  j  a  va 2  s  . c o  m
 */
public static int[] stateless(int[] v1) {
    Validate.notNull(v1);
    int[] tmp = Arrays.copyOf(v1, v1.length);
    Arrays.sort(tmp);
    return tmp;
}

From source file:com.netflix.hystrix.contrib.javanica.utils.CommonUtils.java

public static Object[] createArgsForFallback(Object[] args, MetaHolder metaHolder, Throwable exception) {
    if (metaHolder.isExtendedFallback()) {
        if (metaHolder.isExtendedParentFallback()) {
            args[args.length - 1] = exception;
        } else {//from w  w  w .  j av a  2 s.c o  m
            args = Arrays.copyOf(args, args.length + 1);
            args[args.length - 1] = exception;
        }
    } else {
        if (metaHolder.isExtendedParentFallback()) {
            args = ArrayUtils.remove(args, args.length - 1);
        }
    }
    return args;
}

From source file:com.aast.encrypt.EncryptManager.java

private static byte[] getKeyFromString(String keyStr)
        throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] key = keyStr.getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);// w  w w.java 2 s. com
    key = Arrays.copyOf(key, 16); // use only first 128 bit
    return key;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static boolean[] arrayCopyOf(boolean[] array, int length) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Arrays.copyOf(array, length);
    } else {//from   w ww  .j  av a2 s .c  o m
        boolean[] newArray = new boolean[length];
        System.arraycopy(array, 0, newArray, 0, length);
        return newArray;
    }
}

From source file:Main.java

/**
 * Copy an array. This is a shortcut for
 * {@link Arrays#copyOf(Object[], int)} that does not require to specify the
 * length./*from   www . j  a v  a  2s  .co  m*/
 */
public static <T> T[] copyArray(T[] original) {
    return Arrays.copyOf(original, original.length);
}

From source file:Main.java

/**
 * Appends an element to the end of the array, growing the array if there is no more room.
 * @param array The array to which to append the element. This must NOT be null.
 * @param currentSize The number of elements in the array. Must be less than or equal to
 *                    array.length.// w ww. j  av a2s  .  c o  m
 * @param element The element to append.
 * @return the array to which the element was appended. This may be different than the given
 *         array.
 */
public static <T> T[] append(T[] array, int currentSize, T element) {
    assert currentSize <= array.length;

    if (currentSize + 1 > array.length) {
        @SuppressWarnings("unchecked")
        int newLengh = growSize(currentSize);
        T[] newArray = Arrays.copyOf(array, newLengh);

        /////////////////////////////
        // T[] newArray = ArrayUtils.newUnpaddedArray(
        //         (Class<T>) array.getClass().getComponentType(), growSize(currentSize));
        //             
        // System.arraycopy(array, 0, newArray, 0, currentSize);
        ///////////////////////////// 
        array = newArray;
    }
    array[currentSize] = element;
    return array;
}

From source file:Main.java

public static long[] growIfNecessary(long[] array, int minLength, int numAdditionalLongs) {
    if (array.length >= minLength) {
        return array;
    }/*from w w w  .j  a  v a 2 s  .c om*/
    return Arrays.copyOf(array, minLength + numAdditionalLongs);
}

From source file:clientapi.util.ClientAPIUtils.java

/**
 * Concatenates an array of generic arrays
 *
 * @param arrays The arrays being concatenated
 * @return The concatenated array//from w  w  w  . ja v  a2s.c  o  m
 */
@SafeVarargs
public static <T> T[] concat(T[]... arrays) {
    if (arrays.length < 2)
        throw new IllegalArgumentException("At least 2 arrays should be supplied");

    T[] result = arrays[0];
    for (int i = 1; i < arrays.length; i++) {
        T[] newArray = Arrays.copyOf(result, result.length + arrays[i].length);
        System.arraycopy(arrays[i], 0, newArray, result.length, arrays[i].length);
        result = newArray;
    }

    return result;
}

From source file:Main.java

/**
 * @param <T>//ww  w .  java2s . c  o m
 *            a type
 * @param elements
 *            the elements to choose from
 * @param indices
 *            the array of indices
 * @return the mapped array
 */
public static <T> T[] valuesAt(final T[] elements, final int[] indices) {
    final int n = indices.length;
    final T[] result = Arrays.copyOf(elements, n);
    for (int i = 0; i < n; ++i) {
        result[i] = elements[indices[i]];
    }
    return result;
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);/*from   w ww  . ja v  a  2  s  .  com*/

    return encryptedToken;
}