Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8048174

BufferedInputStream.available always return Integer.MAX_VALUE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7u11
    • core-libs
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0_11"

      ADDITIONAL OS VERSION INFORMATION :
      windows 7 32bit

      A DESCRIPTION OF THE PROBLEM :
      create new BufferedInputStream(new FileInputStream(file)), In some scenarios, available() will always return Integer.MAX_VALUE, show the code as folows:
      public synchronized int available() throws IOException {
              int n = count - pos;
              int avail = getInIfOpen().available();
              return n > (Integer.MAX_VALUE - avail)
                          ? Integer.MAX_VALUE
                          : n + avail;
          }

      I believe that getInIfOpen().available return negative number, so Integer.MAX_VALUE - avail return negative number because of numerical overflow.

      Consider the example code as folows:
      public static void main(String[] args) {
      String filePath = "test"; // file length = 10;
      File file = new File(filePath);
      InputStream inputStream = null;
      try {
      inputStream = new BufferedInputStream(new FileInputStream(file));

      byte[] buffer1 = new byte[5];
      inputStream.read(buffer1, 0, buffer1.length);

      byte[] buffer2 = new byte[5];
      inputStream.read(buffer2, 0, buffer2.length);

      inputStream.skip(1);

      inputStream.available(); // return Integer.MAX_VALUE
      } catch (Exception e) {
      System.out.println(e);
      } finally {
      if (null != inputStream) {
      try {
      inputStream.close();
      } catch (Exception e2) {
      System.out.println(e2);
      }
      }
      }
      }


      REPRODUCIBILITY :
      This bug can be reproduced always.

            igerasim Ivan Gerasimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: