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

The following examples show how to use com.google.common.primitives.Longs#fromByteArray() . 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: H5tiledLayoutBB.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
DataChunk(DataBTree.DataChunk delegate) {
  this.delegate = delegate;

  // Check that the chunk length (delegate.size) isn't greater than the maximum array length that we can
  // allocate (MAX_ARRAY_LEN). This condition manifests in two ways.
  // 1) According to the HDF docs (https://www.hdfgroup.org/HDF5/doc/Advanced/Chunking/, "Chunk Maximum Limits"),
  // max chunk length is 4GB (i.e. representable in an unsigned int). Java, however, only has signed ints.
  // So, if we try to store a large unsigned int in a singed int, it'll overflow, and the signed int will come
  // out negative. We're trusting here that the chunk size read from the HDF file is never negative.
  // 2) In most JVM implementations MAX_ARRAY_LEN is actually less than Integer.MAX_VALUE (see note in ArrayList).
  // So, we could have: "MAX_ARRAY_LEN < chunkSize <= Integer.MAX_VALUE".
  if (delegate.size < 0 || delegate.size > MAX_ARRAY_LEN) {
    // We want to report the size of the chunk, but we may be in an arithmetic overflow situation. So to get the
    // correct value, we're going to reinterpet the integer's bytes as long bytes.
    byte[] intBytes = Ints.toByteArray(delegate.size);
    byte[] longBytes = new byte[8];
    System.arraycopy(intBytes, 0, longBytes, 4, 4); // Copy int bytes to the lowest 4 positions.
    long chunkSize = Longs.fromByteArray(longBytes); // Method requires an array of length 8.

    throw new IllegalArgumentException(String.format("Filtered data chunk is %s bytes and we must load it all "
        + "into memory. However the maximum length of a byte array in Java is %s.", chunkSize, MAX_ARRAY_LEN));
  }
}
 
Example 2
Source File: GenesisTransaction.java    From Qora with MIT License 6 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception{
	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ RECIPIENT
	byte[] recipientBytes = Arrays.copyOfRange(data, position, position + RECIPIENT_LENGTH);
	Account recipient = new Account(Base58.encode(recipientBytes));
	position += RECIPIENT_LENGTH;
	
	//READ AMOUNT
	byte[] amountBytes = Arrays.copyOfRange(data, position, position + AMOUNT_LENGTH);
	BigDecimal amount = new BigDecimal(new BigInteger(amountBytes), 8);
	
	return new GenesisTransaction(recipient, amount, timestamp);	
}
 
Example 3
Source File: MemcachedCache.java    From lsmtree with Apache License 2.0 5 votes vote down vote up
public boolean checkAvailability(String key) {
    long time = System.nanoTime();
    key += "-"+UUID.randomUUID().toString();
    OperationFuture<Boolean> future = memcache.set(key, CACHE_EXPIRY_SECONDS, Longs.toByteArray(time), identityTranscoder);
    try {
        if (!future.get()) return false;
    } catch (Exception e) {
        return false;
    }
    byte[] bytes = memcache.get(key, identityTranscoder);
    memcache.delete(key);
    return bytes != null && Longs.fromByteArray(bytes) == time;
}
 
Example 4
Source File: RegisterNameTransaction.java    From Qora with MIT License 5 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception
{	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ REGISTRANT
	byte[] registrantBytes = Arrays.copyOfRange(data, position, position + REGISTRANT_LENGTH);
	PublicKeyAccount registrant = new PublicKeyAccount(registrantBytes);
	position += REGISTRANT_LENGTH;
	
	//READ NAME
	Name name = Name.Parse(Arrays.copyOfRange(data, position, data.length));
	position += name.getDataLength();
	
	//READ FEE
	byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH);
	BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8);
	position += FEE_LENGTH;		
	
	//READ SIGNATURE
	byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH);
	
	return new RegisterNameTransaction(registrant, name, fee, timestamp, reference, signatureBytes);
}
 
Example 5
Source File: SellNameTransaction.java    From Qora with MIT License 5 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception
{	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ OWNER
	byte[] registrantBytes = Arrays.copyOfRange(data, position, position + OWNER_LENGTH);
	PublicKeyAccount owner = new PublicKeyAccount(registrantBytes);
	position += OWNER_LENGTH;
	
	//READ NAMESALE
	NameSale nameSale = NameSale.Parse(Arrays.copyOfRange(data, position, data.length));
	position += nameSale.getDataLength();
	
	//READ FEE
	byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH);
	BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8);
	position += FEE_LENGTH;		
	
	//READ SIGNATURE
	byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH);
	
	return new SellNameTransaction(owner, nameSale, fee, timestamp, reference, signatureBytes);
}
 
Example 6
Source File: CryptoUtils.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Remove mac byte [ ].
 *
 * @param key  the key
 * @param data the data
 * @return the byte [ ]
 */
public static byte[] removeMAC(long[] key, byte[] data) {
  long hash = SipHashInline.hash24_palindromic(key[0], key[1], data, 0, data.length - 8);
  long mac = Longs.fromByteArray(Arrays.copyOfRange(data, data.length - 8, data.length));

  if (mac == hash) {
    return Arrays.copyOf(data, data.length - 8);
  } else {
    return null;
  }
}
 
Example 7
Source File: KnowledgeBase.java    From fasten with Apache License 2.0 5 votes vote down vote up
private long uri2GID(final FastenURI uri) {
	byte[] result;
	try {
		result = callGraphDB.get(uri2gidFamilyHandle, uri.toString().getBytes(StandardCharsets.UTF_8));
	} catch (final RocksDBException e) {
		throw new RuntimeException(e);
	}
	if (result == null) return -1;
	return Longs.fromByteArray(result);
}
 
Example 8
Source File: JaegerExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private long spanIdToLong(final @Nullable SpanId spanId) {
  if (spanId == null) {
    return 0L;
  }
  // Attempt to minimise allocations, since SpanId#getBytes currently creates a defensive copy:
  spanId.copyBytesTo(spanIdBuffer, 0);
  return Longs.fromByteArray(spanIdBuffer);
}
 
Example 9
Source File: RedisDirectory.java    From RedisDirectory with Apache License 2.0 5 votes vote down vote up
private RedisFile loadRedisToFile(String fileName) {
    byte[] hget = inputOutputStream.hget(Constants.DIR_METADATA_BYTES, fileName.getBytes(), FILE_LENGTH);
    long lenght = Longs.fromByteArray(hget);
    RedisFile redisFile = new RedisFile(fileName, lenght);
    long blockSize = getBlockSize(lenght);
    List<byte[]> bytes = inputOutputStream.loadFileOnce(Constants.FILE_METADATA, fileName, blockSize);
    redisFile.setBuffers(bytes);
    return redisFile;
}
 
Example 10
Source File: DecimalMixedEndianGT8BytesComparator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected int compareInner(ArrowBuf left, int startIndexLeft, int valueLength,
                           ArrowBuf right, int startIndexRight) {
  int highBytesLength = valueLength - 8;
  // sign extend
  byte padValue = isLeftValNegative() ? padNegative : padZero;
  Arrays.fill(bytesHigh, 0, 8 - highBytesLength, padValue);
  left.getBytes(startIndexLeft, bytesHigh, 8 - highBytesLength, highBytesLength);
  long leftValHigh = Longs.fromByteArray(bytesHigh);

  left.getBytes(startIndexLeft + highBytesLength, bytesLow, 0, 8);
  long leftValLow = Longs.fromByteArray(bytesLow);

  return DecimalUtils.compareDecimalsAsTwoLongs(leftValHigh, leftValLow, getRightValHigh(),
    getRightValLow());
}
 
Example 11
Source File: Order.java    From Qora with MIT License 4 votes vote down vote up
public static Order parse(byte[] data) throws Exception
{
	//CHECK IF CORRECT LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match order length");
	}
	
	int position = 0;
	
	//READ ID
	byte[] idBytes = Arrays.copyOfRange(data, position, position + ID_LENGTH);
	BigInteger id = new BigInteger(idBytes);
	position += ID_LENGTH;
	
	//READ CREATOR
	byte[] creatorBytes = Arrays.copyOfRange(data, position, position + CREATOR_LENGTH);
	Account creator = new Account(Base58.encode(creatorBytes));
	position += CREATOR_LENGTH;
	
	//READ HAVE
	byte[] haveBytes = Arrays.copyOfRange(data, position, position + HAVE_LENGTH);
	long have = Longs.fromByteArray(haveBytes);	
	position += HAVE_LENGTH;
	
	//READ HAVE
	byte[] wantBytes = Arrays.copyOfRange(data, position, position + WANT_LENGTH);
	long want = Longs.fromByteArray(wantBytes);	
	position += WANT_LENGTH;
	
	//READ AMOUNT
	byte[] amountBytes = Arrays.copyOfRange(data, position, position + AMOUNT_LENGTH);
	BigDecimal amount = new BigDecimal(new BigInteger(amountBytes), 8);
	position += AMOUNT_LENGTH;		
	
	//READ FULFILLED
	byte[] fulfilledBytes = Arrays.copyOfRange(data, position, position + FULFILLED_LENGTH);
	BigDecimal fulfilled = new BigDecimal(new BigInteger(fulfilledBytes), 8);
	position += FULFILLED_LENGTH;		
	
	//READ PRICE
	byte[] priceBytes = Arrays.copyOfRange(data, position, position + PRICE_LENGTH);
	BigDecimal price = new BigDecimal(new BigInteger(priceBytes), 8);
	position += PRICE_LENGTH;	
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	return new Order(id, creator, have, want, amount, fulfilled, price, timestamp);
}
 
Example 12
Source File: TransferAssetTransaction.java    From Qora with MIT License 4 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception{
	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ SENDER
	byte[] senderBytes = Arrays.copyOfRange(data, position, position + SENDER_LENGTH);
	PublicKeyAccount sender = new PublicKeyAccount(senderBytes);
	position += SENDER_LENGTH;
	
	//READ RECIPIENT
	byte[] recipientBytes = Arrays.copyOfRange(data, position, position + RECIPIENT_LENGTH);
	Account recipient = new Account(Base58.encode(recipientBytes));
	position += RECIPIENT_LENGTH;
	
	//READ KEY
	byte[] keyBytes = Arrays.copyOfRange(data, position, position + KEY_LENGTH);
	long key = Longs.fromByteArray(keyBytes);	
	position += KEY_LENGTH;
	
	//READ AMOUNT
	byte[] amountBytes = Arrays.copyOfRange(data, position, position + AMOUNT_LENGTH);
	BigDecimal amount = new BigDecimal(new BigInteger(amountBytes), 8);
	position += AMOUNT_LENGTH;
	
	//READ FEE
	byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH);
	BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8);
	position += FEE_LENGTH;		
	
	//READ SIGNATURE
	byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH);
	
	return new TransferAssetTransaction(sender, recipient, key, amount, fee, timestamp, reference, signatureBytes);	
}
 
Example 13
Source File: CancelSellNameTransaction.java    From Qora with MIT License 4 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception
{	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ OWNER
	byte[] registrantBytes = Arrays.copyOfRange(data, position, position + OWNER_LENGTH);
	PublicKeyAccount owner = new PublicKeyAccount(registrantBytes);
	position += OWNER_LENGTH;
	
	//READ NAME
	byte[] nameLengthBytes = Arrays.copyOfRange(data, position, position + NAME_SIZE_LENGTH);
	int nameLength = Ints.fromByteArray(nameLengthBytes);
	position += NAME_SIZE_LENGTH;
					
	if(nameLength < 1 || nameLength > 400)
	{
		throw new Exception("Invalid name length");
	}
					
	byte[] nameBytes = Arrays.copyOfRange(data, position, position + nameLength);
	String name = new String(nameBytes, StandardCharsets.UTF_8);
	position += nameLength;
	
	//READ FEE
	byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH);
	BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8);
	position += FEE_LENGTH;		
	
	//READ SIGNATURE
	byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH);
	
	return new CancelSellNameTransaction(owner, name, fee, timestamp, reference, signatureBytes);
}
 
Example 14
Source File: LongLexicoder.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public Long fromByteArray(final byte[] bytes) {
  final long value = Longs.fromByteArray(bytes);
  return value ^ 0x8000000000000000l;
}
 
Example 15
Source File: VoteOnPollTransaction.java    From Qora with MIT License 4 votes vote down vote up
public static Transaction Parse(byte[] data) throws Exception
{	
	//CHECK IF WE MATCH BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data does not match block length");
	}
	
	int position = 0;
	
	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);	
	position += TIMESTAMP_LENGTH;
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ CREATOR
	byte[] creatorBytes = Arrays.copyOfRange(data, position, position + CREATOR_LENGTH);
	PublicKeyAccount creator = new PublicKeyAccount(creatorBytes);
	position += CREATOR_LENGTH;
	
	//READ POLL SIZE
	byte[] pollLengthBytes = Arrays.copyOfRange(data, position, position + POLL_SIZE_LENGTH);
	int pollLength = Ints.fromByteArray(pollLengthBytes);
	position += POLL_SIZE_LENGTH;
			
	if(pollLength < 1 || pollLength > 400)
	{
		throw new Exception("Invalid poll length");
	}
	
	//READ POLL
	byte[] pollBytes = Arrays.copyOfRange(data, position, position + pollLength);
	String poll = new String(pollBytes, StandardCharsets.UTF_8);
	position += pollLength;
	
	//READ OPTION
	byte[] optionBytes = Arrays.copyOfRange(data, position, position + OPTION_SIZE_LENGTH);
	int option = Ints.fromByteArray(optionBytes);
	position += OPTION_SIZE_LENGTH;
	
	//READ FEE
	byte[] feeBytes = Arrays.copyOfRange(data, position, position + FEE_LENGTH);
	BigDecimal fee = new BigDecimal(new BigInteger(feeBytes), 8);
	position += FEE_LENGTH;		
	
	//READ SIGNATURE
	byte[] signatureBytes = Arrays.copyOfRange(data, position, position + SIGNATURE_LENGTH);
	
	return new VoteOnPollTransaction(creator, poll, option, fee, timestamp, reference, signatureBytes);
}
 
Example 16
Source File: ByteArraySeekableFile.java    From xraft with MIT License 4 votes vote down vote up
@Override
public long readLong() throws IOException {
    byte[] buffer = new byte[8];
    read(buffer);
    return Longs.fromByteArray(buffer);
}
 
Example 17
Source File: Snow64.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
public static long fromSnow64(String snow64) {
    return (Longs.fromByteArray(Base64.getDecoder().decode(snow64.replace('-', '/'))));
}
 
Example 18
Source File: PublishPayloadRocksDBSerializer.java    From hivemq-community-edition with Apache License 2.0 4 votes vote down vote up
public static long deserializeKey(final @NotNull byte[] bytes) {
    return Longs.fromByteArray(bytes);
}
 
Example 19
Source File: Block.java    From Qora with MIT License 4 votes vote down vote up
public static Block parse(byte[] data) throws Exception
{
	//CHECK IF WE HAVE MINIMUM BLOCK LENGTH
	if(data.length < BASE_LENGTH)
	{
		throw new Exception("Data is less then minimum block length");
	}
	
	int position = 0;
	
	//READ VERSION
	byte[] versionBytes = Arrays.copyOfRange(data, position, position + VERSION_LENGTH);
	int version = Ints.fromByteArray(versionBytes);
	position += VERSION_LENGTH;

	//READ TIMESTAMP
	byte[] timestampBytes = Arrays.copyOfRange(data, position, position + TIMESTAMP_LENGTH);
	long timestamp = Longs.fromByteArray(timestampBytes);
	position += TIMESTAMP_LENGTH;		
	
	//READ REFERENCE
	byte[] reference = Arrays.copyOfRange(data, position, position + REFERENCE_LENGTH);
	position += REFERENCE_LENGTH;
	
	//READ GENERATING BALANCE
	byte[] generatingBalanceBytes = Arrays.copyOfRange(data, position, position + GENERATING_BALANCE_LENGTH);
	long generatingBalance = Longs.fromByteArray(generatingBalanceBytes);
	position += GENERATING_BALANCE_LENGTH;
	
	//READ GENERATOR
	byte[] generatorBytes = Arrays.copyOfRange(data, position, position + GENERATOR_LENGTH);
	PublicKeyAccount generator = new PublicKeyAccount(generatorBytes);
	position += GENERATOR_LENGTH;
	
	//READ TRANSACTION SIGNATURE
	byte[] transactionsSignature =  Arrays.copyOfRange(data, position, position + TRANSACTIONS_SIGNATURE_LENGTH);
	position += TRANSACTIONS_SIGNATURE_LENGTH;
			
	
	//READ GENERATOR SIGNATURE
	byte[] generatorSignature =  Arrays.copyOfRange(data, position, position + GENERATOR_SIGNATURE_LENGTH);
	position += GENERATOR_SIGNATURE_LENGTH;
	
	//CREATE BLOCK
	Block block = new Block(version, reference, timestamp, generatingBalance, generator, generatorSignature);
	
	//READ TRANSACTIONS COUNT
	byte[] transactionCountBytes = Arrays.copyOfRange(data, position, position + TRANSACTIONS_COUNT_LENGTH);
	int transactionCount = Ints.fromByteArray(transactionCountBytes);
	position += TRANSACTIONS_COUNT_LENGTH;
	
	//SET TRANSACTIONDATA
	byte[] rawTransactions = Arrays.copyOfRange(data, position, data.length);
	block.setTransactionData(transactionCount, rawTransactions);
	
	//SET TRANSACTIONS SIGNATURE
	block.setTransactionsSignature(transactionsSignature);
			
	return block;
}
 
Example 20
Source File: ArrowFileReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static long readLong(InputStream is) throws IOException {
  byte[] buffer = READ_BUFFER.get();
  IOUtils.readFully(is, buffer);

  return Longs.fromByteArray(buffer);
}