Java FileInputStream Read readFileToBytes(File f, int max)

Here you can find the source of readFileToBytes(File f, int max)

Description

Read bytes from a file.

License

Open Source License

Parameter

Parameter Description
f file to read
max if non-zero, at most 'max' bytes will be read

Exception

Parameter Description
IOException on error

Return

bytes in the file

Declaration

public static byte[] readFileToBytes(File f, int max)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*  w  ww .j av a 2  s .c om*/
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * AC 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Read bytes from a file. Used by all "one-shot-reads".
     * @param f file to read
     * @param max if non-zero, at most 'max' bytes will be read
     * @return bytes in the file
     * @throws IOException on error
     */
    public static byte[] readFileToBytes(File f, int max)
            throws IOException {
        int length = (int) f.length();
        if (max > 0 && max < length) {
            length = max;
        }
        try {
            return readStreamToBytes(new FileInputStream(f), length);
        } catch (IOException ioe) {
            throw new IOException("could not read bytes from "
                    + f.getAbsolutePath(), ioe);
        }
    }

    /**
     * Read bytes from a stream. Used by all "one-shot-reads".
     * @param is stream to read
     * @param length number of bytes to read; if less are read, an exception is thrown
     * @return bytes in the file
     * @throws IOException on error
     */
    public static byte[] readStreamToBytes(InputStream is, int length)
            throws IOException {
        byte buffer[] = new byte[length];
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(is);
            int read = bis.read(buffer, 0, length);
            if (read != length) {
                throw new IOException("not enough bytes: wanted " + length
                        + ", read only " + read);
            }
            return buffer;
        } finally {
            if (bis != null) {
                bis.close();
            }
        }
    }
}

Related

  1. readFileToByteArray(File file)
  2. readFileToByteArray(File file)
  3. readFileToByteArray(String file)
  4. readFileToByteArray(String filename)
  5. readFileToBytes(File f)
  6. readFileToBytes(String path)
  7. readFileToStr(String fn, String charset)
  8. readFileUTF8(String file)
  9. readFileUTF_8(String filePath)