Example usage for java.nio ByteBuffer getLong

List of usage examples for java.nio ByteBuffer getLong

Introduction

In this page you can find the example usage for java.nio ByteBuffer getLong.

Prototype

public abstract long getLong(int index);

Source Link

Document

Returns the long at the specified index.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asLongBuffer().put(99472342341142L);
    System.out.println(bb.getLong(0));

}

From source file:com.openteach.diamond.network.waverider.session.DefaultSession.java

public static void main(String[] args) {

    BlockingQueue<ByteBuffer> inputBuffer = new LinkedBlockingQueue<ByteBuffer>();
    /*for (int i = 0; i < 10; i++)
    {*//*from   w w w  .  ja  va  2  s .  c  om*/
    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.put(makePacket().marshall());
    byteBuffer.put(makePacket().marshall());
    byteBuffer.flip();
    byte[] b = new byte[8];
    ByteBuffer halfBuf0 = ByteBuffer.allocate(8);
    byteBuffer.get(b);
    halfBuf0.put(b);
    halfBuf0.flip();
    inputBuffer.add(halfBuf0);
    inputBuffer.add(byteBuffer);
    /*}*/

    int size = 0;
    int oldSize = size;
    long length = Packet.getHeaderSize();
    ByteBuffer buffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE);
    ByteBuffer currentBuffer = null;

    while (size < length) {
        currentBuffer = inputBuffer.peek();
        oldSize = size;
        int position = currentBuffer.position();
        size += currentBuffer.remaining();
        buffer.put(currentBuffer);
        if (size >= Packet.getHeaderSize()) {
            length = buffer.getLong(Packet.getLengthPosition());
        }

        if (size <= length) {
            inputBuffer.remove();
        } else {
            currentBuffer.position(position);
            buffer.position(buffer.position() - currentBuffer.remaining());
            byte[] buf = new byte[(int) (length - oldSize)];
            currentBuffer.get(buf);
            buffer.put(buf);
        }
    }

    // buffer.position(0);
    buffer.flip();
    Packet packet = Packet.unmarshall(buffer);

    Command command = CommandFactory.createCommand(packet.getType(), packet.getPayLoad());

    String str = new String(command.getPayLoad().array());

    System.out.println(str);

}

From source file:org.i3xx.step.mongo.core.util.IdGen.java

/**
 * Gets a 128 bit IdRep from an URL save representation
 * /*from  ww w  .  j  a  v  a  2 s .co  m*/
 * @param urlSave
 * @return The IdRep object
 */
public static final IdRep fromURLSaveString(String urlSave) {

    byte[] bytes = Base64.decodeBase64(urlSave);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    UUID uuid = new UUID(buffer.getLong(0), buffer.getLong(8));

    return new JavaIdRep(uuid);
}

From source file:org.slc.sli.common.util.uuid.DeterministicUUIDGeneratorStrategy.java

protected static UUID generateUuid(byte[] data) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(data);

    long msb = byteBuffer.getLong(0);
    long lsb = byteBuffer.getLong(8);

    UUID uuid = new UUID(msb, lsb);

    return uuid;//from w ww  .  j av  a 2 s  .  co m
}

From source file:com.inmobi.messaging.util.AuditUtil.java

public static long getTimestamp(byte[] msg) {
    if (isValidHeaders(msg)) {
        ByteBuffer buffer = ByteBuffer.wrap(msg);
        return buffer.getLong(POSITION_OF_TIMESTAMP);
    } else// w  w w  .  j a v  a 2s . c o  m
        return -1;
}

From source file:Main.java

/**
 * Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
 * but the returned UUID is always in 128-bit format.
 * Note UUID is little endian in Bluetooth.
 *
 * @param uuidBytes Byte representation of uuid.
 * @return {@link ParcelUuid} parsed from bytes.
 * @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
 *//*from w w  w  .  j  a v  a  2 s.  c  om*/
public static ParcelUuid parseUuidFrom(byte[] uuidBytes) {
    if (uuidBytes == null) {
        throw new IllegalArgumentException("uuidBytes cannot be null");
    }
    int length = uuidBytes.length;
    if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT && length != UUID_BYTES_128_BIT) {
        throw new IllegalArgumentException("uuidBytes length invalid - " + length);
    }

    // Construct a 128 bit UUID.
    if (length == UUID_BYTES_128_BIT) {
        ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
        long msb = buf.getLong(8);
        long lsb = buf.getLong(0);
        return new ParcelUuid(new UUID(msb, lsb));
    }

    // For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
    // 128_bit_value = uuid * 2^96 + BASE_UUID
    long shortUuid;
    if (length == UUID_BYTES_16_BIT) {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
    } else {
        shortUuid = uuidBytes[0] & 0xFF;
        shortUuid += (uuidBytes[1] & 0xFF) << 8;
        shortUuid += (uuidBytes[2] & 0xFF) << 16;
        shortUuid += (uuidBytes[3] & 0xFF) << 24;
    }
    long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
    long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
    return new ParcelUuid(new UUID(msb, lsb));
}

From source file:org.bimserver.utils.BinUtils.java

public static long byteArrayToLong(byte[] value, int index) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(value);
    return byteBuffer.getLong(index);
}

From source file:net.geertvos.theater.core.util.UUIDGen.java

/** creates a type 1 uuid from raw bytes. */
public static UUID getUUID(ByteBuffer raw) {
    return new UUID(raw.getLong(raw.position()), raw.getLong(raw.position() + 8));
}

From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java

static byte[] sivEncrypt(byte[] aesKey, byte[] macKey, byte[] plaintext, byte[]... additionalData)
        throws InvalidKeyException {
    if (aesKey.length != 16 && aesKey.length != 24 && aesKey.length != 32) {
        throw new InvalidKeyException("Invalid aesKey length " + aesKey.length);
    }/*  ww  w.j  av  a 2 s .co m*/

    final byte[] iv = s2v(macKey, plaintext, additionalData);

    final int numBlocks = (plaintext.length + 15) / 16;

    // clear out the 31st and 63rd (rightmost) bit:
    final byte[] ctr = Arrays.copyOf(iv, 16);
    ctr[8] = (byte) (ctr[8] & 0x7F);
    ctr[12] = (byte) (ctr[12] & 0x7F);
    final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr);
    final long initialCtrVal = ctrBuf.getLong(8);

    final byte[] x = new byte[numBlocks * 16];
    final BlockCipher aes = new AESFastEngine();
    aes.init(true, new KeyParameter(aesKey));
    for (int i = 0; i < numBlocks; i++) {
        final long ctrVal = initialCtrVal + i;
        ctrBuf.putLong(8, ctrVal);
        aes.processBlock(ctrBuf.array(), 0, x, i * 16);
        aes.reset();
    }

    final byte[] ciphertext = xor(plaintext, x);

    return ArrayUtils.addAll(iv, ciphertext);
}

From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java

static byte[] sivDecrypt(byte[] aesKey, byte[] macKey, byte[] ciphertext, byte[]... additionalData)
        throws DecryptFailedException, InvalidKeyException {
    if (aesKey.length != 16 && aesKey.length != 24 && aesKey.length != 32) {
        throw new InvalidKeyException("Invalid aesKey length " + aesKey.length);
    }/*from w w w .j  a v a  2 s  . c  o m*/

    final byte[] iv = Arrays.copyOf(ciphertext, 16);

    final byte[] actualCiphertext = Arrays.copyOfRange(ciphertext, 16, ciphertext.length);
    final int numBlocks = (actualCiphertext.length + 15) / 16;

    // clear out the 31st and 63rd (rightmost) bit:
    final byte[] ctr = Arrays.copyOf(iv, 16);
    ctr[8] = (byte) (ctr[8] & 0x7F);
    ctr[12] = (byte) (ctr[12] & 0x7F);
    final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr);
    final long initialCtrVal = ctrBuf.getLong(8);

    final byte[] x = new byte[numBlocks * 16];
    final BlockCipher aes = new AESFastEngine();
    aes.init(true, new KeyParameter(aesKey));
    for (int i = 0; i < numBlocks; i++) {
        final long ctrVal = initialCtrVal + i;
        ctrBuf.putLong(8, ctrVal);
        aes.processBlock(ctrBuf.array(), 0, x, i * 16);
        aes.reset();
    }

    final byte[] plaintext = xor(actualCiphertext, x);

    final byte[] control = s2v(macKey, plaintext, additionalData);

    if (MessageDigest.isEqual(control, iv)) {
        return plaintext;
    } else {
        throw new DecryptFailedException("Authentication failed");
    }
}