changeset 46:c73cb47fe250

6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position Reviewed-by: iris Contributed-by: roman.kennke@aicas.com
author alanb
date Thu, 13 Mar 2008 19:34:49 +0000
parents 7b28e857d36c
children 547c14448b74
files src/share/classes/java/nio/StringCharBuffer.java test/java/nio/Buffer/StringCharBufferSliceTest.java
diffstat 2 files changed, 58 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/StringCharBuffer.java	Thu Mar 13 19:29:41 2008 +0000
+++ b/src/share/classes/java/nio/StringCharBuffer.java	Thu Mar 13 19:34:49 2008 +0000
@@ -60,16 +60,9 @@
         str = s;
     }
 
-    private StringCharBuffer(CharSequence s, int mark,
-                             int pos, int limit, int cap)
-    {
-        super(mark, pos, limit, cap);
-        str = s;
-    }
-
     public CharBuffer duplicate() {
         return new StringCharBuffer(str, markValue(),
-                                    position(), limit(), capacity());
+                                    position(), limit(), capacity(), offset);
     }
 
     public CharBuffer asReadOnlyBuffer() {
@@ -77,11 +70,11 @@
     }
 
     public final char get() {
-        return str.charAt(nextGetIndex());
+        return str.charAt(nextGetIndex() + offset);
     }
 
     public final char get(int index) {
-        return str.charAt(checkIndex(index));
+        return str.charAt(checkIndex(index) + offset);
     }
 
     // ## Override bulk get methods for better performance
@@ -103,15 +96,16 @@
     }
 
     final String toString(int start, int end) {
-        return str.toString().substring(start, end);
+        return str.toString().substring(start + offset, end + offset);
     }
 
     public final CharSequence subSequence(int start, int end) {
         try {
             int pos = position();
-            return new StringCharBuffer(str,
+            return new StringCharBuffer(str, -1,
                                         pos + checkIndex(start, pos),
-                                        pos + checkIndex(end, pos));
+                                        pos + checkIndex(end, pos),
+                                        remaining(), offset);
         } catch (IllegalArgumentException x) {
             throw new IndexOutOfBoundsException();
         }
--- a/test/java/nio/Buffer/StringCharBufferSliceTest.java	Thu Mar 13 19:29:41 2008 +0000
+++ b/test/java/nio/Buffer/StringCharBufferSliceTest.java	Thu Mar 13 19:34:49 2008 +0000
@@ -53,6 +53,57 @@
         buff = CharBuffer.wrap(in, 3, in.length());
         test(buff, buff.slice());
 
+        System.out.println(
+            ">>> StringCharBufferSliceTest-main: testing slice result with get()");
+        buff.position(4);
+        buff.limit(7);
+        CharBuffer slice = buff.slice();
+        for (int i = 0; i < 3; i++) {
+            if (slice.get() != buff.get()) {
+                throw new RuntimeException("Wrong characters in slice result.");
+            }
+        }
+
+        System.out.println(
+            ">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
+        buff.position(4);
+        buff.limit(7);
+        slice = buff.slice();
+        for (int i = 0; i < 3; i++) {
+            if (slice.get(i) != buff.get(4 + i)) {
+                throw new RuntimeException("Wrong characters in slice result.");
+            }
+        }
+
+        System.out.println(
+          ">>> StringCharBufferSliceTest-main: testing toString.");
+        buff.position(4);
+        buff.limit(7);
+        slice = buff.slice();
+        if (! slice.toString().equals("tes")) {
+            throw new RuntimeException("bad toString() after slice(): " + slice.toString());
+        }
+
+        System.out.println(
+          ">>> StringCharBufferSliceTest-main: testing subSequence.");
+        buff.position(4);
+        buff.limit(8);
+        slice = buff.slice();
+        CharSequence subSeq = slice.subSequence(1, 3);
+        if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
+            throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
+        }
+
+        System.out.println(
+          ">>> StringCharBufferSliceTest-main: testing duplicate.");
+        buff.position(4);
+        buff.limit(8);
+        slice = buff.slice();
+        CharBuffer dupe = slice.duplicate();
+        if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
+            || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
+            throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
+        }
         System.out.println(">>> StringCharBufferSliceTest-main: done!");
     }