Java 类org.bitcoinj.core.VarInt 实例源码

项目:golos4j    文件:Util.java   
/**
 * Transform a long value into its byte representation.
 * 
 * @param longValue
 *            value The long value to transform.
 * @return The byte representation of the given value.
 */
public static byte[] long2VarIntByteArray(long longValue) {
    try {
        long value = longValue;

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DataOutput out = new DataOutputStream(byteArrayOutputStream);

        while ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
            out.writeByte(((int) value & 0x7F) | 0x80);
            value >>>= 7;
        }

        out.writeByte((int) value & 0x7F);

        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        LOG.error("Could not transform the given long value into its VarInt representation - "
                + "Using BitcoinJ as Fallback. This could cause problems for values > 127.", e);
        return (new VarInt(longValue)).encode();
    }
}
项目:golos4j    文件:TransactionUtil.java   
/**
 * Конвертировать long value в массив байт.
 * 
 * @param longValue
 *            число.
 * @return число в виде списка байт.
 * @throws BusinessException
 *             бизнес иключение
 */
public static byte[] long2VarIntByteArray(long longValue)
        throws BusinessException {
    try {
        long value = longValue;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        DataOutput out = new DataOutputStream(byteArrayOutputStream);
        while ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
            out.writeByte(((int) value & 0x7F) | 0x80);
            value >>>= 7;
        }
        out.writeByte((int) value & 0x7F);
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        LOG.warn(
                "Unable transform long value to VarInt . This could cause problems for values > 127."
                        + e);
        return (new VarInt(longValue)).encode();
    }
}