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()); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File is too large: File length: " + file.length() + " bytes. Maximum length: " + Integer.MAX_VALUE + " bytes."); } byte[] bytes = new byte[(int) length]; 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(); } } 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; }  static byte[] base64Encode(final File input); static byte[] base64Encode(final byte[] input); static byte[] getByteArrayFromFile(File file); static boolean renameFile(final File fromFile, final String toFileName); static boolean copyFileToPath(final File inFile, final String outFilePath); static boolean copyFileToPath(final File inFile, final String outPath, boolean deleteOriginalFile); static Collection<File> wipeOldFiles(File directory, Long cutoffTime, boolean deleteDirectory); static boolean createDir(File directory); static boolean createDir(String directory); static boolean deleteDirRecursively(File directory); static boolean deleteDirRecursively(String directory); static boolean deleteFileQuietly(String filePath); static boolean deleteFileQuietly(File file); static boolean deleteFile(String filePath); static boolean deleteFile(File file); static boolean doesDirectoryOrFileExist(String filePath); static File findFile(String file, String rootPath); static List<String> getFileList(String filePath, boolean recursive); static List<String> getFileList(String filePath, String[] extensions, boolean recursive); static Collection<File> getFileCollection(String filePath, boolean recursive); static Collection<?> getFileCollection(String filePath, String[] extensions, boolean recursive); static String getSystemTemp(); static boolean zipDirectory(String inputDirectory, String absoluteFileName); static boolean unzipFile(String outputDirectory, File zipFile); static String createUserDirectory(String applicationUserSpaceDir); static boolean updateTimestamp(final String path, final boolean recursive); static Boolean validateShapeFile(final File shapeFile); static Boolean validateShapeZIP(final File shapeZip);  }
@Test public void testFileToByteArray() { System.out.println("Test: " + "testFileToByteArray"); File input = null; byte[] result = null; input = new File(FileHelperTest.sampleDir + "test_zip.zip"); try { result = FileHelper.getByteArrayFromFile(input); } catch (IOException ex) { fail(ex.getMessage()); } assertThat(result.length, is(not(0))); assertThat(result.length, is(equalTo((int) input.length()))); }
@Test public void testFileToByteArrayWithNullInput() { System.out.println("Test: " + "testFileToByteArrayWithNullInput"); File input = null; byte[] result = null; try { result = FileHelper.getByteArrayFromFile(input); } catch (IOException ex) { fail(ex.getMessage()); } assertThat(result.length, is(not(nullValue()))); assertThat(result.length, is(equalTo(0))); }