Hao Zhong created IO-590:
----------------------------
Summary: Is zero a legal buffer size or not?
Key: IO-590
URL: https://issues.apache.org/jira/browse/IO-590
Project: Commons IO
Issue Type: Bug
Components: Utilities
Affects Versions: 3.x
Reporter: Hao Zhong
I notice that in the org.apache.commons.io.IOUtils class, zero is considered as
a legal buffer size. For example, the code of toByteArray is as follow:
{code:java}
public static byte[] toByteArray(final InputStream input, final 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];
}
...
}{code}
The toBufferedReader method does not check buffer sizes. It pass buffer sizes
to the underlying Java API:
{code:java}
public static BufferedReader toBufferedReader(final Reader reader, final int
size) {
return reader instanceof BufferedReader ? (BufferedReader) reader : new
BufferedReader(reader, size);
}
{code}
However, the underlying API considers zero as an illegal input:
{code:java}
public BufferedReader(Reader in, int sz) {
super(in);
if (sz <= 0)
throw new IllegalArgumentException("Buffer size <= 0");
this.in = in;
cb = new char[sz];
nextChar = nChars = 0;
}
{code}
I believe that the class shall have the same definition as the underlying API
does. Zero shall not be considered as an legal input.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)