Java File Read via ByteBuffer readFileFragment(FileChannel fc, long pos, int size)

Here you can find the source of readFileFragment(FileChannel fc, long pos, int size)

Description

read File Fragment

License

Open Source License

Declaration

public static ByteBuffer readFileFragment(FileChannel fc, long pos, int size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static ByteBuffer readFileFragment(FileChannel fc, long pos, int size) {
        ByteBuffer fragment = null;
        try {/*w w  w.  jav  a  2 s.  c o  m*/
            fc.position(pos);
            fragment = ByteBuffer.allocate(size);
            if (fc.read(fragment) != size)
                return null;
            fragment.rewind();
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return fragment;
    }
}

Related

  1. readFileAsByteArray(String path)
  2. readFileAsStringArray(File file)
  3. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  4. readFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
  5. readFileDataIntoBufferBE(FileChannel fc, final int size)
  6. readFileHeader(FileInputStream fpi)
  7. readFileIntoString(File localFile)
  8. readFileIntoString(String path)
  9. readFileNIO(String path, StringBuilder builder)