Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:mtmo.test.mediadrm.Utils.java

public static String accountIdToMarlinFormat(final String accountId) {
    ByteBuffer accountIdBuf = ByteBuffer.allocate(BYTES_OF_ACCOUNT_ID);
    try {// www  .j  a  v  a2 s  .co m
        accountIdBuf.putLong(Long.valueOf(accountId));
    } catch (Exception e) {
        return null;
    }
    accountIdBuf.order(ByteOrder.LITTLE_ENDIAN);
    return String.format(Locale.US, "%016x", accountIdBuf.getLong(0));
}

From source file:MainClass.java

private static void createFile() throws Exception {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/primes.bin");
    FileOutputStream outputFile = new FileOutputStream(aFile);
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());

        file.write(buf);/*from ww w  .ja v  a  2  s . c  o  m*/
        primesWritten += longBuf.position();
        longBuf.clear();
        buf.clear();
    }
    System.out.println("File written is " + file.size() + "bytes.");
    outputFile.close();
}

From source file:Main.java

/**
 * @param columnarKeyBlockData//w  w w  .j  a  va2  s . c  o m
 * @param columnarKeyStoreMetadata
 * @return
 * @author s71955 The high cardinality dimensions rows will be send in byte
 * array with its data length appended in the
 * ColumnarKeyStoreDataHolder byte array since high cardinality dim
 * data will not be part of MDKey/Surrogate keys. In this method the
 * byte array will be scanned and the length which is stored in
 * short will be removed.
 */
public static List<byte[]> readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData) {
    List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50);
    ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length);
    noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData);
    noDictionaryValKeyStoreDataHolder.flip();
    while (noDictionaryValKeyStoreDataHolder.hasRemaining()) {
        short dataLength = noDictionaryValKeyStoreDataHolder.getShort();
        byte[] noDictionaryValKeyData = new byte[dataLength];
        noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData);
        columnarKeyBlockDataList.add(noDictionaryValKeyData);
    }
    return columnarKeyBlockDataList;

}

From source file:com.srotya.tau.nucleus.Utils.java

public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.putLong(x);/*from www  .  j a v a2 s  .c om*/
    return buffer.array();
}

From source file:Main.java

public static void transfer(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    while (in.read(buffer) != -1) {
        buffer.flip();//from ww  w . ja v a  2  s .c  o  m
        while (buffer.hasRemaining()) {
            out.write(buffer);
        }
        buffer.clear();
    }
}

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

public static void attachHeaders(Message m, Long timestamp) {
    byte[] b = m.getData().array();
    int messageSize = b.length;
    int totalSize = messageSize + HEADER_LENGTH;
    ByteBuffer buffer = ByteBuffer.allocate(totalSize);

    // writing version
    buffer.put((byte) currentVersion);
    // writing magic bytes
    buffer.put(magicBytes);/*from  ww w .  ja va  2 s. c  om*/
    // writing timestamp
    long time = timestamp;
    buffer.putLong(time);

    // writing message size
    buffer.putInt(messageSize);
    // writing message
    buffer.put(b);
    buffer.rewind();
    m.set(buffer);
    // return new Message(buffer);

}

From source file:Main.java

public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength)
        throws IOException {
    // End of central directory record (EOCD)
    // Offset    Bytes     Description[23]
    // 0           4       End of central directory signature = 0x06054b50
    // 4           2       Number of this disk
    // 6           2       Disk where central directory starts
    // 8           2       Number of central directory records on this disk
    // 10          2       Total number of central directory records
    // 12          4       Size of central directory (bytes)
    // 16          4       Offset of start of central directory, relative to start of archive
    // 20          2       Comment length (n)
    // 22          n       Comment
    // For a zip with no archive comment, the
    // end-of-central-directory record will be 22 bytes long, so
    // we expect to find the EOCD marker 22 bytes from the end.

    final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);
    zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);
    fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)
    fileChannel.read(zipCentralDirectoryStart);
    final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);
    return centralDirStartOffset;
}

From source file:Main.java

public static byte[] getAESiv(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;/*from ww w . ja  v  a2  s.  c  o  m*/
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer tempD = ByteBuffer.allocate(msgKey.length + 32);
    tempD.put(msgKey);
    tempD.put(authKey, 96 + x, 32);
    byte[] d = getSHA1hash(tempD.array());

    ByteBuffer iv = ByteBuffer.allocate(12 + 8 + 4 + 8);
    iv.put(a, 8, 12);
    iv.put(b, 0, 8);
    iv.put(c, 16, 4);
    iv.put(d, 0, 8);

    return iv.array();
}

From source file:MainClass.java

public void run() {

    ByteBuffer sizeb = ByteBuffer.allocate(4);
    try {/*  w  w w  .ja v a 2  s .  c  om*/
        while (sizeb.hasRemaining())
            in.read(sizeb);
        sizeb.flip();
        int howMany = sizeb.getInt();
        sizeb.clear();

        for (int i = 0; i < howMany; i++) {
            while (sizeb.hasRemaining())
                in.read(sizeb);
            sizeb.flip();
            int length = sizeb.getInt();
            sizeb.clear();

            ByteBuffer data = ByteBuffer.allocate(length);
            while (data.hasRemaining())
                in.read(data);

            BigInteger result = new BigInteger(data.array());
            System.out.println(result);
        }
    } catch (IOException ex) {
        System.err.println(ex);
    } finally {
        try {
            in.close();
        } catch (Exception ex) {
            // We tried
        }
    }
}

From source file:Main.java

public static void readData(SeekableByteChannel seekableChannel, Charset cs) throws IOException {
    ByteBuffer byteBuffer = ByteBuffer.allocate(128);
    String encoding = System.getProperty("file.encoding");
    while (seekableChannel.read(byteBuffer) > 0) {
        byteBuffer.rewind();/*from  w ww.ja v  a  2s. c o m*/
        CharBuffer charBuffer = cs.decode(byteBuffer);
        System.out.print(charBuffer);
        byteBuffer.flip();
    }
}