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 int bytesToInt(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    reverseArray(bytes);/*  www  .  j  a  v a  2  s.co m*/
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();// need flip
    return buffer.getInt();
}

From source file:Main.java

private static byte[] intToByteArray(int data) {
    return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(data).array();
}

From source file:Main.java

public static long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    reverseArray(bytes);/*from w w  w  .j  a v a2  s .c  om*/
    buffer.put(bytes, 0, bytes.length);
    buffer.flip();// need flip
    return buffer.getLong();
}

From source file:Main.java

private static byte[] longToBytes(long paramLong) {
    ByteBuffer localByteBuffer = ByteBuffer.allocate(8);
    localByteBuffer.putLong(paramLong);//from   w ww.  ja  v a2s.c  o m
    return localByteBuffer.array();
}

From source file:Main.java

public static byte[] sumByte(byte[] bytes1, byte[] bytes2) {
    ByteBuffer byteBuffer = ByteBuffer.allocate(bytes1.length + bytes2.length);
    byteBuffer.put(bytes1);//ww  w  . ja v  a2s.co m
    byteBuffer.put(bytes2);
    return byteBuffer.array();
}

From source file:Main.java

public static byte[] my_int_to_bb_le(int myInteger) {
    return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putInt(myInteger).array();
}

From source file:Main.java

public static byte[] my_short_to_bb_le(short myShort) {
    return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort(myShort).array();
}

From source file:Main.java

public static byte commandByteRepresentation(final int command) {

    final byte[] bytes = ByteBuffer.allocate(4).putInt(command).array();

    return bytes[3];
}

From source file:Main.java

public static byte[] getIntArray(int val) {
    ByteBuffer typeBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    typeBuffer.putInt(val);
    return typeBuffer.array();
}

From source file:Main.java

public static void putMessageLength(byte[] message) {
    ByteBuffer lengthBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    lengthBuffer.putInt(message.length);

    byte[] lenArray = lengthBuffer.array();

    for (int i = 0; i < 4; i++)
        message[i] = lenArray[i];/* ww w .j av a  2  s .c o m*/
}