class FileHelper {

    public static byte[] getByteArrayFromFile(File file) throws IOException {
        if (file == null) {
            return (byte[]) Array.newInstance(byte.class, 0);
        }

        log.debug(new StringBuilder("Attempting to get a byte array from file: ").append(file.getPath()).toString());

        // Get the size of the file
        long length = file.length();

        // Maximum size of file cannot be larger than the Integer.MAX_VALUE
        if (length > Integer.MAX_VALUE) {
            throw new IOException("File is too large: File length: " + file.length() + " bytes. Maximum length: " + Integer.MAX_VALUE + " bytes.");
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0, numRead = 0;

        InputStream is = null;
        try {
            is = new FileInputStream(file);
            while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }
        log.debug(new StringBuilder("Successfully attained a byte array from file: ").append(file.getPath()).toString());
        return bytes;
    }

}