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

The following examples show how to use com.google.common.primitives.Longs#toByteArray() . These examples are extracted from open source projects. 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
public byte[] getEncoded() {
    if (protoEncoded != null) {
        return protoEncoded.clone();
    }
    byte[] parentHashArray = parentHash.clone();

    if (parentHashArray == null) {
        parentHashArray = ByteUtil.EMPTY_BYTE_ARRAY;
    }
    byte[] valueByte = Longs.toByteArray(this.value);
    byte[] raw = new byte[parentHashArray.length + this.receiveAddress.length + this.data.length
            + valueByte.length];
    System.arraycopy(parentHashArray, 0, raw, 0, parentHashArray.length);
    System
            .arraycopy(this.receiveAddress, 0, raw, parentHashArray.length, this.receiveAddress.length);
    System.arraycopy(this.data, 0, raw, parentHashArray.length + this.receiveAddress.length,
            this.data.length);
    System.arraycopy(valueByte, 0, raw,
            parentHashArray.length + this.receiveAddress.length + this.data.length,
            valueByte.length);
    this.protoEncoded = raw;
    return protoEncoded.clone();
}
 
Example 2
Source Project: Qora   File: IssueAssetTransaction.java    License: MIT License 5 votes vote down vote up
@Override
public byte[] toBytes() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(ISSUE_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 ISSUER
	data = Bytes.concat(data, this.issuer.getPublicKey());
	
	//WRITE ASSET
	data = Bytes.concat(data , this.asset.toBytes(true));
	
	//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 3
Source Project: hadoop-ozone   File: BlockManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private byte[] getBlockByID(ReferenceCountedDB db, BlockID blockID)
    throws IOException {
  byte[] blockKey = Longs.toByteArray(blockID.getLocalID());

  byte[] blockData = db.getStore().get(blockKey);
  if (blockData == null) {
    throw new StorageContainerException(NO_SUCH_BLOCK_ERR_MSG,
        NO_SUCH_BLOCK);
  }

  return blockData;
}
 
Example 4
Source Project: Qora   File: RegisterNameTransaction.java    License: MIT License 5 votes vote down vote up
public static byte[] generateSignature(DBSet db, PrivateKeyAccount creator, Name name, BigDecimal fee, long timestamp) 
{
	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(timestamp);
	timestampBytes = Bytes.ensureCapacity(timestampBytes, TIMESTAMP_LENGTH, 0);
	data = Bytes.concat(data, timestampBytes);
	
	//WRITE REFERENCE
	data = Bytes.concat(data, creator.getLastReference(db));
	
	//WRITE REGISTRANT
	data = Bytes.concat(data, creator.getPublicKey());
	
	//WRITE NAME
	data = Bytes.concat(data , name.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 5
Source Project: Qora   File: CancelSellNameTransaction.java    License: MIT License 5 votes vote down vote up
@Override
public boolean isSignatureValid() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(CANCEL_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 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 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 6
Source Project: Qora   File: GenesisTransaction.java    License: 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 7
Source Project: Qora   File: CreatePollTransaction.java    License: MIT License 5 votes vote down vote up
@Override
public byte[] toBytes() 
{
	byte[] data = new byte[0];
	
	//WRITE TYPE
	byte[] typeBytes = Ints.toByteArray(CREATE_POLL_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);

	//SIGNATURE
	data = Bytes.concat(data, this.signature);
	
	return data;
}
 
Example 8
Source Project: Qora   File: CancelOrderTransaction.java    License: 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 9
Source Project: Qora   File: RegisterNameTransaction.java    License: MIT License 5 votes vote down vote up
@Override
public byte[] toBytes() 
{
	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 REGISTRANT
	data = Bytes.concat(data, this.registrant.getPublicKey());
	
	//WRITE NAME
	data = Bytes.concat(data , this.name.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 10
Source Project: teku   File: UnsignedLongSerializer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public byte[] serialize(final UnsignedLong value) {
  return Longs.toByteArray(value.longValue());
}
 
Example 11
/**
 * Creates a document for indexing.
 * <p>
 * For this connector sample, the created document is domain public
 * searchable. The content is a simple text string. In addition to indexing
 * the current document, the child nodes are also pushed into the queue.
 * This method will later be called for the child nodes as they're pulled
 * from the queue.
 *
 * @param documentId unique local id for the document
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocumentAndChildren(String documentId) {
  // [START cloud_search_content_sdk_domain_acl]
  // Make the document publicly readable within the domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal()))
      .build();
  // [END cloud_search_content_sdk_domain_acl]


  // [START cloud_search_content_sdk_build_item]
  // Url is required. Use google.com as a placeholder for this sample.
  String viewUrl = "https://www.google.com";

  // Version is required, set to current timestamp.
  byte[] version = Longs.toByteArray(System.currentTimeMillis());

  // Using the SDK item builder class to create the document with
  // appropriate attributes. This can be expanded to include metadata
  // fields etc.
  Item item = IndexingItemBuilder.fromConfiguration(documentId)
      .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .build();
  // [END cloud_search_content_sdk_build_item]

  // [START cloud_search_content_sdk_build_repository_doc]
  // For this sample, content is just plain text
  String content = String.format("Hello world from sample doc %s", documentId);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  RepositoryDoc.Builder docBuilder = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, IndexingService.ContentFormat.TEXT);
  // [END cloud_search_content_sdk_build_repository_doc]

  // [START cloud_search_content_sdk_add_children]
  // Queue the child nodes to visit after indexing this document
  Set<String> childIds = getChildItemNames(documentId);
  for (String id : childIds) {
    log.info(() -> String.format("Pushing child node %s", id));
    PushItem pushItem = new PushItem();
    docBuilder.addChildId(id, pushItem);
  }

  RepositoryDoc doc = docBuilder.build();
  // [END cloud_search_content_sdk_add_children]
  return doc;
}
 
Example 12
@Override
public QpidByteBuffer putLong(final int index, final long value)
{
    byte[] valueArray = Longs.toByteArray(value);
    return put(index, valueArray);
}
 
Example 13
@Override
public void writeLongUnchecked(long value) throws IOException {
	byte[] bytes = Longs.toByteArray(Long.reverseBytes(value));
	dataOutputStream.write(bytes, 0, bytes.length);
}
 
Example 14
Source Project: big-c   File: TestCryptoCodec.java    License: Apache License 2.0 4 votes vote down vote up
private static BigInteger calculateRef(byte[] initIV, long counter) {
  byte[] cb = Longs.toByteArray(counter);
  BigInteger bi = new BigInteger(1, initIV);
  return bi.add(new BigInteger(1, cb));
}
 
Example 15
Source Project: besu   File: Gas.java    License: Apache License 2.0 4 votes vote down vote up
public byte[] getBytes() {
  return Longs.toByteArray(value);
}
 
Example 16
@NotNull
public static byte[] serializeKey(final long id) {
    return Longs.toByteArray(id);
}
 
Example 17
Source Project: Qora   File: Block.java    License: MIT License 4 votes vote down vote up
public boolean isSignatureValid()
{
	//VALIDATE BLOCK SIGNATURE
	byte[] data = new byte[0];
	
	//WRITE PARENT GENERATOR SIGNATURE
	byte[] generatorSignature = Arrays.copyOfRange(this.reference, 0, GENERATOR_SIGNATURE_LENGTH);
	data = Bytes.concat(data, generatorSignature);
	
	//WRITE GENERATING BALANCE
	byte[] baseTargetBytes = Longs.toByteArray(this.generatingBalance);
	data = Bytes.concat(data, baseTargetBytes);
	
	//WRITE GENERATOR
	byte[] generatorBytes = Bytes.ensureCapacity(this.generator.getPublicKey(), GENERATOR_LENGTH, 0);
	data = Bytes.concat(data, generatorBytes);
							
	if(!Crypto.getInstance().verify(this.generator.getPublicKey(), this.generatorSignature, data))
	{
		return false;
	}
	
	//VALIDATE TRANSACTIONS SIGNATURE
	data = this.generatorSignature;		
	for(Transaction transaction: this.getTransactions())
	{
		//CHECK IF TRANSACTION SIGNATURE IS VALID
		if(!transaction.isSignatureValid())
		{
			return false;
		}
		
		//ADD SIGNATURE TO DATA
		data = Bytes.concat(data, transaction.getSignature());
	}
	
	if(!Crypto.getInstance().verify(this.generator.getPublicKey(), this.transactionsSignature, data))
	{
		return false;
	}
	
	return true;
}
 
Example 18
Source Project: HaloDB   File: MemoryPoolChunkTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testSetAndGetMethods() {
    int chunkSize = 16 * 1024;
    int fixedKeyLength = 12, fixedValueLength = 20;
    int slotSize = MemoryPoolHashEntries.HEADER_SIZE + fixedKeyLength + fixedValueLength;

    chunk = MemoryPoolChunk.create(chunkSize, fixedKeyLength, fixedValueLength);
    int offset = chunk.getWriteOffset();

    Assert.assertEquals(chunk.remaining(), chunkSize);
    Assert.assertEquals(chunk.getWriteOffset(), 0);

    // write to an empty slot.
    byte[] key = Longs.toByteArray(101);
    byte[] value = HashTableTestUtils.randomBytes(fixedValueLength);
    MemoryPoolAddress nextAddress = new MemoryPoolAddress((byte) 10, 34343);
    chunk.fillNextSlot(key, value, nextAddress);

    Assert.assertEquals(chunk.getWriteOffset(), offset + slotSize);
    Assert.assertEquals(chunk.remaining(), chunkSize-slotSize);
    Assert.assertTrue(chunk.compareKey(offset, key));
    Assert.assertTrue(chunk.compareValue(offset, value));

    MemoryPoolAddress actual = chunk.getNextAddress(offset);
    Assert.assertEquals(actual.chunkIndex, nextAddress.chunkIndex);
    Assert.assertEquals(actual.chunkOffset, nextAddress.chunkOffset);

    // write to the next empty slot.
    byte[] key2 = HashTableTestUtils.randomBytes(fixedKeyLength);
    byte[] value2 = HashTableTestUtils.randomBytes(fixedValueLength);
    MemoryPoolAddress nextAddress2 = new MemoryPoolAddress((byte) 0, 4454545);
    chunk.fillNextSlot(key2, value2, nextAddress2);
    Assert.assertEquals(chunk.getWriteOffset(), offset + 2*slotSize);
    Assert.assertEquals(chunk.remaining(), chunkSize-2*slotSize);

    offset += slotSize;
    Assert.assertTrue(chunk.compareKey(offset, key2));
    Assert.assertTrue(chunk.compareValue(offset, value2));

    actual = chunk.getNextAddress(offset);
    Assert.assertEquals(actual.chunkIndex, nextAddress2.chunkIndex);
    Assert.assertEquals(actual.chunkOffset, nextAddress2.chunkOffset);

    // update an existing slot.
    byte[] key3 = Longs.toByteArray(0x64735981289L);
    byte[] value3 = HashTableTestUtils.randomBytes(fixedValueLength);
    MemoryPoolAddress nextAddress3 = new MemoryPoolAddress((byte)-1, -1);
    chunk.fillSlot(0, key3, value3, nextAddress3);

    offset = 0;
    Assert.assertTrue(chunk.compareKey(offset, key3));
    Assert.assertTrue(chunk.compareValue(offset, value3));

    // write offset should remain unchanged.
    Assert.assertEquals(chunk.getWriteOffset(), offset + 2*slotSize);
    Assert.assertEquals(chunk.remaining(), chunkSize-2*slotSize);
}
 
Example 19
/**
 * Creates a document for indexing.
 * <p>
 * For this connector sample, the created document is domain public
 * searchable. The content is a simple text string.
 *
 * @param documentId unique local id for the document
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocument(int documentId) {
  // [START cloud_search_content_sdk_domain_acl]
  // Make the document publicly readable within the domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal()))
      .build();
  // [END cloud_search_content_sdk_domain_acl]

  // [START cloud_search_content_sdk_build_item]
  // Url is required. Use google.com as a placeholder for this sample.
  String viewUrl = "https://www.google.com";

  // Version is required, set to current timestamp.
  byte[] version = Longs.toByteArray(System.currentTimeMillis());

  // Set metadata hash so queue can detect changes
  String metadataHash = this.calculateMetadataHash(documentId);

  // Using the SDK item builder class to create the document with
  // appropriate attributes. This can be expanded to include metadata
  // fields etc.
  Item item = IndexingItemBuilder.fromConfiguration(Integer.toString(documentId))
      .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .setHash(metadataHash)
      .build();
  // [END cloud_search_content_sdk_build_item]

  // [START cloud_search_content_sdk_build_repository_doc]
  // For this sample, content is just plain text
  String content = String.format("Hello world from sample doc %d", documentId);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  // Create the fully formed document
  RepositoryDoc doc = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, IndexingService.ContentFormat.TEXT)
      .build();
  // [END cloud_search_content_sdk_build_repository_doc]
  return doc;
}
 
Example 20
/**
 * Writes a {@code long} as specified by
 * {@link DataOutputStream#writeLong(long)}, except using little-endian byte
 * order.
 *
 * @throws IOException
 *             if an I/O error occurs
 */
@Override
public void writeLong(long v) throws IOException {
	byte[] bytes = Longs.toByteArray(Long.reverseBytes(v));
	write(bytes, 0, bytes.length);
}