Java Code Examples for org.apache.commons.lang3.ArrayUtils.toPrimitive()

The following are Jave code examples for showing how to use toPrimitive() of the org.apache.commons.lang3.ArrayUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: smaph   File: ZScoreFeatureNormalizer.java   View Source Code Vote up 6 votes
public static <E extends Serializable, G extends Serializable> ZScoreFeatureNormalizer fromGatherer(ExampleGatherer<E, G> exampleGatherer, boolean strict) {
	ZScoreFeatureNormalizer fn = new ZScoreFeatureNormalizer();
	fn.strict = strict;
	List<FeaturePack<E>> ftrPacks = exampleGatherer.getAllFeaturePacks();
	for (String ftrName : ftrPacks.get(0).getFeatureNames()) {
		Vector<Double> ftrValues = new Vector<>();
		for (FeaturePack<E> fp : ftrPacks)
			if (fp.featureIsSet(ftrName))
				ftrValues.add(fp.getFeature(ftrName));
		double[] ftrValArray = ArrayUtils.toPrimitive(ftrValues.toArray(new Double[] {}));

		double valMean = meanComputer.evaluate(ftrValArray);
		double stdDev = stdDevComputer.evaluate(ftrValArray, valMean);
		fn.avgs.put(ftrName, valMean);
		fn.stdDevs.put(ftrName, stdDev);
	}
	return fn;
}
 
Example 2
Project: URTorrent   File: BencodeReader.java   View Source Code Vote up 6 votes
/**
 * Reads a bencoded <code>Map</code> (dict in the specification) from the
 * <code>InputStream</code>. The <code>Map</code> may contain lists and maps
 * itself.
 *
 * @since 0.1.0
 * @exception IOException if an IO exception occurs when reading
 * @exception EOFException if the stream ended unexpectedly
 * @exception BencodeReadException if the value read is not a properly bencoded Map
 */
public Map<String, Object> readDict() throws IOException, BencodeReadException {
    int initial = forceRead();
    if (initial != 'd') {
        throw new BencodeReadException("Bencoded dict must start with 'd', not '%c'",
                                       initial);
    }
    LinkedHashMap<String, Object> hm = new LinkedHashMap<String, Object>();
    while (peek() != 'e') {
        String key =  new String(ArrayUtils.toPrimitive(readString()), StandardCharsets.UTF_8);
        Object val = read();
        if (val == null) {
            throw new EOFException();
        }
        hm.put(key, val);
    }
    forceRead(); // read 'e' that we peeked
    return hm;
}
 
Example 3
Project: otf2ttf   File: CubicToQuad.java   View Source Code Vote up 6 votes
public double[] solveInflections(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
		double y4) {
	double p = -(x4 * (y1 - 2 * y2 + y3)) + x3 * (2 * y1 - 3 * y2 + y4) + x1 * (y2 - 2 * y3 + y4)
			- x2 * (y1 - 3 * y3 + 2 * y4);
	double q = x4 * (y1 - y2) + 3 * x3 * (-y1 + y2) + x2 * (2 * y1 - 3 * y3 + y4) - x1 * (2 * y2 - 3 * y3 + y4);
	double r = x3 * (y1 - y2) + x1 * (y2 - y3) + x2 * (-y1 + y3);

	Double[] temp = Stream.of(ArrayUtils.toObject(quadSolve(p, q, r))).filter(t -> t > 1e-8 && t < (1 - 1e-8))
			.toArray(Double[]::new);
	Arrays.sort(temp, new Comparator<Double>() {
		@Override
		public int compare(Double o1, Double o2) {
			return (int) byNumber(o1, o2);
		}
	});
	return ArrayUtils.toPrimitive(temp);
}
 
Example 4
Project: ytk-learn   File: GBDTFeatureParams.java   View Source Code Vote up 6 votes
private int[] parseCol(String colConf, Set<Integer> existCols, Map<String, Integer> fName2IndexMap) {
    if (colConf.equalsIgnoreCase("default")) {
        CheckUtils.check(!existCols.contains(-1), "[GBDT] feature approximate config error! default has been set twice");
        existCols.add(-1);
        return new int[]{-1};
    }

    List<Integer> colList = new ArrayList<>(16);
    String[] allCols = colConf.split(COL_SPLIT);

    for (String colField : allCols) {
        colField = colField.trim();
        CheckUtils.check(fName2IndexMap.containsKey(colField), "[GBDT] feature approximate config error! feature(%s) does not exist", colField);
        int col = fName2IndexMap.get(colField);
        CheckUtils.check(!existCols.contains(col), "[GBDT] feature approximate config error!, feature(%s) has been set twice", colField);
        colList.add(col);
        existCols.add(col);
    }
    return ArrayUtils.toPrimitive(colList.toArray(new Integer[colList.size()]));
}
 
Example 5
Project: RuuviCollector   File: HCIData.java   View Source Code Vote up 5 votes
public byte[] dataBytes() {
    if (data == null) {
        return new byte[0];
    } else {
        return ArrayUtils.toPrimitive(data.toArray(new Byte[data.size()]));
    }
}
 
Example 6
Project: URTorrent   File: TrackerRequest.java   View Source Code Vote up 5 votes
public static Iterable<InetSocketAddress> getPeersFromResponseDict(Map<String, Object> responseDict) {
	
		return  () -> {
			return new Iterator<InetSocketAddress>() {
				byte[] responsePeers = ArrayUtils.toPrimitive((Byte[])responseDict.get("peers"));
				int index = 0;
				@Override
				public boolean hasNext() {
					return index < responsePeers.length;
				}
				@Override
				public InetSocketAddress next() {
					try {
						InetAddress addr = InetAddress.getByAddress(Arrays.copyOfRange(responsePeers, index, index+4));
						ByteBuffer portBuf = ByteBuffer.allocate(4);
						portBuf.put(2, responsePeers[index+4]);
						portBuf.put(3, responsePeers[index+5]);
						index += 6;
						return new InetSocketAddress(addr, portBuf.getInt());
					} catch (UnknownHostException e) {
						return null;
					}
				}
			};
		};
	
}
 
Example 7
Project: ethereum-bytecode-analyzer   File: EQTraceAnalyzerTest.java   View Source Code Vote up 5 votes
@Test
public void test_eq_trace_1() throws Exception {
    /*
        Creates a trace like:
                    EQ
                   /  \
                  /    \
               MUL     ADD
               /\       /\
              /  \     /  \
            0x5  0x2  0x3 CALLDATALOAD
     */
    BytecodeChunk chunk = createChunk(0,
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x5)),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x2)),
            new Opcode(Opcodes.MUL, null),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x0)),
            new Opcode(Opcodes.CALLDATALOAD, null),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x3)),
            new Opcode(Opcodes.ADD, null),
            new Opcode(Opcodes.EQ, null),
            new Opcode(Opcodes.STOP, null)
    );

    EVMState evmState = symExecute(new HashMap<Integer, BytecodeChunk>() {{
        put(0, chunk);
    }});
    EVMStack stack = evmState.getStack();
    TraceableWord word = stack.pop();
    TraceTree trace = word.getTrace();

    EQTraceAnalyzer eqTraceAnalyzer = new EQTraceAnalyzer();
    EVMEnvironment environmentForTrace = eqTraceAnalyzer.createEnvironmentForTrace(trace, createDefaultEnvironment());
    byte[] bytes = ArrayUtils.toPrimitive((Byte[]) environmentForTrace.getCallData().toArray());
    BigInteger callDataLoad = new BigInteger(bytes);
    Assert.assertEquals(BigInteger.valueOf(0x7), callDataLoad);
}
 
Example 8
Project: ethereum-bytecode-analyzer   File: EQTraceAnalyzerTest.java   View Source Code Vote up 5 votes
@Test
public void test_eq_trace_2() throws Exception {

    BytecodeChunk chunk = createChunk(0,
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x2)),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0xA)),
            new Opcode(Opcodes.DIV, null),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(1)),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(2)),
            new Opcode(Opcodes.SUB, null),
            new Opcode(Opcodes.ADD, null),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x2)),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x0)),
            new Opcode(Opcodes.CALLDATALOAD, null),
            new Opcode(Opcodes.DIV, null),
            new Opcode(Opcodes.MUL, null),
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x6)),
            new Opcode(Opcodes.EQ, null),
            new Opcode(Opcodes.STOP, null)
    );

    EVMState evmState = symExecute(new HashMap<Integer, BytecodeChunk>() {{
        put(0, chunk);
    }});
    EVMStack stack = evmState.getStack();
    TraceableWord word = stack.pop();
    TraceTree trace = word.getTrace();

    EQTraceAnalyzer eqTraceAnalyzer = new EQTraceAnalyzer();
    EVMEnvironment environmentForTrace = eqTraceAnalyzer.createEnvironmentForTrace(trace, createDefaultEnvironment());
    byte[] bytes = ArrayUtils.toPrimitive((Byte[]) environmentForTrace.getCallData().toArray());
    BigInteger callDataLoad = new BigInteger(bytes);
    Assert.assertEquals(BigInteger.valueOf(0x2), callDataLoad);
}
 
Example 9
Project: ethereum-bytecode-analyzer   File: EQTraceAnalyzerTest.java   View Source Code Vote up 5 votes
@Test
public void test_eq_trace_function_calls() throws Exception {

    BytecodeChunk chunk = createChunk(0,
            new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x0)),
            new Opcode(Opcodes.CALLDATALOAD, null),
            new Opcode(Opcodes.PUSH29, new BigInteger("100000000000000000000000000000000000000000000000000000000", 16)),
            new Opcode(Opcodes.SWAP1, null),
            new Opcode(Opcodes.DIV, null),
            new Opcode(Opcodes.PUSH4, BigInteger.valueOf(0xffffffff)),
            new Opcode(Opcodes.AND, null),
            new Opcode(Opcodes.PUSH4, BigInteger.valueOf(0x3f7a0270)),
            new Opcode(Opcodes.EQ, null),
            new Opcode(Opcodes.STOP, null)
    );

    EVMState evmState = symExecute(new HashMap<Integer, BytecodeChunk>() {{
        put(0, chunk);
    }});
    EVMStack stack = evmState.getStack();
    TraceableWord word = stack.pop();
    TraceTree trace = word.getTrace();

    EQTraceAnalyzer eqTraceAnalyzer = new EQTraceAnalyzer();
    EVMEnvironment environmentForTrace = eqTraceAnalyzer.createEnvironmentForTrace(trace, createDefaultEnvironment());
    byte[] bytes = ArrayUtils.toPrimitive((Byte[]) environmentForTrace.getCallData().toArray());
    BigInteger callDataLoad = new BigInteger(bytes);
    Assert.assertEquals(new BigInteger("3f7a027000000000000000000000000000000000000000000000000000000000", 16), callDataLoad);
}
 
Example 10
Project: wulkanowy   File: Scrambler.java   View Source Code Vote up 5 votes
protected String decryptString(String alias, String text) throws CryptoException {

        if (!alias.isEmpty() && !text.isEmpty()) {
            try {
                KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);

                Cipher output = Cipher.getInstance("RSA/ECB/PKCS1Padding");
                output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());

                CipherInputStream cipherInputStream = new CipherInputStream(
                        new ByteArrayInputStream(Base64.decode(text, Base64.DEFAULT)), output);

                ArrayList<Byte> values = new ArrayList<>();

                int nextByte;

                while ((nextByte = cipherInputStream.read()) != -1) {
                    values.add((byte) nextByte);
                }

                Byte[] bytes = values.toArray(new Byte[values.size()]);

                return new String(ArrayUtils.toPrimitive(bytes), 0, bytes.length, "UTF-8");

            } catch (Exception e) {
                Log.e(DEBUG_TAG, e.getMessage());
                throw new CryptoException(e.getMessage());
            }
        } else {
            Log.e(DEBUG_TAG, "EncryptString - String is empty");
            throw new CryptoException("EncryptString - String is empty");

        }
    }
 
Example 11
Project: hippo   File: RandomHelper.java   View Source Code Vote up 5 votes
/**
 * @return An array of 256 bytes emulating binary content rather being a random text. The array contains
 *         all values possible for {@code byte} type in random order.
 */
public static byte[] newRandomByteArray() {

    // @formatter:off
    final List<Byte> byteList = IntStream.rangeClosed(Byte.MIN_VALUE, Byte.MAX_VALUE)
        .boxed()
        .map(Integer::byteValue)
        .collect(toList());
    // @formatter:on

    shuffle(byteList);

    return ArrayUtils.toPrimitive(byteList.toArray(new Byte[0]));
}
 
Example 12
Project: otf2ttf   File: CubicToQuad.java   View Source Code Vote up 5 votes
public double[] toFlatArray(Point[][] quadsList) {
	List<Double> result = new ArrayList<>();
	result.add(quadsList[0][0].x);
	result.add(quadsList[0][0].y);
	for (int i = 0; i < quadsList.length; i++) {
		result.add(quadsList[i][1].x);
		result.add(quadsList[i][1].y);
		result.add(quadsList[i][2].x);
		result.add(quadsList[i][2].y);
	}
	return ArrayUtils.toPrimitive(result.toArray(new Double[result.size()]));
}
 
Example 13
Project: ethereum-bytecode-analyzer   File: EVMEnvironment.java   View Source Code Vote up 4 votes
public String getCallDataHex() {
    byte[] bytes = ArrayUtils.toPrimitive(callData.toArray(new Byte[callData.size()]));
    return Hex.encodeHexString(bytes);
}