Name: rmT116609 Date: 02/19/2004
FULL PRODUCT VERSION :
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux ventana 2.4.22-1.2115.nptl #1 Wed Oct 29 15:42:51 EST 2003 i686 i686 i386 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
As per javadoc, CharBuffer.slice() should return a "new buffer will start at this buffer's current position" and " The new buffer's position will be zero." The CharBuffer returned on the a call to CharBuffer.wrap( CharSequence ) appears to be the StringCharBuffer which appears to have a broken slice implementation. Slice in this case appears to return the same CharBuffer as would be returned via duplicate(): position() is same as original buffer.
See test case below.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
(Test case output desired):
Slice 1 position (fixed): 0
Slice 2 position (correct): 0
ACTUAL -
Actual test case output:
Slice 1 position (bug): 3
Slice 2 position (correct): 0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.nio.*;
public class slicebug
{
public static void main( String[] args )
{
String in = "foobar";
// This version of wrap appears to internally use a
// StringCharBuffer, which has the problem:
CharBuffer buff1 = CharBuffer.wrap( in );
buff1.position( 3 );
CharBuffer slice1 = buff1.slice();
// Position of slice should be 0, but is 3 instead:
System.out.println( "Slice 1 position (bug): "
+ slice1.position() );
// Demonstrate workaround by copying to a char[] instead (and
// thus avoiding StringCharBuffer.
CharBuffer buff2 = CharBuffer.wrap( in.toCharArray() );
buff2.position( 3 );
CharBuffer slice2 = buff2.slice();
System.out.println( "Slice 2 position (correct): "
+ slice2.position() );
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Copy sequence to char[] as demonstrated in test case above. However this has copy performance penalty.
(Incident Review ID: 235819)
======================================================================