Java File Read via ByteBuffer largeFileReader(String filename)

Here you can find the source of largeFileReader(String filename)

Description

good for Large Files >2Mb

License

Open Source License

Parameter

Parameter Description
filename a parameter

Declaration

private static byte[] largeFileReader(String filename) 

Method Source Code

//package com.java2s;
/*/*from  w  w w. j  ava  2  s. c o m*/
 *  Copyright (c) 2009, Amostudio,inc
 *  All rights reserved.
 *  Code licensed under the BSD License:
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Amostudio,inc  nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY Amostudio,inc ''AS IS'' AND ANY
 *   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL Amostudio,inc  BE LIABLE FOR ANY
 *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /**
     *
     * good for Large Files >2Mb
     * @param filename
     * @return
     */
    private static byte[] largeFileReader(String filename) {
        byte[] bytes = null;
        FileChannel fc = null;
        try {
            fc = new FileInputStream(filename).getChannel();

            MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            int size = byteBuffer.capacity();
            if (size > 0) {
                // Retrieve all bytes in the buffer
                byteBuffer.clear();
                bytes = new byte[size];
                byteBuffer.get(bytes, 0, bytes.length);
            }
            fc.close();
        } catch (FileNotFoundException fnf) {
            System.err.println("" + fnf);
        } catch (IOException io) {
            System.err.println("" + io);
        } finally {
            if (fc != null) {
                try {
                    fc.close();
                } catch (IOException ioe) {
                    // ignore
                }
            }
        }
        return bytes;
    }
}

Related

  1. copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
  2. copyStream(BufferedReader reader, BufferedWriter writer)
  3. count(final ReadableByteChannel src)
  4. getReadBuffer(File file)
  5. getStringContents(ReadableByteChannel channel)
  6. mapReadWrite(File file)
  7. nioCopy(ReadableByteChannel input, WritableByteChannel output)
  8. openReadOnly(Path path, int offset, int length)
  9. read(DataInput in, int length)