JDK-4997655 : (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2004-02-20
  • Updated: 2017-05-16
  • Resolved: 2005-09-04
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 6
6 b51Fixed
Related Reports
Relates :  
Relates :  
Description
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) 
======================================================================

Comments
EVALUATION Yes, this is a bug. It only affects the wrap(CharSequence) method. If a char buffer is explicitly allocated or created by wrapping a char array then the slice() method works correctly. Fixing this would create a small incompatibility, but unfortunately there's no coherent way to salvage the situation since the slice() method works correctly most of the time. This bug should therefore be fixed in a feature release if at all possible. -- ###@###.### 2004/2/25 The bug can be reproduced too with the wrap(CharSequence, int, int) method, if the value for the parameter "start" is not 0. ###@###.### 2005-06-30 07:42:39 GMT
02-12-0191