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[] bitmapToByteArray(Bitmap bitmap) {
    int bytes = bitmap.getByteCount();
    ByteBuffer buf = ByteBuffer.allocate(bytes);
    bitmap.copyPixelsToBuffer(buf);/*from ww  w .  ja v  a  2s  . c o  m*/
    byte[] byteArray = buf.array();
    return byteArray;
}

From source file:Main.java

private static short stream2Short(byte[] stream, int offset) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(stream[offset]);/*from w w  w .  jav  a2 s.c  om*/
    buffer.put(stream[offset + 1]);
    return buffer.getShort(0);
}

From source file:Main.java

public final static ByteBuffer toNALFileFormat(final ByteBuffer buffer) {
    ByteBuffer result = ByteBuffer.allocate(buffer.remaining());
    result.put(buffer);/*from  www  .j a  v  a 2  s .c o  m*/
    result.flip();
    int length = 0;
    int position = -1;
    int remaining = result.remaining() - 3;
    for (int i = 0; i < remaining; ++i) {
        if (result.get(i) == 0x00 && result.get(i + 1) == 0x00 && result.get(i + 2) == 0x00
                && result.get(i + 3) == 0x01) {
            if (0 <= position) {
                result.putInt(position, length - 3);
            }
            position = i;
            length = 0;
        } else {
            ++length;
        }
    }
    result.putInt(position, length);
    return result;
}

From source file:Main.java

public static char[] getChars(byte[] buffer, int offset, int length) {

    ByteBuffer bb = ByteBuffer.allocate(length);
    bb.put(buffer, offset, length);//ww w . j av a  2s.c  om
    bb.flip();

    return Charset.defaultCharset().decode(bb).array();
}

From source file:Main.java

public static IntBuffer newIntBuffer(int numInts) {
    ByteBuffer buffer = ByteBuffer.allocate(numInts * 4);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asIntBuffer();
}

From source file:Main.java

public static LongBuffer newLongBuffer(int numLongs) {
    ByteBuffer buffer = ByteBuffer.allocate(numLongs * 8);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asLongBuffer();
}

From source file:Main.java

public static byte[] getCharacteristicWriteByteArray(int data) {
    // Designed by our hardware, characteristic
    short inputShort = ((short) data);
    byte[] inputData = ByteBuffer.allocate(6).putShort(inputShort).array();
    return inputData;
}

From source file:Main.java

public static synchronized byte[] int16tobyte(int a) {
    byte[] inttemp = new byte[2];
    inttemp[1] = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(a).get(1);
    inttemp[0] = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(a).get(0);
    return inttemp;
}

From source file:Main.java

public static String readTxtFromFile(File file) throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
    FileInputStream in = new FileInputStream(file);
    in.getChannel().read(buffer);//from   w ww  .  java  2 s  .  c  o m
    in.close();
    return new String(buffer.array());
}

From source file:Main.java

public static String convertBitmapToBase64(Bitmap bitmap) {
    final int size = bitmap.getByteCount();

    ByteBuffer dst = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(dst);//  w w w .  ja v  a2  s. c  o m
    return Base64.encodeToString(dst.array(), Base64.DEFAULT);
}