JDK-4511104 : (bf spec) CharBuffer.subSequence() specification incorrect
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_8
  • CPU: sparc
  • Submitted: 2001-10-04
  • Updated: 2009-04-17
  • Resolved: 2009-04-17
Related Reports
Duplicate :  
Relates :  
Description
The same bug was created earlier (bug#4415483). However it was closed as a duplicate of 4416536. Bug 4416536 could not be verified as StringCharBuffer is no longer a public class. Therefore the problem mentioned in 4415483 remains unaddressed. Thus this new bug is created. (As 4415483 couldnt be reopened)
 
Specification for CharBuffer.subSequence(int start, int end) says "new buffer's  capacity will be that of this buffer, its position will be position() + start,   and its limit will be position() + end."
But in actual new buffer's capacity, limit and position values are different     from what spec says.

import java.nio.*;
public class SubSequence {
   public static void main(String[] args) throws Exception {
     String source = "This is the string used to test subSequence() method";
     char[] charArray = source.toCharArray();
     CharBuffer c = CharBuffer.wrap(charArray);
     int position = 5;
     int start = 5;
     int end = 10;
     c.position(position);
     CharBuffer cNew = (CharBuffer)(c.subSequence(start,end));
     int pos = c.position()+start;
     int lim = c.position()+end;
     System.out.println("capacity: "+cNew.capacity()+" Expected:"+c.capacity());
     System.out.println("position: "+cNew.position()+" Expected: "+pos);
     System.out.println("limit   : "+cNew.limit()+" Expected: "+lim);
   }
}

Results in:
capacity: 5 Expected: 52
position: 0 Expected: 10
limit   : 5 Expected: 15


Comments
EVALUATION This method has an unfortuate history, see discussion at: http://mail.openjdk.java.net/pipermail/core-libs-dev/2009-April/001391.html The implementation has been corrected via 6795561 in jdk7. It is important to have this method right in jdk7 because the method has been updated to return a CharBuffer.
17-04-2009

EVALUATION The specification will be changed to say that the subSequence method works just like the slice method with respect to the buffer that is returned: The new buffer's position will be zero, its capacity and its limit will be end - start, and its mark will be undefined. -- ###@###.### 2001/10/9
10-09-0175