Java Utililty Methods ByteBuffer Read

List of utility methods to do ByteBuffer Read

Description

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

Method

intreadCInt(ByteBuffer buffer)
read C Int
final byte head = buffer.get();
if (head > -127 && head <= 127) {
    return head;
if (head == -128) {
    return buffer.getShort();
} else {
    return buffer.getInt();
...
StringreadCString(ByteBuffer buf, int len)
read C String
return readCString(buf, new byte[len], len);
byte[]readDERString(ByteBuffer buf)
This method reads a DER encoded byte string from a ByteBuffer.
int length = buf.getInt();
if (length > 8192) {
    throw new IllegalArgumentException("DER String Length " + length + " > 8192");
byte[] bytes = new byte[length];
buf.get(bytes);
return bytes;
longreadDword(ByteBuffer buffer)
read Dword
long v = (long) buffer.getInt();
if (v < 0) {
    v += 4294967296L;
return v;
ByteBufferreadFileToByteBuffer(File file)
read File To Byte Buffer
RandomAccessFile randomAccessFile = null;
try {
    randomAccessFile = new RandomAccessFile(file, "r");
    byte[] bytes = new byte[(int) randomAccessFile.length()];
    randomAccessFile.readFully(bytes);
    return ByteBuffer.wrap(bytes);
} finally {
    if (randomAccessFile != null)
...
StringreadFixedLengthString(ByteBuffer buf, int length)
read Fixed Length String
return new String(readBytes(buf, length));
StringreadFixedLengthString(ByteBuffer byteBuffer, int size)
read Fixed Length String
byte[] buf = new byte[size];
byteBuffer.get(buf);
return new String(buf, StandardCharsets.UTF_8).trim();
floatreadFixedPoint88(ByteBuffer bb)
read Fixed Point
byte[] bytes = new byte[2];
bb.get(bytes);
short result = 0;
result |= ((bytes[0] << 8) & 0xFF00);
result |= ((bytes[1]) & 0xFF);
return ((float) result) / 256;
intreadFourByteInt(ByteBuffer buffer, int start)
read Four Byte Int
final byte b1 = buffer.get(start);
final byte b2 = buffer.get(start + 1);
final byte b3 = buffer.get(start + 2);
final byte b4 = buffer.get(start + 3);
return ((b1 & 255) << 24) | ((b2 & 255) << 16) | ((b3 & 255) << 8) | (b4 & 255);
voidreadFrom(File file, ByteBuffer buffer)
read From
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
    FileChannel channel = raf.getChannel();
    long needed = raf.length();
    while (needed > 0 && buffer.hasRemaining())
        needed = needed - channel.read(buffer);