Skip to content

Commit 936b820

Browse files
ilmarmorsPascalSchumacher
authored andcommitted
IO-542: FileUtils#readFileToByteArray: optimize reading of files with known size (closes #38)
1 parent d4f28d7 commit 936b820

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,9 @@ public static String readFileToString(final File file) throws IOException {
18491849
*/
18501850
public static byte[] readFileToByteArray(final File file) throws IOException {
18511851
try (InputStream in = openInputStream(file)) {
1852-
return IOUtils.toByteArray(in); // Do NOT use file.length() - see IO-453
1852+
long fileLength = file.length();
1853+
// file.length() may return 0 for system-dependent entities, treat 0 as unknown length - see IO-453
1854+
return fileLength > 0 ? IOUtils.toByteArray(in, fileLength) : IOUtils.toByteArray(in);
18531855
}
18541856
}
18551857

0 commit comments

Comments
 (0)