[ https://issues.apache.org/jira/browse/IO-486?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14996199#comment-14996199 ]
Viktor Isaev commented on IO-486: --------------------------------- OK, Sebb, I see your point. But then, if so, {{FileUtils.readFileToByteArray()}} is not safe too, because it uses {{file.length()}}: {code:java} public static byte[] readFileToByteArray(File file) throws IOException { InputStream in = null; try { in = openInputStream(file); return IOUtils.toByteArray(in, file.length()); } finally { IOUtils.closeQuietly(in); } } {code} Here we may as well miss data, if OS returns a file length less then the real data size. Should this be fixed? What do you think? By the way, in {{IOUtils.toByteArray()}}, which is called from {{FileUtils.readFileToByteArray()}}, there IS already a short-circuit for the case when {{size == 0}}: {code:java} public static byte[] toByteArray(InputStream input, int size) throws IOException { if (size < 0) { throw new IllegalArgumentException("Size must be equal or greater than zero: " + size); } if (size == 0) { return new byte[0]; } byte[] data = new byte[size]; int offset = 0; int readed; while (offset < size && (readed = input.read(data, offset, size - offset)) != EOF) { offset += readed; } if (offset != size) { throw new IOException("Unexpected readed size. current: " + offset + ", excepted: " + size); } return data; } {code} (However, we still open an input stream.) So, my suggestion is to at least make these two guys ({{readFileToString()}} and {{readFileToByteArray()}}) consistent with each other in the sense of using {{file.length()}} and related short-circuits. > FileUtils.readFileToString() can immediately return empty string if file > length is zero > --------------------------------------------------------------------------------------- > > Key: IO-486 > URL: https://issues.apache.org/jira/browse/IO-486 > Project: Commons IO > Issue Type: Improvement > Reporter: Viktor Isaev > > If {{file.length() == 0}}, there is no point of doing extra work of opening > and closing an input stream - we can return an empty string right away. > Same applies for {{FileUtils.readFileToByteArray()}} and > {{FileUtils.readLines()}}. -- This message was sent by Atlassian JIRA (v6.3.4#6332)