Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-590

Is zero a legal buffer size or not?

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Not A Problem
    • None
    • None
    • Utilities
    • None

    Description

      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:

       

      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];
      }
      ...
      }

      The toBufferedReader method does not check buffer sizes. It pass buffer sizes to the underlying Java API:

       

       

      public static BufferedReader toBufferedReader(final Reader reader, final int size) {
      return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader, size);
      }
      

      However, the underlying API considers zero as an illegal input:

       

       

      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;
      }
      

      I believe that the class shall have the same definition as the underlying API does. Zero shall not be considered as an legal input.

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            haozhong Hao Zhong
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: