Example usage for java.nio ByteBuffer putLong

List of usage examples for java.nio ByteBuffer putLong

Introduction

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

Prototype

public abstract ByteBuffer putLong(int index, long value);

Source Link

Document

Writes the given long to the specified index of this buffer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putLong(2, 123);

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:Main.java

public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(0, x);
    return buffer.array();
}

From source file:Main.java

public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / 8);
    buffer.putLong(0, x);
    return buffer.array();
}

From source file:org.voltdb.ClientResponseImpl.java

/**
 * Set the client handle without deserializing it first
 * @param b/* ww w .  j  a  v a 2  s .  co  m*/
 * @param handle
 */
public static void setClientHandle(ByteBuffer b, long handle) {
    b.putLong(13, handle); // 1 + 4 + 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);
    }/*from www.j  a va  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 av  a2 s  .  co 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");
    }
}

From source file:com.linkedin.pinot.core.indexsegment.utils.MmapMemoryManagerTest.java

@Test
public void testSmallBlocksForSameColumn() throws Exception {
    final String segmentName = "someSegment";
    PinotDataBufferMemoryManager memoryManager = new MmapMemoryManager(_tmpDir, segmentName);
    final long s1 = 500;
    final long s2 = 1000;
    final String col1 = "col1";
    PinotDataBuffer buf1 = memoryManager.allocate(s1, col1);

    PinotDataBuffer buf2 = memoryManager.allocate(s2, col1);

    ByteBuffer b1 = buf1.toDirectByteBuffer(0, (int) s1);
    b1.putLong(0, s1);

    ByteBuffer b2 = buf2.toDirectByteBuffer(0, (int) s2);
    b2.putLong(0, s2);//from   w w w.j  a  va2s .com

    Assert.assertEquals(b1.getLong(0), s1);
    Assert.assertEquals(b2.getLong(0), s2);

    File dir = new File(_tmpDir);
    Assert.assertEquals(dir.listFiles().length, 1);

    buf1.close();
    buf2.close();

    memoryManager.close();

    List<Pair<MmapUtils.AllocationContext, Integer>> allocationContexts = MmapUtils.getAllocationsAndSizes();
    Assert.assertEquals(allocationContexts.size(), 0);

    Assert.assertEquals(dir.listFiles().length, 0);
}

From source file:com.linkedin.pinot.core.indexsegment.utils.MmapMemoryManagerTest.java

@Test
public void testLargeBlocks() throws Exception {
    final String segmentName = "someSegment";
    PinotDataBufferMemoryManager memoryManager = new MmapMemoryManager(_tmpDir, segmentName);
    final long s1 = 2 * MmapMemoryManager.getDefaultFileLength();
    final long s2 = 1000;
    final String col1 = "col1";
    final String col2 = "col2";
    final byte value = 34;
    PinotDataBuffer buf1 = memoryManager.allocate(s1, col1);

    // Verify that we can write to and read from the buffer
    ByteBuffer b1 = buf1.toDirectByteBuffer(0, (int) s1);
    b1.putLong(0, s1);
    Assert.assertEquals(b1.getLong(0), s1);
    b1.put((int) s1 - 1, value);
    Assert.assertEquals(b1.get((int) s1 - 1), value);

    PinotDataBuffer buf2 = memoryManager.allocate(s2, col2);
    ByteBuffer b2 = buf2.toDirectByteBuffer(0, (int) s2);
    b2.putLong(0, s2);//from w w  w  .j  a  va2s .  c o  m
    Assert.assertEquals(b2.getLong(0), s2);

    File dir = new File(_tmpDir);
    File[] files = dir.listFiles();
    Assert.assertEquals(files.length, 2);

    Arrays.sort(files, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    String fileName = files[0].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    fileName = files[1].getName();
    Assert.assertTrue(fileName.contains(segmentName));

    buf1.close();
    buf2.close();

    memoryManager.close();

    List<Pair<MmapUtils.AllocationContext, Integer>> allocationContexts = MmapUtils.getAllocationsAndSizes();
    Assert.assertEquals(allocationContexts.size(), 0);
    Assert.assertEquals(new File(_tmpDir).listFiles().length, 0);
}

From source file:de.micromata.genome.logging.spi.ifiles.IndexHeader.java

public void writeFileHeader(OutputStream os, File indexFile, IndexDirectory indexDirectory) throws IOException {
    indexDirectoryIdx = indexDirectory.createNewLogIdxFile(indexFile);
    ByteBuffer lbb = ByteBuffer.wrap(new byte[Long.BYTES]);
    ByteBuffer ibb = ByteBuffer.wrap(new byte[Integer.BYTES]);
    os.write(INDEX_FILE_TYPE);/*from   www.  ja v  a2 s.  c  om*/
    os.write(INDEX_FILE_VERSION);
    lbb.putLong(0, System.currentTimeMillis());
    os.write(lbb.array());
    ibb.putInt(0, indexDirectoryIdx);
    os.write(ibb.array());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    for (Pair<String, Integer> headerp : headerOrder) {
        String hn = StringUtils.rightPad(headerp.getFirst(), HEADER_NAME_LENGTH);
        bout.write(hn.getBytes());
        ibb.putInt(0, headerp.getSecond());
        bout.write(ibb.array());
    }
    byte[] headerar = bout.toByteArray();
    int idxOffset = FILE_TYPE_LENGTH + FILE_VERSION_LENGTH + Long.BYTES /* timestamp */
            + Integer.BYTES /** indexDirectory */
            + Integer.BYTES /* indexOfset */
            + headerar.length;
    ibb.putInt(0, idxOffset);
    os.write(ibb.array());
    os.write(headerar);
    os.flush();
}

From source file:org.saadahmed.snowcrystal.SnowCrystal.java

protected SnowCrystal(long timestamp, short sequence, byte[] nodeId) {
    if (sequence > 4095) {
        throw new IllegalArgumentException(
                "Sequence cannot be greater than 2^12 - 1 = 4095. Sequence found: " + sequence);
    }/*from   w ww  .jav a 2 s. c o  m*/

    if (nodeId == null) {
        throw new NullPointerException("SnowCrystal Node id is null");
    }
    if (nodeId.length != NODE_LENGTH) {
        throw new IllegalArgumentException(
                "Invalid SnowCrystal Node size. Expected: " + NODE_LENGTH + ", Found: " + nodeId.length);
    }

    long ts = timestamp << 4;
    byte[] tsBytes = ByteBuffer.allocate(8).putLong(ts).array();
    byte[] seqBytes = ByteBuffer.allocate(2).putShort(sequence).array();
    byte commonByte = (byte) (tsBytes[7] | seqBytes[0]);

    ByteBuffer buffer = ByteBuffer.allocate(SNOWCRYSTAL_LENGTH);
    buffer.putLong(TIMESTAMP_OFFSET, ts);
    buffer.put(SEQUENCE_0_OFFSET, commonByte);
    buffer.put(SEQUENCE_1_OFFSET, seqBytes[1]);

    this.binary = buffer.array();
    System.arraycopy(nodeId, 0, this.binary, NODE_OFFSET, NODE_LENGTH);
}