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

/**
 * Converts a given integer to byte, in little endian
 * @param i An integer to be converted to byte
 * @return A byte array stores byte representation of given integer
 *//* w ww  . j a  v a 2  s. c  om*/
public static byte[] intToByte(int i) {
    ByteBuffer b = ByteBuffer.allocate(4);
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.putInt(i);
    byte buf[] = b.array();
    return buf;
}

From source file:Main.java

/**
 * convert int to byte array//from w  w w .j  a v  a2s.c  o m
 * 
 * @param value
 *            integer value
 * @return byte array
 */
public static byte[] convertIntToByte(int value) {
    byte[] temp = new byte[4];
    byte[] result = ByteBuffer.allocate(4).putInt(value).array();
    temp[0] = result[3];
    temp[1] = result[2];
    temp[2] = result[1];
    temp[3] = result[0];
    return temp;
}

From source file:Main.java

/**
 * bytes tp chars/*from w w  w  . ja v a  2 s.  c  o m*/
 * 
 * @param bytes
 * @return
 */
public static char[] getChars(byte[] bytes) {
    Charset cs = Charset.forName("UTF-8");
    ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    bb.put(bytes);
    bb.flip();
    CharBuffer cb = cs.decode(bb);
    return cb.array();
}

From source file:Main.java

public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
    // Create clone with same capacity as original.
    final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity())
            : ByteBuffer.allocate(original.capacity());

    // Create a read-only copy of the original.
    // This allows reading from the original without modifying it.
    final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();

    // Flip and read from the original.
    readOnlyCopy.flip();//from   ww w .jav a 2s.  co m
    clone.put(readOnlyCopy);
    clone.position(original.position());
    clone.limit(original.limit());
    clone.order(original.order());
    return clone;
}

From source file:Main.java

/**
 * Converts bitmap to the byte array without compression
 * @param bitmap source bitmap//  www  .j  a  va 2  s.c  o  m
 * @return result byte array
 */
public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer.array();
}

From source file:Main.java

/**
 * Create a message from an array, first field of the message is the size of the array
 * @param array/*from   www .ja  va  2 s  .com*/
 * @return
 */
public static byte[] createMessageFromArray(ArrayList<byte[]> array) {

    byte[] buffer1 = new byte[4];
    System.arraycopy(ByteBuffer.allocate(4).putInt(array.size()).array(), 0, buffer1, 0, 4);

    byte[] buffer2 = compileMessages(array);

    return concatenateMessages(buffer1, buffer2);
}

From source file:Main.java

public static byte[] uuidToBytes(String uuidStr) {
    UUID uuid = stringToUuid(uuidStr);

    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putLong(uuid.getLeastSignificantBits());
    bb.putLong(uuid.getMostSignificantBits());
    return bb.array();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap b) {
    //calculate how many bytes our image consists of.
    int bytes = b.getByteCount();
    //or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
    //int bytes = b.getWidth()*b.getHeight()*4;

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

    byte[] array = buffer.array();

    return array;
}

From source file:Main.java

public static byte[] toArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(value);//ww  w.  ja va  2  s  .  com
    return buffer.array();
}

From source file:Main.java

public static byte[] createDigest(String passcode, long t1, double q1)
        throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(passcode.getBytes());// w w  w  . j ava2  s . co m
    ByteBuffer bb = ByteBuffer.allocate(16); //8 bytes for long and double each
    bb.putLong(t1);
    bb.putDouble(q1);
    md.update(bb);
    return md.digest();
}