Example usage for java.util Arrays fill

List of usage examples for java.util Arrays fill

Introduction

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

Prototype

public static void fill(Object[] a, Object val) 

Source Link

Document

Assigns the specified Object reference to each element of the specified array of Objects.

Usage

From source file:Main.java

public static String padString(String s, char padChar, int length) {
    int slen, numPads = 0;
    if (s == null) {
        s = "";/*from  ww  w.  j  a v a  2  s  .  com*/
        numPads = length;
    } else if ((slen = s.length()) > length) {
        s = s.substring(0, length);
    } else if (slen < length) {
        numPads = length - slen;
    }
    if (numPads == 0) {
        return s;
    }
    char[] c = new char[numPads];
    Arrays.fill(c, padChar);
    return s + new String(c);
}

From source file:Main.java

public static String rightPadString(String s, char padChar, int length) {
    int slen, numPads = 0;
    if (s == null) {
        s = "";//w  ww. ja va 2 s. c  om
        numPads = length;
    } else if ((slen = s.length()) > length) {
        s = s.substring(length);
    } else if (slen < length) {
        numPads = length - slen;
    }
    if (numPads == 0) {
        return (s);
    }
    char[] c = new char[numPads];
    Arrays.fill(c, padChar);
    return new String(c) + s;
}

From source file:Main.java

public static Class[] params(Class cls1, Class cls2, Class clsFill, int times) {
    Class[] classes = new Class[times + 2];
    Arrays.fill(classes, clsFill);
    classes[0] = cls1;//from w w  w  .j a  v  a 2 s  .c  om
    classes[1] = cls2;
    return classes;
}

From source file:Main.java

public static void zeroize(char[] secret) {
    if (secret != null)
        Arrays.fill(secret, '\0');
}

From source file:Main.java

public static String repeatingChars(char toRepeat, int number) {
    char[] chars = new char[number];
    Arrays.fill(chars, toRepeat);
    return new String(chars);
}

From source file:Main.java

public static int[] columnChecksums(BufferedImage img) {

    int[] sums = new int[img.getWidth()];
    Arrays.fill(sums, 0);

    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            final int colour = img.getRGB(x, y);
            sums[x] += (int) ((colour & 0x00ff0000) >> 16);
            sums[x] += (int) ((colour & 0x0000ff00) >> 8);
            sums[x] += (int) (colour & 0x000000ff);
        }//from  www .j a  va2  s.c o  m
    }

    for (int x = 0; x < img.getWidth(); x++) {
        sums[x] %= 256;
    }

    return sums;
}

From source file:Main.java

public static byte[] padRight(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }/*from   w ww. j av a2 s  .  co  m*/
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);
    ByteBuffer buffer = ByteBuffer.allocate(len);

    buffer.put(tgt);
    buffer.put(paddings);

    return buffer.array();
}

From source file:Main.java

public static void writeCString(OutputStream out, String s, int fixedLen) throws IOException {
    byte[] bytes = s.getBytes();
    out.write(bytes.length);//from w w w .j a  v a 2 s.  com
    fixedLen -= 1;

    if (fixedLen <= 0)
        return;

    if (fixedLen <= bytes.length) {
        out.write(bytes, 0, fixedLen);
    } else {
        out.write(bytes);
        byte[] fillBytes = new byte[fixedLen - bytes.length];
        Arrays.fill(fillBytes, (byte) 0);
        out.write(fillBytes);
    }

    out.flush();
}

From source file:Main.java

private static String digest(final String value) {
    final byte[] digested;
    try {/*ww  w  .j  a v a  2s .  c  o  m*/
        digested = MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(CHARSET));
    } catch (final NoSuchAlgorithmException e) {
        return null;
    } catch (final UnsupportedEncodingException e) {
        return null;
    }

    final String hashed = new BigInteger(1, digested).toString(16);
    final int padding = HASH_LENGTH - hashed.length();
    if (padding == 0) {
        return hashed;
    }

    final char[] zeros = new char[padding];
    Arrays.fill(zeros, '0');
    return new StringBuilder(HASH_LENGTH).append(zeros).append(hashed).toString();
}

From source file:Main.java

public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException {
    if (keyString == null || keyString.length() == 0) {
        throw new NullPointerException("Please give Password");
    }/*from   w  w  w. ja v  a2  s  .c  o  m*/

    if (clearText == null || clearText.length <= 0) {
        throw new NullPointerException("Please give clearText");
    }

    try {
        SecretKeySpec skeySpec = getKey(keyString);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // Cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);

        return cipher.doFinal(clearText);

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}