Java Code Examples for com.google.common.primitives.Ints#toByteArray()

The following examples show how to use com.google.common.primitives.Ints#toByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectStoreDeleteConverter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<ObjectStoreDeleteOperation> convertRecord(Class<?> outputSchema, GenericRecord inputRecord,
    WorkUnitState workUnit) throws DataConversionException {
  Optional<Object> fieldValue = AvroUtils.getFieldValue(inputRecord, this.objectIdField);
  byte[] objectId;
  if (fieldValue.isPresent()) {
    if (fieldValue.get() instanceof Utf8) {
      objectId = ((Utf8) fieldValue.get()).getBytes();
    } else if (fieldValue.get() instanceof String) {
      objectId = ((String) fieldValue.get()).getBytes(Charsets.UTF_8);
    } else if (fieldValue.get() instanceof Long) {
      objectId = Longs.toByteArray((Long) fieldValue.get());
    } else if (fieldValue.get() instanceof Integer) {
      objectId = Ints.toByteArray((Integer) fieldValue.get());
    } else {
      objectId = (byte[]) fieldValue.get();
    }
    return new SingleRecordIterable<ObjectStoreDeleteOperation>(ObjectStoreOperationBuilder.deleteBuilder()
        .withObjectId(objectId).build());
  } else {
    throw new DataConversionException(String.format("Object Id field %s not found in record %s", this.objectIdField,
        inputRecord));
  }
}
 
Example 2
Source File: BlockMessage.java    From Qora with MIT License 6 votes vote down vote up
public byte[] toBytes() 
{
	byte[] data = new byte[0];
	
	//WRITE BLOCK HEIGHT
	byte[] heightBytes = Ints.toByteArray(this.block.getHeight());
	data = Bytes.concat(data, heightBytes);
	
	//WRITE BLOCK
	byte[] blockBytes = this.block.toBytes();
	data = Bytes.concat(data, blockBytes);
	
	//ADD CHECKSUM
	data = Bytes.concat(super.toBytes(), this.generateChecksum(data), data);
	
	return data;
}
 
Example 3
Source File: GenesisTransaction.java    From Qora with MIT License 5 votes vote down vote up
public boolean isSignatureValid()
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(GENESIS_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE RECIPIENT
	data = Bytes.concat(data, Base58.decode(this.recipient.getAddress()));
			
	//WRITE AMOUNT
	byte[] amountBytes = this.amount.unscaledValue().toByteArray();
	byte[] fill = new byte[AMOUNT_LENGTH - amountBytes.length];
	amountBytes = Bytes.concat(fill, amountBytes);
	data = Bytes.concat(data, amountBytes);
	
	//DIGEST
	byte[] digest = Crypto.getInstance().digest(data);
	digest = Bytes.concat(digest, digest);
			
	//CHECK IF EQUAL
	return Arrays.equals(digest, this.signature);
}
 
Example 4
Source File: CreatePollTransaction.java    From Qora with MIT License 5 votes vote down vote up
public boolean isSignatureValid()
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(REGISTER_NAME_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE CREATOR
	data = Bytes.concat(data, this.creator.getPublicKey());
	
	//WRITE POLL
	data = Bytes.concat(data , this.poll.toBytes());
	
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	byte[] fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);
			
	return Crypto.getInstance().verify(this.creator.getPublicKey(), this.signature, data);
}
 
Example 5
Source File: TestThriftIndexPageSource.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<PrestoThriftPageResult> getRows(PrestoThriftId splitId, List<String> columns, long maxBytes, PrestoThriftNullableToken nextToken)
{
    if (rowsPerSplit == 0) {
        return immediateFuture(new PrestoThriftPageResult(ImmutableList.of(), 0, null));
    }
    int key = Ints.fromByteArray(splitId.getId());
    int offset = nextToken.getToken() != null ? Ints.fromByteArray(nextToken.getToken().getId()) : 0;
    PrestoThriftId newNextToken = offset + 1 < rowsPerSplit ? new PrestoThriftId(Ints.toByteArray(offset + 1)) : null;
    return immediateFuture(pageResult(key * 10 + offset, newNextToken));
}
 
Example 6
Source File: CancelOrderTransaction.java    From Qora with MIT License 5 votes vote down vote up
@Override
public boolean isSignatureValid() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(CANCEL_ORDER_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE CREATOR
	data = Bytes.concat(data, this.creator.getPublicKey());
	
	//WRITE ORDER
	byte[] orderBytes = this.order.toByteArray();
	byte[] fill = new byte[ORDER_LENGTH - orderBytes.length];
	orderBytes = Bytes.concat(fill, orderBytes);
	data = Bytes.concat(data, orderBytes);
			
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);
	
	return Crypto.getInstance().verify(this.creator.getPublicKey(), this.signature, data);
}
 
Example 7
Source File: SellNameTransaction.java    From Qora with MIT License 5 votes vote down vote up
@Override
public boolean isSignatureValid() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(SELL_NAME_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE OWNER
	data = Bytes.concat(data, this.owner.getPublicKey());
	
	//WRITE NAMESALE
	data = Bytes.concat(data, this.nameSale.toBytes());
	
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	byte[] fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);

	return Crypto.getInstance().verify(this.owner.getPublicKey(), this.signature, data);
}
 
Example 8
Source File: SellNameTransaction.java    From Qora with MIT License 5 votes vote down vote up
public static byte[] generateSignature(DBSet db, PrivateKeyAccount creator, NameSale nameSale, BigDecimal fee, long timestamp) 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(SELL_NAME_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, creator.getLastReference(db));
	
	//WRITE OWNER
	data = Bytes.concat(data, creator.getPublicKey());
	
	//WRITE NAMESALE
	data = Bytes.concat(data, nameSale.toBytes());
	
	//WRITE FEE
	byte[] feeBytes = fee.unscaledValue().toByteArray();
	byte[] fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);
	
	return Crypto.getInstance().sign(creator, data);
}
 
Example 9
Source File: FixedBufferTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void testRead4PrefixedString() throws Exception {
    String value = "test";
    byte[] length = Ints.toByteArray(value.length());
    byte[] string = value.getBytes();
    byte[] result = BytesUtils.merge(length, string);


    Buffer buffer = new FixedBuffer(result);
    String prefixedString = buffer.read4PrefixedString();
    Assert.assertEquals(prefixedString, value);

}
 
Example 10
Source File: ArbitraryTransaction.java    From Qora with MIT License 5 votes vote down vote up
public static byte[] generateSignature(DBSet db, PrivateKeyAccount creator, int service, byte[] arbitraryData, BigDecimal fee, long timestamp) 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(ARBITRARY_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, creator.getLastReference(db));
	
	//WRITE CREATOR
	data = Bytes.concat(data, creator.getPublicKey());
	
	//WRITE SERVICE
	byte[] serviceBytes = Ints.toByteArray(service);
	data = Bytes.concat(data, serviceBytes);
	
	//WRITE DATA SIZE
	byte[] dataSizeBytes = Ints.toByteArray(arbitraryData.length);
	data = Bytes.concat(data, dataSizeBytes);
	
	//WRITE DATA
	data = Bytes.concat(data, arbitraryData);
	
	//WRITE FEE
	byte[] feeBytes = fee.unscaledValue().toByteArray();
	byte[] fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);
	
	return Crypto.getInstance().sign(creator, data);
}
 
Example 11
Source File: NumberUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
public static byte[] toBytes(int value) {
	return Ints.toByteArray(value);
}
 
Example 12
Source File: TransferAssetTransaction.java    From Qora with MIT License 4 votes vote down vote up
public boolean isSignatureValid()
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(TRANSFER_ASSET_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE SENDER
	data = Bytes.concat(data , this.sender.getPublicKey());
	
	//WRITE RECIPIENT
	data = Bytes.concat(data, Base58.decode(this.recipient.getAddress()));
	
	//WRITE KEY
	byte[] keyBytes = Longs.toByteArray(this.key);
	keyBytes = Bytes.ensureCapacity(keyBytes, KEY_LENGTH, 0);
	data = Bytes.concat(data, keyBytes);
	
	//WRITE AMOUNT
	byte[] amountBytes = this.amount.unscaledValue().toByteArray();
	byte[] fill = new byte[AMOUNT_LENGTH - amountBytes.length];
	amountBytes = Bytes.concat(fill, amountBytes);
	data = Bytes.concat(data, amountBytes);
	
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);
			
	return Crypto.getInstance().verify(this.sender.getPublicKey(), this.signature, data);
}
 
Example 13
Source File: MiniZKFCCluster.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
protected byte[] targetToData(HAServiceTarget target) {
  return Ints.toByteArray(((DummyHAService)target).index);
}
 
Example 14
Source File: MultiQpidByteBuffer.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Override
public final QpidByteBuffer putInt(final int value)
{
    byte[] valueArray = Ints.toByteArray(value);
    return put(valueArray);
}
 
Example 15
Source File: Asset.java    From Qora with MIT License 4 votes vote down vote up
public byte[] toBytes(boolean includeReference)
{
	byte[] data = new byte[0];
	
	//WRITE OWNER
	try
	{
		data = Bytes.concat(data , Base58.decode(this.owner.getAddress()));
	}
	catch(Exception e)
	{
		//DECODE EXCEPTION
	}
	
	//WRITE NAME SIZE
	byte[] nameBytes = this.name.getBytes(StandardCharsets.UTF_8);
	int nameLength = nameBytes.length;
	byte[] nameLengthBytes = Ints.toByteArray(nameLength);
	data = Bytes.concat(data, nameLengthBytes);
	
	//WRITE NAME
	data = Bytes.concat(data, nameBytes);
	
	//WRITE DESCRIPTION SIZE
	byte[] descriptionBytes = this.description.getBytes(StandardCharsets.UTF_8);
	int descriptionLength = descriptionBytes.length;
	byte[] descriptionLengthBytes = Ints.toByteArray(descriptionLength);
	data = Bytes.concat(data, descriptionLengthBytes);
			
	//WRITE DESCRIPTION
	data = Bytes.concat(data, descriptionBytes);
	
	//WRITE QUANTITY
	byte[] quantityBytes = Longs.toByteArray(this.quantity);
	data = Bytes.concat(data, quantityBytes);
	
	//WRITE DIVISIBLE
	byte[] divisibleBytes = new byte[1];
	divisibleBytes[0] = (byte) (this.divisible == true ? 1 : 0);
	data = Bytes.concat(data, divisibleBytes);
	
	if(includeReference)
	{
		//WRITE REFERENCE
		data = Bytes.concat(data, this.reference);
	}
	else
	{
		//WRITE EMPTY REFERENCE
		data = Bytes.concat(data, new byte[64]);
	}
	
	return data;
}
 
Example 16
Source File: ByteArray.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static byte[] fromInt(int val) {
    return Ints.toByteArray(val);
}
 
Example 17
Source File: PaymentTransaction.java    From Qora with MIT License 4 votes vote down vote up
@Override
public byte[] toBytes() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(PAYMENT_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE SENDER
	data = Bytes.concat(data , this.sender.getPublicKey());
	
	//WRITE RECIPIENT
	data = Bytes.concat(data, Base58.decode(this.recipient.getAddress()));
	
	//WRITE AMOUNT
	byte[] amountBytes = this.amount.unscaledValue().toByteArray();
	byte[] fill = new byte[AMOUNT_LENGTH - amountBytes.length];
	amountBytes = Bytes.concat(fill, amountBytes);
	data = Bytes.concat(data, amountBytes);
	
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);

	//SIGNATURE
	data = Bytes.concat(data, this.signature);
	
	return data;
}
 
Example 18
Source File: MultiPaymentTransaction.java    From Qora with MIT License 4 votes vote down vote up
@Override
public byte[] toBytes() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(MULTI_PAYMENT_TRANSACTION);
	typeBytes = Bytes.ensureCapacity(typeBytes, TYPE_LENGTH, 0);
	data = Bytes.concat(data, typeBytes);
	
	//WRITE TIMESTAMP
	byte[] timestampBytes = Longs.toByteArray(this.timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, this.reference);
	
	//WRITE SENDER
	data = Bytes.concat(data , this.sender.getPublicKey());

	//WRITE PAYMENTS SIZE
	int paymentsLength = this.payments.size();
	byte[] paymentsLengthBytes = Ints.toByteArray(paymentsLength);
	data = Bytes.concat(data, paymentsLengthBytes);
	
	//WRITE PAYMENTS
	for(Payment payment: this.payments)
	{
		data = Bytes.concat(data, payment.toBytes());
	}
	
	//WRITE FEE
	byte[] feeBytes = this.fee.unscaledValue().toByteArray();
	byte[] fill = new byte[FEE_LENGTH - feeBytes.length];
	feeBytes = Bytes.concat(fill, feeBytes);
	data = Bytes.concat(data, feeBytes);

	//SIGNATURE
	data = Bytes.concat(data, this.signature);
	
	return data;
}
 
Example 19
Source File: Validation.java    From onos with Apache License 2.0 3 votes vote down vote up
/**
 * Throws exception if type is not correct.
 *
 * @param errorCode Error code
 * @param subErrCode Sub Error Code
 * @param type erroneous type
 * @throws BgpParseException for erroneous type
 */
public static void validateType(byte errorCode, byte subErrCode, int type) throws BgpParseException {
    byte[] errType = Ints.toByteArray(type);
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeBytes(errType);
    throw new BgpParseException(errorCode, subErrCode, buffer);
}
 
Example 20
Source File: FixedBufferTest.java    From pinpoint with Apache License 2.0 3 votes vote down vote up
@Test
public void testRead4PrefixedString_Null() throws Exception {
    byte[] length = Ints.toByteArray(-1);


    Buffer buffer = new FixedBuffer(length);
    String prefixedString = buffer.read4PrefixedString();
    Assert.assertEquals(prefixedString, null);

}