Bug in java.nio.StringCharBuffer.slice

John Platts john_platts at hotmail.com
Wed Nov 17 16:01:31 UTC 2010


Here is the current implementation of slice in java.nio.StringCharBuffer (as found in OpenJDK 7):
44     public CharBuffer slice() {
45         return new StringCharBuffer(str,
46                                     -1,
47                                     0,
48                                     this.remaining(),
49                                     this.remaining(),
50                                     this.position());
51     }

Here is the implementation of slice used for heap-based byte buffers, which is found in java/nio/Heap-X-Buffer.java.template:
 97     public $Type$Buffer slice() {
 98         return new Heap$Type$Buffer$RW$(hb,
 99                                         -1,
100                                         0,
101                                         this.remaining(),
102                                         this.remaining(),
103                                         this.position() + offset);
104     }

It appears that the slice() method fails to return the correct region of the string for CharBuffer objects that wrap a string whenever offset is non-zero. Here is the corrected code, with the correction in red:
44     public CharBuffer slice() {

45         return new StringCharBuffer(str,

46                                     -1,

47                                     0,

48                                     this.remaining(),

49                                     this.remaining(),

50                                     offset + this.position());

51     }

Is this bug limited to OpenJDK 7, or is this bug found in OpenJDK 6, JDK 6, JDK 5, and JDK 1.4? This bug might be affecting code that uses CharBuffers.
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20101117/962b8dc0/attachment.html>


More information about the core-libs-dev mailing list