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

long[]toLongArray(byte[] b)
to Long Array
ByteBuffer bb = ByteBuffer.wrap(b);
int length = bb.getInt();
if (length == -1) {
    return null;
long[] la = new long[length];
for (int i = 0; i < length; i++) {
    la[i] = bb.getLong();
...
long[]ToLongArray(byte[] data)
To Long Array
int n = (data.length % 8 == 0 ? 0 : 1) + data.length / 8;
long[] result = new long[n];
for (int i = 0; i < n - 1; i++) {
    result[i] = bytes2long(data, i * 8);
byte[] buffer = new byte[8];
for (int i = 0, j = (n - 1) * 8; j < data.length; i++, j++) {
    buffer[i] = data[j];
...
long[]toLongArray(byte[] ids)
to Long Array
LongBuffer buffy = ByteBuffer.wrap(ids).order(ByteOrder.LITTLE_ENDIAN).asLongBuffer();
long[] result = new long[buffy.capacity()];
int i = 0;
while (buffy.hasRemaining()) {
    result[i++] = buffy.get();
return result;
ByteBuffertoMappedBuffer(File file)
to Mapped Buffer
try (FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ)) {
    return channel.map(MapMode.READ_ONLY, 0, file.length());
ByteBuffertoMappedBuffer(File file)
to Mapped Buffer
RandomAccessFile raf = null;
try {
    raf = new RandomAccessFile(file, "r");
    return raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
} finally {
    if (raf != null) {
        raf.close();
ByteBuffertoMappedBuffer(File file, long start, long end)
to Mapped Buffer
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
    return raf.getChannel().map(FileChannel.MapMode.READ_ONLY, start, end);
byte[]toMsftBinary(UUID uuid)
to Msft Binary
byte[] uuidMsftBytes = new byte[16];
byte[] uuidThriftBytes = toThriftBinary(uuid);
uuidMsftBytes[3] = uuidThriftBytes[0];
uuidMsftBytes[2] = uuidThriftBytes[1];
uuidMsftBytes[1] = uuidThriftBytes[2];
uuidMsftBytes[0] = uuidThriftBytes[3];
uuidMsftBytes[5] = uuidThriftBytes[4];
uuidMsftBytes[4] = uuidThriftBytes[5];
...
shorttoShort(byte firstByte, byte secondByte)
Retrieves a short from the given two bytes.
return ByteBuffer.wrap(new byte[] { firstByte, secondByte }).getShort();
shorttoShort(byte[] bytes, int index)
to Short
return ByteBuffer.wrap(bytes, index, 2).order(ByteOrder.LITTLE_ENDIAN).getShort();
shorttoShort(final byte byteValue, final ByteOrder byteOrder)
to Short
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2);
byteBuffer.order(byteOrder);
byteBuffer.put(byteValue);
byteBuffer.put((byte) 0x00);
byteBuffer.flip();
return byteBuffer.getShort();