United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6546113 (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
JDK-6546113 : (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position

Details
Type:
Bug
Submit Date:
2007-04-13
Status:
Closed
Updated Date:
2011-05-18
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
core-libs
OS:
linux
Sub-Component:
java.nio
CPU:
x86
Priority:
P2
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Relates:
Relates:

Sub Tasks

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.
                                     
2007-04-15



Hardware and Software, Engineered to Work Together