Here you can find the source of readFileToBuffer(java.io.File file)
private static java.nio.ByteBuffer readFileToBuffer(java.io.File file) throws IOException
//package com.java2s; import java.io.*; import java.nio.channels.FileChannel; public class Main { private static java.nio.ByteBuffer readFileToBuffer(java.io.File file) throws IOException { FileInputStream is = new FileInputStream(file); try {//from w w w .jav a 2 s . co m 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(); return buffer; } finally { is.close(); } } }