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

The following are Jave code examples for showing how to use reverse() 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: bireme   File: DebeziumPipeLine.java   View Source Code Vote up 6 votes
@Override
protected String decodeToBit(String data, int precision) {
  switch (data) {
    case "true":
      return "1";
    case "false":
      return "0";
  }

  StringBuilder sb = new StringBuilder();
  String oneByte;
  String result;
  byte[] decoded = Base64.decodeBase64(data);

  ArrayUtils.reverse(decoded);

  for (byte b : decoded) {
    oneByte = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
    sb.append(oneByte);
  }
  result = sb.toString();

  return result.substring(result.length() - precision);
}
 
Example 2
Project: neo-java   File: AbstractBlockBase.java   View Source Code Vote up 6 votes
/**
 * return the hash, as calculated from the other parameters.
 *
 * @return the hash, as calculated from the other parameters.
 */
private UInt256 calculateHash() {
	final ByteArrayOutputStream bout = new ByteArrayOutputStream();
	NetworkUtil.write(bout, nextConsensus.toByteArray());
	NetworkUtil.write(bout, consensusData.toByteArray());
	NetworkUtil.write(bout, index.toByteArray());
	NetworkUtil.write(bout, timestamp.toByteArray());
	NetworkUtil.write(bout, merkleRoot.toByteArray());
	final byte[] prevHashBa = prevHash.toByteArray();
	ArrayUtils.reverse(prevHashBa);
	NetworkUtil.write(bout, prevHashBa);
	NetworkUtil.write(bout, version.toByteArray());
	final byte[] hashDataBa = bout.toByteArray();
	ArrayUtils.reverse(hashDataBa);
	final byte[] hashBa = SHA256HashUtil.getDoubleSHA256Hash(hashDataBa);
	return new UInt256(hashBa);
}
 
Example 3
Project: teiniker-lectures-configurationmanagement   File: ArrayExample.java   View Source Code Vote up 6 votes
public static void main(String[] args)
{
	String[] array = ArrayUtils.toArray("one", "two", "three");	
	System.out.println(ArrayUtils.toString(array));
	
	array = ArrayUtils.add(array, "four");
	System.out.println(ArrayUtils.toString(array));
	
	if(ArrayUtils.contains(array,"two"))
	{
		System.out.println("found \"two\" in the array.");
	}
	
	ArrayUtils.reverse(array);
	System.out.println(ArrayUtils.toString(array));
}
 
Example 4
Project: teiniker-lectures-configurationmanagement   File: ArrayExample.java   View Source Code Vote up 6 votes
public static void main(String[] args)
{
	String[] array = ArrayUtils.toArray("one", "two", "three");	
	System.out.println(ArrayUtils.toString(array));
	
	array = ArrayUtils.add(array, "four");
	System.out.println(ArrayUtils.toString(array));
	
	if(ArrayUtils.contains(array,"two"))
	{
		System.out.println("found \"two\" in the array.");
	}
	
	ArrayUtils.reverse(array);
	System.out.println(ArrayUtils.toString(array));
}
 
Example 5
Project: cyberduck   File: LaxHostnameDelegatingTrustManager.java   View Source Code Vote up 5 votes
@Override
public void setTarget(final String hostname) {
    final String simple;
    final String[] parts = StringUtils.split(hostname, '.');
    if(parts.length > 4) {
        ArrayUtils.reverse(parts);
        // Rewrite c.cyberduck.s3.amazonaws.com which does not match wildcard certificate *.s3.amazonaws.com
        simple = StringUtils.join(parts[3], ".", parts[2], ".", parts[1], ".", parts[0]);
        log.warn(String.format("Rewrite hostname target to %s", simple));
    }
    else {
        simple = hostname;
    }
    super.setTarget(simple);
}
 
Example 6
Project: otf2ttf   File: CTQ.java   View Source Code Vote up 5 votes
public Contour[] canonicalStart(Contour[] points) {
	int jm = 0;
	for (int j = 1; j < points.length; j++) {
		if (points[j].x < points[jm].x || (points[j].x == points[jm].x && points[j].y < points[jm].y)) {
			jm = j;
		}
	}
	Contour[] result = ArrayUtils.addAll(Arrays.copyOfRange(points, jm, points.length),
			Arrays.copyOfRange(points, 0, jm));
	ArrayUtils.reverse(result);
	return result;
}
 
Example 7
Project: neo-java   File: Message.java   View Source Code Vote up 5 votes
/**
 * return the message as a byte array.
 *
 * @return the message as a byte array.
 * @throws IOException
 *             if an error occurs.
 * @throws UnsupportedEncodingException
 *             if an error occurs.
 */
public byte[] toByteArray() throws IOException, UnsupportedEncodingException {
	final byte[] magicBa = NetworkUtil.getIntByteArray(magic);
	ArrayUtils.reverse(magicBa);
	final UInt32 magicObj = new UInt32(magicBa);
	final byte[] checksumFull = SHA256HashUtil.getDoubleSHA256Hash(payloadBa);
	final byte[] checksum = new byte[4];
	System.arraycopy(checksumFull, 0, checksum, 0, 4);
	final ByteArrayOutputStream bout = new ByteArrayOutputStream();
	bout.write(magicObj.getBytesCopy());
	NetworkUtil.writeString(bout, 12, command);

	if (LOG.isTraceEnabled()) {
		LOG.trace("createMessage magic+command {}", Hex.encodeHexString(bout.toByteArray()));
	}
	final byte[] lengthBa = NetworkUtil.getIntByteArray(payloadBa.length);
	ArrayUtils.reverse(lengthBa);
	if (LOG.isTraceEnabled()) {
		LOG.trace("createMessage lengthBa {}", Hex.encodeHexString(lengthBa));
	}
	bout.write(lengthBa);
	if (LOG.isTraceEnabled()) {
		LOG.trace("createMessage magic+command+length {}", Hex.encodeHexString(bout.toByteArray()));
		LOG.trace("createMessage checksum {}", Hex.encodeHexString(checksum));
	}
	bout.write(checksum);
	if (LOG.isTraceEnabled()) {
		LOG.trace("createMessage magic+command+length+checksum {}", Hex.encodeHexString(bout.toByteArray()));
	}
	bout.write(payloadBa);
	if (LOG.isTraceEnabled()) {
		LOG.trace("createMessage payloadBa {}", Hex.encodeHexString(payloadBa));
		LOG.trace("createMessage magic+command+length+checksum+payload {}",
				Hex.encodeHexString(bout.toByteArray()));
	}
	return bout.toByteArray();
}
 
Example 8
Project: neo-java   File: TransactionOutputFactory.java   View Source Code Vote up 5 votes
@Override
public TransactionOutput toObject(final ByteBuffer bb) {
	bb.getLong();
	final byte[] assetIdBa = ModelUtil.getVariableLengthByteArray(bb);
	final byte[] valueBa = ModelUtil.getVariableLengthByteArray(bb);
	ArrayUtils.reverse(valueBa);
	final byte[] scriptHashBa = ModelUtil.getVariableLengthByteArray(bb);

	final UInt256 assetId = new UInt256(ByteBuffer.wrap(assetIdBa));
	final Fixed8 value = new Fixed8(ByteBuffer.wrap(valueBa));
	final UInt160 scriptHash = new UInt160(scriptHashBa);
	final TransactionOutput transactionOutput = new TransactionOutput(assetId, value, scriptHash);
	return transactionOutput;
}
 
Example 9
Project: neo-java   File: TransactionOutputMapToObject.java   View Source Code Vote up 5 votes
@Override
public TransactionOutput toObject(final Map<String, Object> map) {
	final byte[] assetIdBa = getBytes(map, "asset_id");
	final byte[] valueBa = getBytes(map, "value");
	ArrayUtils.reverse(valueBa);
	final byte[] scriptHashBa = getBytes(map, "script_hash");

	final UInt256 assetId = new UInt256(ByteBuffer.wrap(assetIdBa));
	final Fixed8 value = new Fixed8(ByteBuffer.wrap(valueBa));
	final UInt160 scriptHash = new UInt160(scriptHashBa);
	final TransactionOutput transactionOutput = new TransactionOutput(assetId, value, scriptHash);
	return transactionOutput;
}
 
Example 10
Project: neo-java   File: Base58Util.java   View Source Code Vote up 5 votes
/**
 * decodes a Base58 byte array.
 *
 * @param input
 *            the base58 string to use.
 * @return the decoded byte array.
 */
public static byte[] decode(final String input) {

	BigInteger bi = BigInteger.ZERO;

	for (int ix = 0; ix < input.length(); ix++) {
		final char c = input.charAt(ix);
		final int index = ALPHABET.indexOf(c);
		if (index == -1) {
			throw new RuntimeException("invalid char:" + c);
		}

		bi = bi.multiply(BIGINT_58);
		bi = bi.add(BigInteger.valueOf(index));
	}
	final byte[] bytes = bi.toByteArray();

	final boolean stripSignByte = (bytes.length > 1) && (bytes[0] == 0) && (bytes[1] >= (byte) 0x80);

	ArrayUtils.reverse(bytes);

	final ByteArrayOutputStream bout = new ByteArrayOutputStream();
	try {
		if (stripSignByte) {
			bout.write(bytes, 0, bytes.length - 1);
		} else {
			bout.write(bytes);
		}
		for (int ix = 0; (ix < input.length()) && (input.charAt(ix) == ALPHABET.charAt(0)); ix++) {
			bout.write(new byte[1]);
		}
	} catch (final IOException e) {
		throw new RuntimeException(e);
	}

	final byte[] ba = bout.toByteArray();
	return ba;
}
 
Example 11
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 5 votes
/**
 * coverts a scriptHash to an address.
 *
 * @param scriptHash
 *            the scriptHash to use.
 * @return the address.
 */
public static String toAddress(final UInt160 scriptHash) {
	final byte[] data = new byte[21];

	if (LOG.isTraceEnabled()) {
		LOG.trace("toAddress ADDRESS_VERSION {}", ModelUtil.toHexString(ADDRESS_VERSION));
	}

	final byte[] scriptHashBa = scriptHash.toByteArray();
	System.arraycopy(scriptHashBa, 0, data, 0, scriptHashBa.length);

	data[data.length - 1] = ADDRESS_VERSION;
	if (LOG.isTraceEnabled()) {
		LOG.info("toAddress data {}", ModelUtil.toHexString(data));
	}

	final byte[] dataAndChecksum = new byte[25];
	System.arraycopy(data, 0, dataAndChecksum, 4, data.length);

	ArrayUtils.reverse(data);
	final byte[] hash = SHA256HashUtil.getDoubleSHA256Hash(data);
	final byte[] hash4 = new byte[4];
	System.arraycopy(hash, 0, hash4, 0, 4);
	ArrayUtils.reverse(hash4);
	System.arraycopy(hash4, 0, dataAndChecksum, 0, 4);
	if (LOG.isTraceEnabled()) {
		LOG.info("toAddress dataAndChecksum {}", ModelUtil.toHexString(dataAndChecksum));
	}

	final String address = toBase58String(dataAndChecksum);
	return address;
}
 
Example 12
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 5 votes
/**
 * converts a byte array to a hex string in reverse byte order.
 *
 * @param bytes
 *            the array of bytes.
 * @return the string.
 */
public static String toReverseHexString(final byte... bytes) {
	final byte[] ba = new byte[bytes.length];
	System.arraycopy(bytes, 0, ba, 0, bytes.length);
	ArrayUtils.reverse(ba);
	final BigInteger bi = new BigInteger(1, ba);
	return bi.toString(16);
}
 
Example 13
Project: neo-java   File: VersionPayload.java   View Source Code Vote up 4 votes
@Override
public byte[] toByteArray() {
	try {
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		final byte[] versionBa = version.getBytesCopy();
		ArrayUtils.reverse(versionBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version {}", Hex.encodeHexString(versionBa));
		}
		out.write(versionBa);
		final byte[] servicesBa = services.getBytesCopy();
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload services {}", Hex.encodeHexString(servicesBa));
		}
		ArrayUtils.reverse(servicesBa);
		out.write(servicesBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services {}", Hex.encodeHexString(out.toByteArray()));
		}
		final byte[] timestampBa = timestamp.getBytesCopy();
		ArrayUtils.reverse(timestampBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload timestamp {}", Hex.encodeHexString(timestampBa));
		}
		out.write(timestampBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp {}", Hex.encodeHexString(out.toByteArray()));
		}
		final byte[] portBa = port.getBytesCopy();
		ArrayUtils.reverse(portBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload port {}", Hex.encodeHexString(portBa));
		}
		out.write(portBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp+port {}", Hex.encodeHexString(out.toByteArray()));
		}
		final byte[] nonceBa = nonce.getBytesCopy();
		ArrayUtils.reverse(nonceBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload nonce {}", Hex.encodeHexString(nonceBa));
		}
		out.write(nonceBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp+port+nonce {}",
					Hex.encodeHexString(out.toByteArray()));
		}
		NetworkUtil.writeString(out, userAgent);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp+port+nonce+userAgent {}",
					Hex.encodeHexString(out.toByteArray()));
		}

		final byte[] startHeightBa = startHeight.getBytesCopy();
		ArrayUtils.reverse(startHeightBa);
		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp+port+nonce+userAgent+startHeight {}",
					Hex.encodeHexString(out.toByteArray()));
		}

		out.write(startHeightBa);

		if (relay) {
			out.write(new byte[] { 1 });
		} else {
			out.write(new byte[] { 0 });
		}

		if (LOG.isTraceEnabled()) {
			LOG.trace("versionPayload version+services+timestamp+port+nonce+userAgent+startHeight+relay {}",
					Hex.encodeHexString(out.toByteArray()));
		}

		return out.toByteArray();
	} catch (final IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 14
Project: neo-java   File: Base58Util.java   View Source Code Vote up 4 votes
/**
 * encodes a Base58 byte array.
 *
 * @param input
 *            the byte array to use.
 * @return the encoded Base58 string.
 */
public static String encode(final byte[] input) {
	try {
		final byte[] revInput = new byte[input.length];
		System.arraycopy(input, 0, revInput, 0, input.length);
		ArrayUtils.reverse(revInput);

		BigInteger value = new BigInteger(1, revInput);
		final StringBuilder sb = new StringBuilder();
		if (LOG.isDebugEnabled()) {
			LOG.debug("[0]val58: {}", ModelUtil.toHexString(BIGINT_58.toByteArray()));
			LOG.debug("[0]value: {}", ModelUtil.toHexString(value.toByteArray()));
		}

		while (value.compareTo(BigInteger.ZERO) != 0) {
			final BigInteger[] valueDivAndRemainder = value.divideAndRemainder(BIGINT_58);
			final BigInteger valueDiv = valueDivAndRemainder[0];
			final BigInteger valueRemainder = valueDivAndRemainder[1];

			sb.append(ALPHABET.charAt(valueRemainder.intValue()));
			value = valueDiv;
			if (LOG.isDebugEnabled()) {
				LOG.debug("[1]value: {}", ModelUtil.toHexString(value.toByteArray()));
				LOG.debug("[1]value.compareTo(val58): {}", value.compareTo(BIGINT_58));
			}
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("[2]value: {}", ModelUtil.toHexString(value.toByteArray()));
		}

		for (int ix = 0; (ix < revInput.length) && (revInput[ix] == 0); ix++) {
			sb.append(ALPHABET.charAt(0));
		}

		sb.reverse();

		return sb.toString();
	} catch (final Exception e) {
		throw new RuntimeException("error encoding \"" + ModelUtil.toHexString(input) + "\"", e);
	}
}
 
Example 15
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 3 votes
/**
 * copies and reverses a byte array.
 *
 * @param input
 *            the byte array to copy and reverse.
 * @return a copy of the byte array, in reverse byte order.
 */
public static byte[] copyAndReverse(final byte[] input) {
	final byte[] revInput = new byte[input.length];
	System.arraycopy(input, 0, revInput, 0, input.length);
	ArrayUtils.reverse(revInput);
	return revInput;
}
 
Example 16
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 3 votes
/**
 * gets a fixed length byte array from the ByteBuffer.
 *
 * @param bb
 *            the ByteBuffer to read.
 * @param size
 *            the size of the byte array.
 * @param reverse
 *            if true, reverse the byte array.
 * @return the fixed length byte array.
 */
public static byte[] getFixedLengthByteArray(final ByteBuffer bb, final int size, final boolean reverse) {
	final byte[] ba = new byte[size];
	bb.get(ba);
	if (reverse) {
		ArrayUtils.reverse(ba);
	}
	return ba;
}
 
Example 17
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 3 votes
/**
 * returns a UInt160 read from the ByteBuffer.
 *
 * @param bb
 *            the ByteBuffer to read.
 * @param reverse
 *            if true, reverse the byte array creating the data used to create
 *            the object before creating the object.
 * @return the new UInt160.
 */
public static UInt160 getUInt160(final ByteBuffer bb, final boolean reverse) {
	final byte[] ba = getFixedLengthByteArray(bb, UInt160.SIZE, true);
	if (reverse) {
		ArrayUtils.reverse(ba);
	}
	return new UInt160(ba);
}
 
Example 18
Project: neo-java   File: ModelUtil.java   View Source Code Vote up 3 votes
/**
 * returns a UInt256 read from the ByteBuffer.
 *
 * @param bb
 *            the ByteBuffer to read.
 * @param reverse
 *            if true, reverse the byte array creating the data used to create
 *            the object before creating the object.
 * @return the new UInt256.
 */
public static UInt256 getUInt256(final ByteBuffer bb, final boolean reverse) {
	final byte[] ba = getFixedLengthByteArray(bb, UInt256.SIZE, true);
	if (reverse) {
		ArrayUtils.reverse(ba);
	}
	return new UInt256(ba);
}