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:Main.java

public static byte[] bitmapToArray(Bitmap b) {
    int bytes = b.getByteCount();

    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

    byte[] array = buffer.array(); //Get the underlying array containing the data.

    return array;
}

From source file:Main.java

public static byte[] getBytes(Bitmap bitmap) {
    int byteCount = bitmap.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(byteCount);
    bitmap.copyPixelsToBuffer(buffer);//w  w w.j  a  v  a2 s  .  c  o  m
    buffer.rewind();

    byte[] data = new byte[byteCount];
    buffer.get(data);
    return data;
}

From source file:Main.java

/**
 * Converts an int value into an array of 4 bytes.
 *
 * @param x The int.//from ww w .  ja  v a2  s  .  c om
 * @return The bytes.
 */
public static byte[] intToBytes(final int x) {
    final ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(x);
    return buffer.array();
}

From source file:Main.java

public static byte[] randomUUID() {
    UUID uuid = UUID.randomUUID();
    long hi = uuid.getMostSignificantBits();
    long lo = uuid.getLeastSignificantBits();
    return ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
}

From source file:Main.java

/**
 * Converts an array of 4 bytes into a int.
 *
 * @param bytes The bytes./*from w  ww  .  j  a  v  a2s .c om*/
 * @return The int.
 */
public static int bytesToInt(final byte[] bytes) {
    final ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.put(bytes, 0, 4);
    buffer.flip();
    return buffer.getInt();
}

From source file:Main.java

public static byte[] addHeader(byte[] data) {
    byte[] h264 = new byte[data.length + 4];
    for (int i = 0; i < data.length; i++) {
        h264[i + 4] = data[i];/*from  w  ww  .ja v  a2  s.c  o  m*/
    }
    byte[] lens = ByteBuffer.allocate(4).putInt(data.length).array();
    h264[0] = lens[0];
    h264[1] = lens[1];
    h264[2] = lens[2];
    h264[3] = lens[3];
    return h264;
}

From source file:Main.java

/**
 * Converts an array of 8 bytes into a long.
 *
 * @param bytes The bytes.// www .  j av  a 2  s.  c  om
 * @return The long.
 */
public static long bytesToLong(final byte[] bytes) {
    final ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.put(bytes, 0, 8);
    buffer.flip();
    return buffer.getLong();
}

From source file:Main.java

public static byte[] indexValueForObject(Object obj) {

    byte[] bytes = null;

    if (obj instanceof Double) {

        double d = (Double) obj;
        bytes = ByteBuffer.allocate(4).putDouble(d).array();

        bytes[0] ^= 0xff;/*  w w w . j  ava  2 s . com*/

        if (d < 0) {
            bytes[1] ^= 0xff;
            bytes[2] ^= 0xff;
            bytes[3] ^= 0xff;
        }
    }

    else if (obj instanceof Integer) {

        // flip sign bit
        int i = (Integer) obj;

        i ^= 0x80000000;

        bytes = ByteBuffer.allocate(4).putInt(i).array();
    }

    else if (obj instanceof String) {

        bytes = ((String) obj).getBytes();
    }

    return bytes;
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    //int numBytes = bitmap.getWidth() + bitmap.getHeight() * 4;
    int numBytes = bitmap.getByteCount();
    ByteBuffer byteBuffer = ByteBuffer.allocate(numBytes);
    bitmap.copyPixelsToBuffer(byteBuffer);
    return byteBuffer.array();
}

From source file:Main.java

public static byte[] getDHdataWithHash(byte[] hash, byte[] data) {
    int ost = (data.length + 20) % 16 > 0 ? 16 : 0;
    int size = ((20 + data.length) / 16) * 16 + ost;

    ByteBuffer buffer = ByteBuffer.allocate(size);
    buffer.put(hash);/*from  w ww .  ja v a2  s.c  o m*/
    buffer.put(data);

    byte[] rand = new byte[size - hash.length - data.length];
    new Random().nextBytes(rand);
    buffer.put(rand);
    return buffer.array();
}