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.
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.
- duplicates
-
JDK-8011136 FileInputStream.available and skip inconsistencies
- Closed