JDK-4949583 : (cs) String(byte[]) constructor does not work on enormous byte arrays
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux,windows_2000
  • CPU: x86
  • Submitted: 2003-11-05
  • Updated: 2005-02-13
  • Resolved: 2005-02-13
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 11/05/2003


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

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
String Constructor public String(byte[] bytes) produces a BufferOverflowEception in nio package when passing a large array of a certain size

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a new String object passing an byte array of size 28664105.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The bug is reproducible for array sizes incremented by 4 (e.g. 28664105, 28664109, 28664113,...) It works fine for values between (e.g. 28664106, 28664107, 28664108).
ACTUAL -
The constructor throws a BufferoverflowException

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.nio.BufferOverflowException
	at java.nio.charset.CoderResult.throwException(Unknown Source)
	at java.lang.StringCoding$CharsetSD.decode(Unknown Source)
	at java.lang.StringCoding.decode(Unknown Source)
	at java.lang.StringCoding.decode(Unknown Source)
	at java.lang.String.<init>(Unknown Source)
	at java.lang.String.<init>(Unknown Source)
	at TestBufferOverflow.main(TestBufferOverflow.java:18)
Exception in thread "main"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class TestBufferOverflow {
	public static void main(String[] args) {
		int maxNumber = 28664105;
		byte[] ByteArray = new byte[maxNumber];
		String newStr = new String (ByteArray);
	}
}

---------- END SOURCE ----------
(Incident Review ID: 198982) 
======================================================================

Comments
EVALUATION True, but this is a fairly inefficient way to convert large byte sequences to strings. You'd be better off using java.nio.charset.CharsetDecoder directly. -- ###@###.### 2003/11/15 This is clearly a duplicate of 4949631: String.getBytes() does not work on some strings larger than 16MB which has been fixed for mustang. ###@###.### 2005-2-13 04:13:37 GMT
13-02-2005