Details
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
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
Attachments
Issue Links
- duplicates
-
JDK-6795561 (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
-
- Resolved
-
- relates to
-
JDK-4415483 (bf) buffer created by CharBuffer.subSequence() has different values for cap,lim
-
- Closed
-