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:io.datenwelt.cargo.rest.utils.Strings.java

public static String token() {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 100);
    for (int i = 0; i < 100; i++) {
        buffer.putLong(random.nextLong());
    }/*  ww  w .j av a  2  s .  c  o m*/
    return DigestUtils.md5Hex(buffer.array());
}

From source file:Main.java

public static IntBuffer createIntBufferOnHeap(final int size) {
    final IntBuffer buf = ByteBuffer.allocate(SIZEOF_FLOAT * size).order(ByteOrder.nativeOrder()).asIntBuffer();
    buf.clear();//w w  w  .j  a  va  2 s.  com
    return buf;
}

From source file:Main.java

/**
 * Convert the given long to an 8-byte array
 * /*www. j a  v  a  2 s .  c o m*/
 * @param l
 *            A 'long'
 * 
 * @return An 8-byte array in big endian (MSB) ordering
 */
public static byte[] convertLongToByteArrayBigEndian(long l) {
    return ByteBuffer.allocate(Long.SIZE / EIGHT).putLong(l).array();
}

From source file:Main.java

public static byte intToByte(final int value) {
    return ByteBuffer.allocate(4).putInt(value).array()[3];
}

From source file:Main.java

public static FloatBuffer createFloatBufferOnHeap(final int size) {
    final FloatBuffer buf = ByteBuffer.allocate(SIZEOF_FLOAT * size).order(ByteOrder.nativeOrder())
            .asFloatBuffer();/*  w  ww .ja v a2  s . c o  m*/
    buf.clear();
    return buf;
}

From source file:Main.java

public static DoubleBuffer createDoubleBufferOnHeap(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocate(SIZEOF_DOUBLE * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();/*from  www  .  java 2 s .  c  o m*/
    buf.clear();
    return buf;
}

From source file:Main.java

public static ByteBuffer copy(ByteBuffer buffer) {

    final int length = buffer.limit() - buffer.position();
    final ByteOrder o = buffer.order();
    final ByteBuffer r = ByteBuffer.allocate(length);
    r.order(o);//from   w w w  . java2 s . co m

    r.put(buffer);
    r.clear(); // Reset position and limit after the put()

    return r;

}

From source file:Main.java

public static byte[] int2bytes(int a) {
    int arraySize = Integer.SIZE / Byte.SIZE;
    ByteBuffer buffer = ByteBuffer.allocate(arraySize);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.putInt(a).array();
}

From source file:Main.java

static byte[] intToByteArray(int i) {
    return ByteBuffer.allocate(4).putInt(i).array();
}

From source file:Main.java

public static byte[] padRight(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }/*from w  w w  . jav a2s .  c  o m*/
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);
    ByteBuffer buffer = ByteBuffer.allocate(len);

    buffer.put(tgt);
    buffer.put(paddings);

    return buffer.array();
}