/** * Read and return the entire contents of the supplied {@link File file}. * * @param file the file containing the contents; may be null * @return the contents, or an empty byte array if the supplied file is null * @throws IOException if there is an error reading the content */ public static byte[] readBytes( File file ) throws IOException { if (file == null) return new byte[] {}; InputStream stream = new BufferedInputStream(new FileInputStream(file)); boolean error = false; try { return readBytes(stream); } catch (IOException e) { error = true; // this error should be thrown, even if there is an error closing stream throw e; } catch (RuntimeException e) { error = true; // this error should be thrown, even if there is an error closing stream throw e; } finally { try { stream.close(); } catch (IOException e) { if (!error) throw e; } } }