JDK-7129312 : BufferedInputStream calculates negative array size with large streams and mark
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6u29,8
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2008
  • CPU: x86
  • Submitted: 2012-01-12
  • Updated: 2013-09-09
  • Resolved: 2013-08-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b106Fixed
Description
FULL PRODUCT VERSION :
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
There is an issue with the BufferedInputStream when used with large inputstreams and using mark where the fill() method will calculate a negative size and throw a NegativeArraySizeException

java.lang.NegativeArraySizeException
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source).



REGRESSION.  Last worked in version 6u29

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Open a large inputstream such as a file. ( My repro used a 3600 MB file)
2. Use a buffered inputstream to read the file
3. call mark
4. Copy the entire stream to another outputstream

exception will be thrown in BufferedInputStream.fill()

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
File copy works correctly.
ACTUAL -
java.lang.NegativeArraySizeException
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NegativeArraySizeException
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
// Note my input file was 3.6 Gbs for this repro.

InputStream buffStream = new BufferedInputStream(new FileInputStream(f));

        FileOutputStream outStream = new FileOutputStream(other);


            // Mark sourceStream for current position.
            buffStream.mark(Integer.MAX_VALUE);

        try {
            int count = -1;
            long total = 0;
            final byte[] retrievedBuff = new byte[8 * 1024];

            count = buffStream.read(retrievedBuff, 0, 8 * 1024);

            while (count != -1) {
                outStream.write(retrievedBuff, 0, count);
                total += count;
                count = buffStream.read(retrievedBuff, 0, 8 * 1024);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        buffStream.close();
        outStream.close();

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Dont use BufferedInputStream :)

Comments
ILW => P3
05-08-2013

This also duplicates with jdk8 b14.
28-09-2012