6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
Reviewed-by: sherman, iris
--- a/jdk/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java Wed Apr 15 14:53:34 2009 +0100
@@ -196,10 +196,12 @@
if ((start < 0) || (end > len) || (start > end))
throw new IndexOutOfBoundsException();
- int sublen = end - start;
- int off = offset + ((pos + start) << $LG_BYTES_PER_VALUE$);
- assert (off >= 0);
- return new ByteBufferAsCharBuffer$RW$$BO$(bb, -1, 0, sublen, sublen, off);
+ return new ByteBufferAsCharBuffer$RW$$BO$(bb,
+ -1,
+ pos + start,
+ pos + end,
+ capacity(),
+ offset);
}
#end[char]
--- a/jdk/src/share/classes/java/nio/Direct-X-Buffer.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/src/share/classes/java/nio/Direct-X-Buffer.java Wed Apr 15 14:53:34 2009 +0100
@@ -412,10 +412,12 @@
if ((start < 0) || (end > len) || (start > end))
throw new IndexOutOfBoundsException();
- int sublen = end - start;
- int off = (pos + start) << $LG_BYTES_PER_VALUE$;
- assert (off >= 0);
- return new DirectCharBuffer$RW$$BO$(this, -1, 0, sublen, sublen, off);
+ return new DirectCharBuffer$RW$$BO$(this,
+ -1,
+ pos + start,
+ pos + end,
+ capacity(),
+ offset);
}
#end[char]
--- a/jdk/src/share/classes/java/nio/Heap-X-Buffer.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/src/share/classes/java/nio/Heap-X-Buffer.java Wed Apr 15 14:53:34 2009 +0100
@@ -572,10 +572,13 @@
|| (end > length())
|| (start > end))
throw new IndexOutOfBoundsException();
- int len = end - start;
+ int pos = position();
return new HeapCharBuffer$RW$(hb,
- -1, 0, len, len,
- offset + position() + start);
+ -1,
+ pos + start,
+ pos + end,
+ capacity(),
+ offset);
}
#end[char]
--- a/jdk/src/share/classes/java/nio/StringCharBuffer.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/src/share/classes/java/nio/StringCharBuffer.java Wed Apr 15 14:53:34 2009 +0100
@@ -102,10 +102,12 @@
public final CharBuffer subSequence(int start, int end) {
try {
int pos = position();
- return new StringCharBuffer(str, -1,
+ return new StringCharBuffer(str,
+ -1,
pos + checkIndex(start, pos),
pos + checkIndex(end, pos),
- remaining(), offset);
+ capacity(),
+ offset);
} catch (IllegalArgumentException x) {
throw new IndexOutOfBoundsException();
}
--- a/jdk/test/java/nio/Buffer/Basic-X.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/Basic-X.java Wed Apr 15 14:53:34 2009 +0100
@@ -365,8 +365,11 @@
b.position(2);
ck(b, b.charAt(1), 'd');
- CharBuffer c = (CharBuffer)b.subSequence(1, 4);
- ck(b, b.subSequence(1, 4).toString().equals("def"));
+ CharBuffer c = b.subSequence(1, 4);
+ ck(c, c.capacity(), b.capacity());
+ ck(c, c.position(), b.position()+1);
+ ck(c, c.limit(), b.position()+4);
+ ck(c, b.subSequence(1, 4).toString().equals("def"));
// 4938424
b.position(4);
@@ -722,6 +725,8 @@
ck(b, start, b.position());
ck(b, end, b.limit());
ck(b, s.length(), b.capacity());
+ b.position(6);
+ ck(b, b.subSequence(0,3).toString().equals("ghi"));
// The index, relative to the position, must be non-negative and
// smaller than remaining().
--- a/jdk/test/java/nio/Buffer/Basic.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/Basic.java Wed Apr 15 14:53:34 2009 +0100
@@ -25,7 +25,7 @@
* @summary Unit test for buffers
* @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
* 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 6231529
- * 6221101 6234263 6535542 6591971 6593946
+ * 6221101 6234263 6535542 6591971 6593946 6795561
* @author Mark Reinhold
*/
--- a/jdk/test/java/nio/Buffer/BasicByte.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicByte.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final byte [] ba) {
int offset = 47;
int length = 900;
--- a/jdk/test/java/nio/Buffer/BasicChar.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicChar.java Wed Apr 15 14:53:34 2009 +0100
@@ -365,8 +365,11 @@
b.position(2);
ck(b, b.charAt(1), 'd');
- CharBuffer c = (CharBuffer)b.subSequence(1, 4);
- ck(b, b.subSequence(1, 4).toString().equals("def"));
+ CharBuffer c = b.subSequence(1, 4);
+ ck(c, c.capacity(), b.capacity());
+ ck(c, c.position(), b.position()+1);
+ ck(c, c.limit(), b.position()+4);
+ ck(c, b.subSequence(1, 4).toString().equals("def"));
// 4938424
b.position(4);
@@ -722,6 +725,8 @@
ck(b, start, b.position());
ck(b, end, b.limit());
ck(b, s.length(), b.capacity());
+ b.position(6);
+ ck(b, b.subSequence(0,3).toString().equals("ghi"));
// The index, relative to the position, must be non-negative and
// smaller than remaining().
--- a/jdk/test/java/nio/Buffer/BasicDouble.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicDouble.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final double [] ba) {
int offset = 47;
int length = 900;
--- a/jdk/test/java/nio/Buffer/BasicFloat.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicFloat.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final float [] ba) {
int offset = 47;
int length = 900;
--- a/jdk/test/java/nio/Buffer/BasicInt.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicInt.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final int [] ba) {
int offset = 47;
int length = 900;
--- a/jdk/test/java/nio/Buffer/BasicLong.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicLong.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final long [] ba) {
int offset = 47;
int length = 900;
--- a/jdk/test/java/nio/Buffer/BasicShort.java Mon Apr 13 18:20:51 2009 -0700
+++ b/jdk/test/java/nio/Buffer/BasicShort.java Wed Apr 15 14:53:34 2009 +0100
@@ -375,6 +375,9 @@
+
+
+
// Compact
relPut(b);
@@ -786,6 +789,8 @@
+
+
public static void test(final short [] ba) {
int offset = 47;
int length = 900;