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

byte[]toByteArrayFromInt(int intValue, boolean shortSize)
Converts an integer (which in Java is a signed four bytes value) into a byte array
byte[] tempArr = ByteBuffer.allocate(4).putInt(intValue).array();
byte[] retArr;
if (shortSize) {
    retArr = ByteBuffer.allocate(2).put(tempArr, 2, 2).array();
} else {
    retArr = tempArr;
return retArr;
...
byte[]toByteArrayFromLong(long longValue)
Converts a long (which in Java is a signed eight bytes value) into a byte array
return ByteBuffer.allocate(8).putLong(longValue).array();
byte[]toBytes(BigDecimal number, int byteLength)
to Bytes
return toBytes(number.unscaledValue(), byteLength);
byte[]toBytes(char[] ch)
Converts a char array to a byte array.
return toBytes(ch, Charset.defaultCharset());
byte[]toBytes(char[] chars)
to Bytes
CharBuffer cf = CharBuffer.wrap(chars);
ByteBuffer bf = CHARSET.encode(cf);
return bf.array();
byte[]toBytes(char[] string)
to Bytes
CharBuffer charBuffer = CharBuffer.wrap(string);
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
Arrays.fill(charBuffer.array(), '\u0000');
Arrays.fill(byteBuffer.array(), (byte) 0);
return bytes;
byte[]toBytes(final float val)
Convert a float value to a byte array
ByteBuffer bb = ByteBuffer.allocate(SIZEOF_FLOAT);
bb.putFloat(val);
return bb.array();
byte[]toBytes(final UUID uuid)
to Bytes
return ByteBuffer.allocate(Integer.SIZE).putInt(uuid.hashCode()).array();
byte[]toBytes(InputStream input)
Converts an Input Stream to byte[]
if (input == null)
    return null;
try {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int n = 0;
    int count = -1;
    while (EOF != (n = input.read(buffer))) {
...
byte[]toBytes(int value)
to Bytes
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(value).flip();
return readBytes(buffer, 4);