Here you can find the source of readFile(File file)
Parameter | Description |
---|---|
file | The file from which the byte array is read |
public static byte[] readFile(File file)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**//from w w w .j a v a2 s. c o m *** Returns an array of bytes read from the specified file *** * @param file * The file path from which the byte array is read *** @return The byte array read from the specified file **/ public static byte[] readFile(String file) { if ((file != null) && !file.equals("")) { return readFile(new File(file)); } else { return null; } } /** *** Returns an array of bytes read from the specified file *** * @param file * The file from which the byte array is read *** @return The byte array read from the specified file **/ public static byte[] readFile(File file) { if (file == null) { return null; } else if (!file.exists()) { // Print.logError("File does not exist: " + file); return null; } else { FileInputStream fis = null; try { fis = new FileInputStream(file); return readStream(fis); } catch (IOException ioe) { // Print.logError("Unable to read file: " + file + " [" + ioe + // "]"); } finally { if (fis != null) { try { fis.close(); } catch (IOException ioe) {/* ignore */ } } } return null; } } /** *** Returns an array of bytes read from the specified InputStream *** * @param input * The InputStream *** @return The array of bytes read from the InputStream **/ public static byte[] readStream(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copyStreams(input, output); return output.toByteArray(); } /** *** Copies bytes from one stream to another *** * @param input * The InputStream *** @param output * The OutputStream *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output) throws IOException { return copyStreams(input, output, -1); } /** *** Copies bytes from one stream to another *** * @param input * The InputStream *** @param output * The OutputStream *** @param maxLen * The maximum number of bytes to copy *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output, int maxLen) throws IOException { /* copy nothing? */ if (maxLen == 0) { return 0; } /* copy bytes */ int length = 0; // count of bytes copied byte tmpBuff[] = new byte[10 * 1024]; // 10K blocks while (true) { /* read length */ int readLen; if (maxLen >= 0) { readLen = maxLen - length; if (readLen == 0) { break; // done reading } else if (readLen > tmpBuff.length) { readLen = tmpBuff.length; // max block size } } else { readLen = tmpBuff.length; } /* read input stream */ int cnt = input.read(tmpBuff, 0, readLen); /* copy to output stream */ if (cnt < 0) { if ((maxLen >= 0) && (length != maxLen)) { // Print.logError("Copy size mismatch: " + maxLen + " => " + // length); } break; } else if (cnt > 0) { output.write(tmpBuff, 0, cnt); length += cnt; if ((maxLen >= 0) && (length >= maxLen)) { break; // per 'maxLen', done copying } } else { // Print.logDebug("Read 0 bytes ... continuing"); } } output.flush(); /* return number of bytes copied */ return length; } }