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

voidreadFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
Reads len bytes in a loop using the channel of the stream
int toRead = len;
ByteBuffer byteBuffer = ByteBuffer.wrap(buf, off, len);
while (toRead > 0) {
    int ret = fileChannel.read(byteBuffer);
    if (ret < 0) {
        throw new IOException("Premeture EOF from inputStream");
    toRead -= ret;
...
ByteBufferreadFileDataIntoBufferBE(FileChannel fc, final int size)
read File Data Into Buffer BE
final ByteBuffer tagBuffer = ByteBuffer.allocateDirect(size);
fc.read(tagBuffer);
tagBuffer.position(0);
tagBuffer.order(ByteOrder.BIG_ENDIAN);
return tagBuffer;
ByteBufferreadFileFragment(FileChannel fc, long pos, int size)
read File Fragment
ByteBuffer fragment = null;
try {
    fc.position(pos);
    fragment = ByteBuffer.allocate(size);
    if (fc.read(fragment) != size)
        return null;
    fragment.rewind();
} catch (IOException ex) {
...
intreadFileHeader(FileInputStream fpi)
read File Header
DataInputStream inStrm = new DataInputStream(fpi);
byte[] buf = new byte[4];
try {
    inStrm.readFully(buf, 0, 4);
    String fType = new String(buf, 0, 4);
    if ("RIFF".equals(fType)) {
        byteOrder = ByteOrder.LITTLE_ENDIAN;
        if (readWavHeader(inStrm, fpi.getChannel()))
...
StringreadFileIntoString(File localFile)
read File Into String
FileInputStream stream = new FileInputStream(localFile);
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
    stream.close();
StringreadFileIntoString(String path)
Read the contents of a file into String.
FileInputStream stream = new FileInputStream(new File(path));
try {
    FileChannel fileChannel = stream.getChannel();
    MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0,
            fileChannel.size());
    return Charset.defaultCharset().decode(mappedByteBuffer).toString();
} finally {
    if (stream != null) {
...
voidreadFileNIO(String path, StringBuilder builder)
read File NIO
RandomAccessFile aFile = null;
FileChannel inChannel = null;
try {
    aFile = new RandomAccessFile(path, "r");
    inChannel = aFile.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (inChannel.read(buffer) > 0) {
        buffer.flip();
...
java.nio.ByteBufferreadFileToBuffer(java.io.File file)
read File To Buffer
FileInputStream is = new FileInputStream(file);
try {
    FileChannel fc = is.getChannel();
    java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate((int) fc.size());
    for (int count = 0; count >= 0 && buffer.hasRemaining();) {
        count = fc.read(buffer);
    buffer.flip();
...
StringreadFileToString(String path)
read File To String
FileInputStream stream = new FileInputStream(path);
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
    stream.close();
StringreadFileToString(String path)
Reads a file into a string.
FileInputStream stream = new FileInputStream(new File(path));
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
    stream.close();