Java Utililty Methods Convert via ByteBuffer

List of utility methods to do Convert via ByteBuffer

Description

The list of methods to do Convert via ByteBuffer are organized into topic(s).

Method

IntBufferintArrayToIntBuffer(int[] data)
Converts an array of primitive ints to a java.nio.IntBuffer .
IntBuffer ret = ByteBuffer.allocateDirect(data.length << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
ret.put(data).flip();
return ret;
byte[]intsToBytes(int[] n)
ints To Bytes
byte[] bytes = new byte[n.length * (Integer.SIZE / 8)];
ByteBuffer b = ByteBuffer.wrap(bytes);
b.asIntBuffer().put(n);
return bytes;
byte[]IntToBArray(int src)
Int To B Array
return ByteBuffer.allocate(4).putInt(src).array();
byte[]intToByte(int[] intArray)
int To Byte
ByteBuffer byteBuffer = ByteBuffer.allocate(intArray.length * 4);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(intArray);
byte[] array = byteBuffer.array();
return array;
byte[]intToByteArray(int integer)
int To Byte Array
ByteBuffer buf = ByteBuffer.allocate(4);
buf.putInt(integer);
return buf.array();
byte[]intToByteArray(int l)
Converts an int to a byte array of length 4 containing the integer encoded in two's complement.
byte[] bArray = new byte[4];
ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
IntBuffer iBuffer = bBuffer.asIntBuffer();
iBuffer.put(0, l);
return bArray;
byte[]intToByteArray(int number)
Returns a int as a byte array.
return ByteBuffer.allocate(Integer.BYTES).putInt(number).array();
byte[]intToByteArray(int someInt, int byteSize)
Convert an int to an byte array
if (byteSize < 0) {
    throw new RuntimeException("Invalid byte size");
ByteBuffer buff = resize(ByteBuffer.allocate(4).putInt(someInt), byteSize);
return buff.array();
byte[]intToBytes(final int i)
Converts integer to big-endian byte array.
if (i < 0) {
    throw new IllegalArgumentException("Cannot convert negative numbers.");
if (NUM_BYTES_PER_INT < MAX_INT_SIZE && (i > (2 ^ (8 * NUM_BYTES_PER_INT)))) {
    throw new IllegalArgumentException("Byte array too large");
ByteBuffer bb = ByteBuffer.allocate(MAX_INT_SIZE).putInt(i);
bb.rewind();
...
byte[]intToBytes(final int integer)
Convert an integer into a byte array.
return ByteBuffer.allocate(INTEGER_SIZE).order(ByteOrder.LITTLE_ENDIAN).putInt(integer).array();