Hi,

better late than never. Here's the revised patch for the
StringCharBuffer, fixing the following bug:

> > >>> http://bugs.sun.com/view_bug.do?bug_id=6546113

> > Unfortunately, the bug wasn't caught by existing tests 
> > so one thing that would be good to do as part of 6546113 is to add or 
> > improve the regression tests for char buffers that wrap a char sequence. 
> > Are you interested in doing that as part of the fix to this bug?

I added a couple of tests. See the attached patch.


> >  Anyway, 
> > your changes look like a reasonable fix for the get methods. Did you 
> > check the toString, subSequence, and duplicate methods in case they have 
> > the same problem?

I checked them (and added tests), and yes, they have the same problem.
Fixed in this revised patch.


-- 
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com   * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
diff -r aaf71f6c087f src/share/classes/java/nio/StringCharBuffer.java
--- a/src/share/classes/java/nio/StringCharBuffer.java	Mon Mar 10 16:45:39 2008 +0100
+++ b/src/share/classes/java/nio/StringCharBuffer.java	Mon Mar 10 23:31:47 2008 +0100
@@ -60,16 +60,9 @@ class StringCharBuffer                  
         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 @@ class StringCharBuffer                  
     }
 
     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 @@ class StringCharBuffer                  
     }
 
     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();
         }
diff -r aaf71f6c087f test/java/nio/Buffer/StringCharBufferSliceTest.java
--- a/test/java/nio/Buffer/StringCharBufferSliceTest.java	Mon Mar 10 16:45:39 2008 +0100
+++ b/test/java/nio/Buffer/StringCharBufferSliceTest.java	Mon Mar 10 23:31:47 2008 +0100
@@ -53,6 +53,57 @@ public class StringCharBufferSliceTest {
         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!");
     }
 

Reply via email to