JDK-4851247 : java.nio.BufferOverflowException in java.lang.String.getBytes
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-04-21
  • Updated: 2003-07-28
  • Resolved: 2003-07-28
Description

Name: rmT116609			Date: 04/20/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

JDK 1.3.1 does not exhibit the problem.

FULL OS VERSION :
W2K SP 3
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Read in a String with approximately 50 MB of data. Call the getBytes method on the String. You will get the following stack trace (this one from JDK 1.4.2, other stack traces are the same, except for line numbers):

Exception in thread "main" java.nio.BufferOverflowException
        at java.nio.charset.CoderResult.throwException(CoderResult.java:259)
        at java.lang.StringCoding$CharsetSE.encode(StringCoding.java:340)
        at java.lang.StringCoding.encode(StringCoding.java:374)
        at java.lang.StringCoding.encode(StringCoding.java:380)
        at java.lang.String.getBytes(String.java:590)
        at TestGetBytes.main(TestGetBytes.java:35)



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
You can use the following code to create a simple class which reads in a file into a String and then calls getBytes. The problem is very easy to reproduce. In our experience, a file of approximately 50-60MB will always reproduce the problem.


EXPECTED VERSUS ACTUAL BEHAVIOR :
No exceptions to occur.
Exception in thread "main" java.nio.BufferOverflowException
        at java.nio.charset.CoderResult.throwException(CoderResult.java:259)
        at java.lang.StringCoding$CharsetSE.encode(StringCoding.java:340)
        at java.lang.StringCoding.encode(StringCoding.java:374)
        at java.lang.StringCoding.encode(StringCoding.java:380)
        at java.lang.String.getBytes(String.java:590)
        at TestGetBytes.main(TestGetBytes.java:35)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.nio.BufferOverflowException
        at java.nio.charset.CoderResult.throwException(CoderResult.java:259)
        at java.lang.StringCoding$CharsetSE.encode(StringCoding.java:340)
        at java.lang.StringCoding.encode(StringCoding.java:374)
        at java.lang.StringCoding.encode(StringCoding.java:380)
        at java.lang.String.getBytes(String.java:590)
        at TestGetBytes.main(TestGetBytes.java:35)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;

/**
 * To test whether String.getBytes can handle a lot of data.
 */
public class TestGetBytes
{
    public static void main (String [] args) throws Exception
    {
        if (args.length < 1) {
            System.out.println ("USAGE: TestGetBytes <filename>");
            System.exit (0);
        }

        String filename = args[0];

	File f = new File(filename);
        BufferedReader br = new BufferedReader (new FileReader (f));
        String line = null;
        String out = "";
        StringBuffer other = new StringBuffer ();

            long start = System.currentTimeMillis ();
            long end;

            while ((line = br.readLine ()) != null)
            {
                other.append(line);
            }

            end = System.currentTimeMillis ();

            System.out.println ("Time "+(end-start));

        byte b[] = other.toString().getBytes();
    }
}

---------- END SOURCE ----------
(Review ID: 184402) 
======================================================================

Comments
EVALUATION Not reproducible. This is probably due to a coding error in the input file, and most likely has nothing to do with the size of the file. The given code runs fine on a 60MB input file of plain ASCII. To diagnose this further we would need a copy of the input file and the name of the locale you're using. -- ###@###.### 2003/4/22
04-11-0188