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:it.unibo.alchemist.model.implementations.positions.ContinuousGenericEuclidean.java

@Override
public double[] getCartesianCoordinates() {
    return Arrays.copyOf(c, c.length);
}

From source file:net.navasoft.madcoin.backend.services.security.Encrypter.java

/**
 * Instantiates a new encrypter.//from  w w  w .jav  a 2s.  c  o m
 * 
 * @param keyString
 *            the key string
 * @param ivString
 *            the iv string
 * @since 5/08/2014, 08:03:33 PM
 */
public Encrypter(String keyString, String ivString) {
    try {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("utf-8")));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        keySpec = new DESedeKeySpec(keyBytes);

        key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        iv = new IvParameterSpec(ivString.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:algorithm.AesEncryption.java

private void setKey(String function) {

    MessageDigest sha = null;/*from w w  w  .j  av  a2  s .co  m*/
    try {
        //File f = new File("Aes_key");
        key = new byte[16];
        //   KeyGenerator kgen = KeyGenerator.getInstance("AES");

        //TODO : SET 128/196/256 keysize
        //generise kljuc od 128
        //         kgen.init(128);
        //NE KORISTI SE!!!
        //      secretKey = kgen.generateKey();
        //TODO : Set md5/SHA / SHA-1/ SHA-256
        //   sha = MessageDigest.getInstance("SHA-256");
        sha = MessageDigest.getInstance(function);
        key = sha.digest(key);

        // use only first 128 bit
        key = Arrays.copyOf(key, 16);

        secretKeySpec = new SecretKeySpec(key, "AES");

        //  new FileOutputStream(f).write(key);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:com.google.testing.coverage.BitField.java

/**
 * Sets or clears a bit at the given index.
 *
 * @param index bit index/* w  ww  .ja  va 2  s.com*/
 */
private void setBit(int index, boolean isSet) {
    int byteIndex = index / 8;
    int newByteSize = byteIndex + 1;
    if (bytes.length < newByteSize) {
        bytes = Arrays.copyOf(bytes, newByteSize);
    }

    int bitIndex = index % 8;
    int mask = 1 << bitIndex;

    if (isSet) {
        bytes[byteIndex] = (byte) (bytes[byteIndex] | mask);
    } else {
        bytes[byteIndex] = (byte) (bytes[byteIndex] & ~mask);
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.tamarisk.RestoreAction.java

@Override
public void doAction(final RandomGenerator rng, final TamariskState s) {
    assert (old_species_ == null);
    old_species_ = Arrays.copyOf(s.habitats[reach], s.params.Nhabitats);
    cost_ = s.params.restore_cost;//ww w . j a v a  2 s .  com
    for (int i = 0; i < s.params.Nhabitats; ++i) {
        if (old_species_[i] == Species.None) {
            cost_ += s.params.restore_cost_per_empty;
            final double r = s.rng.nextDouble();
            if (r < s.params.restore_rate) {
                s.habitats[reach][i] = Species.Native;
            }
        }
    }
}

From source file:com.opengamma.financial.analytics.LabelledMatrix1D.java

public LabelledMatrix1D(final S[] keys, final Object[] labels, final String labelsTitle, final double[] values,
        final String valuesTitle, final T defaultTolerance) {
    Validate.notNull(keys, "labels");
    Validate.notNull(labels, "label names");
    Validate.notNull(values, "values");
    final int n = keys.length;
    Validate.isTrue(n == labels.length, "length of keys array must match length of label names array");
    Validate.isTrue(n == values.length, "length of keys array must match length of values array");
    _keys = Arrays.copyOf(keys, n);
    _labels = Arrays.copyOf(labels, n);
    _labelsTitle = labelsTitle;/*from w  ww .j av  a  2  s . c  om*/
    _values = Arrays.copyOf(values, n);
    _valuesTitle = valuesTitle;
    _defaultTolerance = defaultTolerance;
    quickSort();
}

From source file:com.opengamma.analytics.math.interpolation.data.Interpolator1DPiecewisePoynomialWithExtraKnotsDataBundle.java

/**
 * Constructor where coefficients for interpolant and its node sensitivity are computed 
 * @param underlyingData Contains sorted data (x,y)
 * @param method {@link PiecewisePolynomialInterpolator}
 *///from   w ww. j  a  v a2 s  .c om
public Interpolator1DPiecewisePoynomialWithExtraKnotsDataBundle(final Interpolator1DDataBundle underlyingData,
        final PiecewisePolynomialInterpolator method) {
    ArgumentChecker.notNull(underlyingData, "underlying data");
    ArgumentChecker.notNull(method, "method");

    _eps = 1.e-7;
    _small = 1.e-14;

    _underlyingData = underlyingData;
    _poly = method.interpolate(underlyingData.getKeys(), underlyingData.getValues());

    final double[] yValues = underlyingData.getValues();
    final int nData = yValues.length;
    _polyUp = new PiecewisePolynomialResult[nData];
    _polyDw = new PiecewisePolynomialResult[nData];
    double[] yValuesUp = Arrays.copyOf(yValues, nData);
    double[] yValuesDw = Arrays.copyOf(yValues, nData);
    for (int i = 0; i < nData; ++i) {
        yValuesUp[i] = Math.abs(yValues[i]) < _small ? _eps : yValues[i] * (1. + _eps);
        yValuesDw[i] = Math.abs(yValues[i]) < _small ? -_eps : yValues[i] * (1. - _eps);
        _polyUp[i] = method.interpolate(underlyingData.getKeys(), yValuesUp);
        _polyDw[i] = method.interpolate(underlyingData.getKeys(), yValuesDw);
        yValuesUp[i] = yValues[i];
        yValuesDw[i] = yValues[i];
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.tamarisk.EradicateAction.java

@Override
public void doAction(final RandomGenerator rng, final TamariskState s) {
    assert (old_species_ == null);
    old_species_ = Arrays.copyOf(s.habitats[reach], s.params.Nhabitats);
    cost_ = s.params.eradicate_cost;//from  www  .  j a  va 2s . co m
    for (int i = 0; i < s.params.Nhabitats; ++i) {
        if (old_species_[i] == Species.Tamarisk) {
            cost_ += s.params.eradicate_cost_per_habitat;
            final double r = s.rng.nextDouble();
            if (r < s.params.eradicate_rate) {
                s.habitats[reach][i] = Species.None;
            }
        }
    }
}

From source file:au.org.ala.delta.intkey.directives.invocation.ContentsDirectiveInvocation.java

@Override
public boolean execute(IntkeyContext context) {
    try {//w  w w  . j ava 2s. c  om
        BufferedReader reader = new BufferedReader(new FileReader(file));

        // use LinkedHashMap to maintain insertion order of keys
        LinkedHashMap<String, String> contentsMap = new LinkedHashMap<String, String>();

        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains("*")) {
                String[] tokens = line.split("\\*");
                if (tokens.length != 2) {
                    context.getUI()
                            .displayErrorMessage(UIUtils.getResourceString("BadlyFormedContentsFile.error"));
                    return false;
                }

                contentsMap.put(tokens[0].trim(), tokens[1].trim());
            } else {
                String[] tokens = line.split(" ");
                if (tokens.length > 1) {
                    String description = StringUtils.join(Arrays.copyOf(tokens, tokens.length - 1), " ");
                    String fileName = tokens[tokens.length - 1];

                    // TODO hack here. Really should be building
                    // IntkeyDirectiveInvocation objects
                    // from both line formats and passing them to the
                    // contents directive, rather than
                    // getting the contents directive to do directive
                    // parsing.
                    String command = "FILE DISPLAY " + fileName.trim();
                    contentsMap.put(description.trim(), command);
                } else {

                }
            }
        }
        context.getUI().displayContents(contentsMap);
    } catch (IOException ex) {

    }

    return true;
}

From source file:edu.hm.cs.fs.scriptinat0r7.model.ScriptDocument.java

public byte[] getFile() {
    return Arrays.copyOf(file, file.length);
}