Java Utililty Methods File Read via ByteBuffer

List of utility methods to do File Read via ByteBuffer

Description

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

Method

longreadUint32(final DataInput di)
Read a 32-bit big-endian unsigned integer using a DataInput.
final byte[] buf8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
di.readFully(buf8, 4, 4);
return ByteBuffer.wrap(buf8).getLong();
intreadUint32AsInt(DataInput di)
Overflow checking since java can't handle unsigned numbers.
final long l = readUint32(di);
if (l > Integer.MAX_VALUE) {
    throw new IOException("uint32 value read overflows int");
return (int) l;
longreadUintLE(byte[] bytes, int pointer, int size)
read Uint LE
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put(bytes, pointer, size);
buffer.put(new byte[8 - size]);
buffer.flip();
return buffer.order(ByteOrder.LITTLE_ENDIAN).getLong();
longreadUnsignedInt(InputStream is)
Reads an unsigned integer (32 bit) from specified input stream.
return (long) readInt(is) & 0xffffffffL;
intreadUnsignedInt24(InputStream is)
Reads an unsigned 24 bit integer from specified input stream.
ByteBuffer bb4 = getByteBuffer(4);
for (int cnt = 0; cnt < 3;) {
    int n = is.read(bb4.array(), cnt, bb4.array().length - cnt - 1);
    if (n < 0) {
        throw new IOException("End of stream");
    cnt += n;
bb4.position(0);
return bb4.getInt() & 0xffffff;
StringreadUtf8(DataInput in)
Safely reads a string as UTF-8 from a byte stream.
byte[] bytes = new byte[in.readInt()];
in.readFully(bytes);
return decodeUtf8(bytes);
longreadVarLong(byte[] arr)
read Var Long
return readVarLong(ByteBuffer.wrap(arr));
short[]readWaveFile44100_16Bit_Mono(String dir, String name)
read Wave Fil Bi Mono
try {
    return readWaveFile44100_16Bit_Mono(new BufferedInputStream(new FileInputStream(new File(dir, name))));
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
booleanreadWavHeader(DataInputStream inStrm, FileChannel fc)
read Wav Header
byte[] buf = new byte[16];
int size;
ByteBuffer bb = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN);
bb.clear();
try {
    inStrm.readInt();
    inStrm.readFully(buf, 0, 8);
    if (!"WAVEfmt ".equals(new String(buf, 0, 8)))
...
voidreadWholeFile(File file, StringBuilder stringBuilder)
Reads a whole file into a StringBuffer based on java.nio
long length = file.length();
MappedByteBuffer in = new FileInputStream(file).getChannel().map(FileChannel.MapMode.READ_ONLY, 0, length);
int i = 0;
while (i < length)
    stringBuilder.append((char) in.get(i++));