JDK-6546113 : (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2007-04-13
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
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 JDK 7
6u10Fixed 7 b25Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux moonlight 2.6.20-14-generic #2 SMP Mon Apr 2 20:37:49 UTC 2007 i686 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
When slicing a CharSequence wrapping CharBuffer, the resulting CharBuffer appears to be incorrect. According to the specs, the resulting sliced buffer should start at the original buffer's position. So, slicing 'Hello World' at position 4 and limit 7 should create a buffer that has 'o W' in it. But instead it has 'Hel'. This is demonstrated by the following testcase. Wrapping a char array instead shows the expected result.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try attached test program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Wrapping the char sequence should yield 'o W'
ACTUAL -
Wrapping the char sequence yields 'Hel'

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.nio.CharBuffer;

public class TestCharSequenceBuffer {

  public static void main(String[] args) {
    CharBuffer cb = CharBuffer.wrap("Hello World");
    cb.position(4);
    cb.limit(7);
    CharBuffer sl = cb.slice();
    System.out.println("wrapping a CharSequence");
    System.out.println("expected: 'o', result: '" + sl.get() + "'");
    System.out.println("expected: ' ', result: '" + sl.get() + "'");
    System.out.println("expected: 'W', result: '" + sl.get() + "'");


    cb = CharBuffer.wrap("Hello World".toCharArray());
    cb.position(4);
    cb.limit(7);
    sl = cb.slice();
    System.out.println("wrapping a char[]");
    System.out.println("expected: 'o', result: '" + sl.get() + "'");
    System.out.println("expected: ' ', result: '" + sl.get() + "'");
    System.out.println("expected: 'W', result: '" + sl.get() + "'");
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Convert the char sequence to a char[] and wrap this.

Release Regression From : 5.0u10
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION See 4997655.
15-04-2007