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

The following examples show how to use com.google.common.primitives.Longs#fromBytes() . 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
Source Project: hive-dwrf   File: Slice.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Fill the slice with the specified value;
 */
public void fill(byte value)
{
    int offset = 0;
    int length = size;
    long longValue = Longs.fromBytes(value, value, value, value, value, value, value, value);
    while (length >= SizeOf.SIZE_OF_LONG) {
        unsafe.putLong(base, address + offset, longValue);
        offset += SizeOf.SIZE_OF_LONG;
        length -= SizeOf.SIZE_OF_LONG;
    }

    while (length > 0) {
        unsafe.putByte(base, address + offset, value);
        offset++;
        length--;
    }
}
 
Example 2
@Override
public void readExternal(final ObjectInput arg0) throws IOException, ClassNotFoundException {
	byte[] bytes = extreadbuffer_.get();
	arg0.read(bytes);
	this.db = Longs.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]);
	this.x = Longs.fromBytes(bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
	this.y = Longs.fromBytes(bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23]);
}
 
Example 3
@Override
public long getLongLittleEndian(byte[] source, int offset) {
  return Longs.fromBytes(
      source[offset + 7],
      source[offset + 6],
      source[offset + 5],
      source[offset + 4],
      source[offset + 3],
      source[offset + 2],
      source[offset + 1],
      source[offset]);
}
 
Example 4
private long traceIdHigh() {
  return Longs.fromBytes(
      traceIdBuffer[0],
      traceIdBuffer[1],
      traceIdBuffer[2],
      traceIdBuffer[3],
      traceIdBuffer[4],
      traceIdBuffer[5],
      traceIdBuffer[6],
      traceIdBuffer[7]);
}
 
Example 5
private long traceIdLow() {
  return Longs.fromBytes(
      traceIdBuffer[8],
      traceIdBuffer[9],
      traceIdBuffer[10],
      traceIdBuffer[11],
      traceIdBuffer[12],
      traceIdBuffer[13],
      traceIdBuffer[14],
      traceIdBuffer[15]);
}
 
Example 6
/**
 * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using
 * little-endian byte order.
 *
 * @return the next eight bytes of the input stream, interpreted as a {@code long} in
 *     little-endian byte order
 * @throws IOException if an I/O error occurs
 */

@CanIgnoreReturnValue // to skip some bytes
@Override
public long readLong() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();
  byte b5 = readAndCheckByte();
  byte b6 = readAndCheckByte();
  byte b7 = readAndCheckByte();
  byte b8 = readAndCheckByte();
  return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1);
}
 
Example 7
public static String getUnidFromBytes(final byte[] bytes) {
	if (bytes.length >= 16) {
		long first = Longs.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]);
		long last = Longs.fromBytes(bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
		return getUnidFromLongs(first, last);
	} else {
		//TODO NTF Something here...
		throw new IllegalArgumentException("Cannot convert a byte array of length " + bytes.length + " to a unid");
	}
}
 
Example 8
public LocalNoteCoordinate(final byte[] bytes, final LocalNoteList parentList) {
	parent_ = parentList;
	if (bytes.length >= BUFFER_SIZE) {
		this.x = Longs.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]);
		this.y = Longs.fromBytes(bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
	} else {
		throw new IllegalArgumentException("Can't construct new LocalNoteCoordinate from byte length of " + bytes.length);
	}
}
 
Example 9
private boolean isProbablyDuplicate(final HashCode eventDigest) {
    // Our hashing algorithm produces 8 bytes:
    //  0: slot[0]
    //  1: slot[1]
    //  2: slot[2]
    //  3: slot[3]
    //  4:
    //  5:
    //  6:
    //  7:
    //  8: signature[0]
    //  9:  ..
    // 10:  ..
    // 11:  ..
    // 12:  ..
    // 13:  ..
    // 14:  ..
    // 15: signature[7]
    final byte[] hashBytes = eventDigest.asBytes();

    // We use the low int for the slot.
    final int slotSelector = Ints.fromBytes(hashBytes[0],
                                            hashBytes[1],
                                            hashBytes[2],
                                            hashBytes[3]);
    // We use the high long for the signature.
    final long signature = Longs.fromBytes(hashBytes[8],
                                           hashBytes[9],
                                           hashBytes[10],
                                           hashBytes[11],
                                           hashBytes[12],
                                           hashBytes[13],
                                           hashBytes[14],
                                           hashBytes[15]);

    final int slot = (slotSelector & Integer.MAX_VALUE) % memory.length;
    final boolean result = memory[slot] == signature;
    memory[slot] = signature;
    return result;
}
 
Example 10
@Override
public long getLongLittleEndian(byte[] source, int offset) {
  return Longs.fromBytes(
    source[offset + 7],
    source[offset + 6],
    source[offset + 5],
    source[offset + 4],
    source[offset + 3],
    source[offset + 2],
    source[offset + 1],
    source[offset]);
}
 
Example 11
/**
 * Reads a {@code long} as specified by {@link DataInputStream#readLong()}, except using
 * little-endian byte order.
 *
 * @return the next eight bytes of the input stream, interpreted as a {@code long} in
 *     little-endian byte order
 * @throws IOException if an I/O error occurs
 */

@CanIgnoreReturnValue // to skip some bytes
@Override
public long readLong() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();
  byte b5 = readAndCheckByte();
  byte b6 = readAndCheckByte();
  byte b7 = readAndCheckByte();
  byte b8 = readAndCheckByte();
  return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1);
}
 
Example 12
Source Project: buck   File: ObjectFileScrubbers.java    License: Apache License 2.0 5 votes vote down vote up
public static long getLittleEndianLong(ByteBuffer buffer) {
  byte b1 = buffer.get();
  byte b2 = buffer.get();
  byte b3 = buffer.get();
  byte b4 = buffer.get();
  byte b5 = buffer.get();
  byte b6 = buffer.get();
  byte b7 = buffer.get();
  byte b8 = buffer.get();
  return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1);
}
 
Example 13
@Override
public long getLongLittleEndian(byte[] source, int offset) {
  return Longs.fromBytes(
      source[offset + 7],
      source[offset + 6],
      source[offset + 5],
      source[offset + 4],
      source[offset + 3],
      source[offset + 2],
      source[offset + 1],
      source[offset]);
}
 
Example 14
@Override
public long readLong() throws IOException {
  byte b1 = readAndCheckByte();
  byte b2 = readAndCheckByte();
  byte b3 = readAndCheckByte();
  byte b4 = readAndCheckByte();
  byte b5 = readAndCheckByte();
  byte b6 = readAndCheckByte();
  byte b7 = readAndCheckByte();
  byte b8 = readAndCheckByte();

  return Longs.fromBytes(b8, b7, b6, b5, b4, b3, b2, b1);
}
 
Example 15
Source Project: SonarPet   File: UUIDUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static UUID fromBytes(byte[] bytes) {
    Preconditions.checkNotNull(bytes, "Null bytes");
    Preconditions.checkArgument(bytes.length == 16, "Invalid length: %s", bytes.length);
    long msb = Longs.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]);
    long lsb = Longs.fromBytes(bytes[8], bytes[9], bytes[10], bytes[11],
            bytes[12], bytes[13], bytes[14], bytes[15]);
    return new UUID(msb, lsb);
}
 
Example 16
Source Project: CuckooFilter4J   File: IndexTagCalc.java    License: Apache License 2.0 4 votes vote down vote up
private long longFromHighBytes(byte[] bytes) {
	return Longs.fromBytes(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]);
}
 
Example 17
Source Project: CuckooFilter4J   File: IndexTagCalc.java    License: Apache License 2.0 4 votes vote down vote up
private long longFromLowBytes(byte[] bytes) {
	return Longs.fromBytes(bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
}
 
Example 18
@Override
public long getLongLittleEndian(byte[] source, int offset) {
  return Longs.fromBytes(source[offset + 7], source[offset + 6], source[offset + 5], source[offset + 4], source[offset + 3], source[offset + 2], source[offset + 1], source[offset]);
}
 
Example 19
Source Project: attic-apex-malhar   File: HDFSStorage.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @param b
 * @param startIndex
 * @return
 */
long byteArrayToLong(byte[] b, int startIndex)
{
  final byte b1 = 0;
  return Longs.fromBytes(b1, b1, b1, b1, b[3 + startIndex], b[2 + startIndex], b[1 + startIndex], b[startIndex]);
}
 
Example 20
Source Project: hivemq-community-edition   File: Bytes.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Read a serialized long value for a given byte array
 *
 * @param buffer        A byte array that contains the serialized long value to read.
 * @param startPosition The position of the first bit of the long value in the buffer
 * @return The next 8 bits from startPosition as long
 * @throws IllegalArgumentException if the buffer is to small to read a long value from the start position
 */
public static long readLong(final byte[] buffer, final int startPosition) {
    if (startPosition + Long.BYTES > buffer.length) {
        throw new IllegalArgumentException("The provided array[" + buffer.length + "] is to small to read 8 bytes from start position " + startPosition);
    }
    return Longs.fromBytes(buffer[startPosition], buffer[startPosition + 1], buffer[startPosition + 2], buffer[startPosition + 3],
            buffer[startPosition + 4], buffer[startPosition + 5], buffer[startPosition + 6], buffer[startPosition + 7]);
}