JDK-8048174 : BufferedInputStream.available always return Integer.MAX_VALUE
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 7u11
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2014-06-24
  • Updated: 2014-06-28
  • Resolved: 2014-06-28
Related Reports
Duplicate :  
Description
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.


Comments
With JDK-8011136, the underlying inputStream's available() does not return negative values anymore. Negative values of available was unspecified behavior, so other implementations of InputStream should not do it either. Closing this bug as a duplicate.
28-06-2014