Android File to ByteBuffer Read bufferFile(File file)

Here you can find the source of bufferFile(File file)

Description

buffer File

License

Open Source License

Declaration

public static ByteBuffer bufferFile(File file) throws IOException 

Method Source Code

//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;
    }
}

Related

  1. readToByteBuffer(File f)
  2. readToByteBuffer(File f, int toRead)