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

intreadInt(InputStream inputStream)
read Int
byte[] bytesToRead = new byte[4];
int readBytes = inputStream.read(bytesToRead);
if (readBytes == -1) {
    return -1;
ByteBuffer buffer = ByteBuffer.wrap(bytesToRead);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer.getInt();
...
intreadInt(ReadableByteChannel channel)
Read an integer value from a channel.
ByteBuffer buf = ByteBuffer.allocate(4);
int count = 0;
while (count < 4) {
    int n = 0;
    while (n == 0)
        n = channel.read(buf);
    if (n < 0)
        throw new ClosedChannelException();
...
intreadInt(ReadableByteChannel channel)
read Int
ByteBuffer buf = ByteBuffer.allocate(4);
if (fillBuffer(channel, buf, true)) {
    buf.rewind();
    return buf.getInt();
return -1;
intreadInt16(DataInput di)
Read a 16-bit big-endian signed integer
final byte[] buf = { 0x00, 0x00 };
di.readFully(buf, 0, 2);
final int i = ByteBuffer.wrap(buf).getShort();
return i;
int[]readIntArray(DataInput input)
read Int Array
int length = input.readInt();
byte[] bytes = new byte[length * 4];
input.readFully(bytes);
int[] data = new int[length];
ByteBuffer.wrap(bytes).asIntBuffer().get(data);
return data;
intreadIntLittleEndian(InputStream in)
read Int Little Endian
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0) {
    throw new EOFException();
return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
...
ByteBufferreadIntoBuffer(RandomAccessFile file, int offset, int size)
Reading bytes from a file into a ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.order(BYTE_ORDER);
file.seek(offset);
file.read(buffer.array());
return buffer;
JSONObjectreadJsonFromFile(String path)
Reads JSON object (in string format) from specified file.
return new JSONObject(readStringFromFile(path));
StringreadLargeFile(File file)
Read large > 5Mb text files to String.
FileChannel channel = new FileInputStream(file).getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
channel.close();
return new String(buffer.array());
longreadLong(byte[] src, int pointer)
read Long
return ByteBuffer.wrap(src, pointer, 8).getLong();