List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:Main.java
public static long bytesToLong(byte[] bytes) { long l = 0;//from w ww.j a v a2 s .com try { ByteBuffer buffer = ByteBuffer.allocate(8); buffer.put(bytes, 0, bytes.length); buffer.flip();//need flip l = buffer.getLong(); } catch (Exception e) { } return l; }
From source file:Main.java
public static short toUnsignedByte(byte bytes) { bb = ByteBuffer.allocate(1); bb.put(bytes);/*from w ww . j a v a2s . c o m*/ bb.order(ByteOrder.LITTLE_ENDIAN); return (short) (bb.get() & 0xff); }
From source file:Main.java
public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original);/* w w w . j av a2s . co m*/ original.rewind(); clone.flip(); return clone; }
From source file:Main.java
private static byte[] longToBytes(long value) { ByteBuffer buffer = ByteBuffer.allocate(BYTE_BUFFER_CAPACITY); buffer.putLong(value);/* ww w .j ava2 s . c o m*/ return buffer.array(); }
From source file:Main.java
public static byte[] shortToByteArray(int value) { ByteBuffer bb = ByteBuffer.allocate(2); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putShort((short) value); return bb.array(); }
From source file:Main.java
public static byte[] intToByteArray(int value) { ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); buffer.order(ByteOrder.BIG_ENDIAN); buffer.putInt(value);/*from w ww . j a v a 2s .co m*/ return buffer.array(); }
From source file:Main.java
public static int readInt(byte[] message, int start) { /* Read in the type */ ByteBuffer typeBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN); typeBuffer.put(message, start, 4).position(0); return typeBuffer.getInt(); }
From source file:Main.java
public static byte[] intToByteArrayLE(int value) { ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putInt(value);/*from w w w.j av a2 s. c om*/ return buffer.array(); }
From source file:Main.java
public static byte[] shortToByteArray(short value) { ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE); buffer.order(ByteOrder.BIG_ENDIAN); buffer.putShort(value);// w ww . j a v a2 s .c om return buffer.array(); }
From source file:Main.java
public static byte[] shortToByteArrayLE(short value) { ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putShort(value);// w w w . j a v a2 s. c o m return buffer.array(); }