Here you can find the source of bufferFile(File file)
public static ByteBuffer bufferFile(File file) throws IOException
//package com.java2s; /*/*from w w w .ja va 2 s . c om*/ * Copyright (c) 2015. Philip DeCamp * Released under the BSD 2-Clause License * http://opensource.org/licenses/BSD-2-Clause */ import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static ByteBuffer bufferFile(File file) throws IOException { long size = file.length(); ByteBuffer buf = ByteBuffer.allocate((int) (size & 0x7FFFFFFF)); FileChannel chan = new FileInputStream(file).getChannel(); while (buf.remaining() > 0) { int n = chan.read(buf); if (n <= 0) throw new IOException("Read operation failed."); } chan.close(); buf.flip(); return buf; } }