FileSystemUtil { @SuppressWarnings("squid:S1166") public static byte[] readBytesFromFile(String filename) { File file = new File(filename); try { return Files.toByteArray(file); } catch (IOException e) { String msg = String.format("Error reading file %s: %s", file.getAbsolutePath(), e.getMessage()); LOGGER.error(msg); throw new IllegalArgumentException(msg, e); } } private  FileSystemUtil(); static boolean checkFileOrResourceExists(String filename); static void assertFileOrResourceExists(String filename); static String readFileAsText(String filename); static String readFileAsText(String fileName, String alternateDir); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void writeStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void appendStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static byte[] readBytesFromFile(String filename); static void deleteDirectoryQuietly(String rootDirName); static final String CLASSPATH_PREFIX; }
@Test public void testReadBytesFromFile() throws IOException { String filename = "build/bytes_test_file"; byte[] bytes = {1, 2, 3, 10, 11, 12}; Files.write(bytes, new File(filename)); byte[] result = FileSystemUtil.readBytesFromFile(filename); assertNotNull(result); assertThat(result, is(bytes)); }
@Test public void testReadBytesFromFileNotExisting() { String filename = "build/not_existing_file"; try { FileSystemUtil.readBytesFromFile(filename); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException e) { assertThat(e.getMessage().startsWith("Error reading file "), is(true)); } }