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.offbynull.voip.audio.gateways.io.OutputData.java

public OutputData(byte[] data) {
    Validate.notNull(data);
    this.data = Arrays.copyOf(data, data.length);
}

From source file:com.smartitengineering.cms.api.impl.Utils.java

public static String readStringInUTF8(DataInput in) throws IOException, UnsupportedEncodingException {
    int allocationBlockSize = 2000;
    int capacity = allocationBlockSize;
    int length = 0;
    ByteBuffer buffer = ByteBuffer.allocate(allocationBlockSize);
    boolean notEof = true;
    while (notEof) {
        try {/*w w w  . j a va  2  s .c o m*/
            buffer.put(in.readByte());
            if (++length >= capacity) {
                capacity += allocationBlockSize;
                buffer.limit(capacity);
            }
        } catch (EOFException ex) {
            notEof = false;
        }
    }
    String string = StringUtils.newStringUtf8(Arrays.copyOf(buffer.array(), length));
    return string;
}

From source file:controlpac.EncryptHelper.java

public static String Desencriptar(String textoEncriptado) {
    String base64EncryptedString = "";
    try {//from w  w  w.  ja  v  a  2  s  .co m
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));//Desencodea el texto en base64
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); //Crea un hash con la clave elegida
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");//Crea la clave 
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key); //Inicia el descifrado
        byte[] plainText = decipher.doFinal(message);//Descifra el texto
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:lib.clases_cripto.java

public static String Desencriptar(String textoEncriptado) throws Exception {

    String secretKey = "qualityinfosolutions"; //llave para desenciptar datos
    String base64EncryptedString = "";

    try {//  ww w .ja v  a  2  s.  co m
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:com.bytecode.util.Crypto.java

private static byte[] encrypt(String keystring, String message, int bits) throws Exception {
    byte[] encValue = null;
    SecureRandom random = new SecureRandom();
    byte[] nonceBytes = new byte[8];
    random.nextBytes(nonceBytes);/*from  w ww .ja va2s.co  m*/
    IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16));

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key, nonce);
    byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8"));
    encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length);
    for (int i = 0; i < ciphertextWithoutNonce.length; i++) {
        encValue[i + 8] = ciphertextWithoutNonce[i];
    }

    return encValue;
}

From source file:com.offbynull.peernetic.playground.chorddht.messages.external.NotifyRequest.java

public NotifyRequest(byte[] id) {
    this.id = Arrays.copyOf(id, id.length);
    validate();
}

From source file:com.offbynull.peernetic.playground.chorddht.messages.external.GetIdResponse.java

public GetIdResponse(byte[] id) {
    this.id = Arrays.copyOf(id, id.length);
    validate();
}

From source file:com.offbynull.voip.audio.gateways.io.InputData.java

public InputData(byte[] data, int len) {
    Validate.notNull(data);// w  w  w .  j  av  a  2 s. c  o m
    Validate.isTrue(len >= 0);
    Validate.isTrue(len <= data.length);
    this.data = Arrays.copyOf(data, len);
}

From source file:com.opengamma.analytics.math.statistics.descriptive.ModeCalculator.java

/**
 * @param x The array of data, not null or empty
 * @return The arithmetic mean/*from  w  ww  . j ava2 s .c  om*/
 */
@Override
public Double evaluate(final double[] x) {
    Validate.notNull(x, "x");
    Validate.isTrue(x.length > 0, "x cannot be empty");
    if (x.length == 1) {
        return x[0];
    }
    final double[] x1 = Arrays.copyOf(x, x.length);
    Arrays.sort(x1);
    final TreeMap<Integer, Double> counts = new TreeMap<Integer, Double>();
    int count = 1;
    for (int i = 1; i < x1.length; i++) {
        if (Math.abs(x1[i] - x1[i - 1]) < EPS) {
            count++;
        } else {
            counts.put(count, x1[i - 1]);
            count = 1;
        }
    }
    if (counts.lastKey() == 1) {
        throw new MathException("Could not find mode for array; no repeated values");
    }
    return counts.lastEntry().getValue();
}

From source file:com.offbynull.peernetic.playground.chorddht.messages.external.UpdateFingerTableRequest.java

public UpdateFingerTableRequest(byte[] id) {
    this.id = Arrays.copyOf(id, id.length);
    validate();
}