Java FileInputStream Read load(File file)

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

Description

load

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] load(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2012 by Min Cai (min.cai.china@gmail.com).
 *
 * This file is part of the PickaPack library.
 *
 * PickaPack is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version./*from w w w .j a  v a 2  s . c o  m*/
 *
 * PickaPack is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PickaPack. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.*;

public class Main {
    /**
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] load(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        byte[] content = load(in, (int) file.length());
        in.close();
        return content;
    }

    /**
     *
     * @param in
     * @param length
     * @return
     * @throws IOException
     */
    public static byte[] load(InputStream in, int length) throws IOException {
        byte[] buffer = new byte[length];
        int offset = 0;
        for (;;) {
            int remain = length - offset;
            if (remain <= 0) {
                break;
            }
            int numRead = in.read(buffer, offset, remain);
            if (numRead == -1) {
                throw new IOException("Reached EOF, read " + offset + " expecting " + length);
            }
            offset += numRead;
        }
        return buffer;
    }
}

Related

  1. load(File file)
  2. load(File file)
  3. load(String file)
  4. loadContents(File file)
  5. loadData(File f)