7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
authoralanb
Fri, 21 Sep 2012 15:39:10 +0100
changeset 14001 e8e8413ad49a
parent 14000 bd8282a5e0ab
child 14002 23126368d790
7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer Reviewed-by: iris, dxu, chegar
jdk/src/share/classes/java/nio/X-Buffer.java.template
jdk/test/java/nio/Buffer/Basic-X.java.template
jdk/test/java/nio/Buffer/Basic.java
jdk/test/java/nio/Buffer/BasicByte.java
jdk/test/java/nio/Buffer/BasicChar.java
jdk/test/java/nio/Buffer/BasicDouble.java
jdk/test/java/nio/Buffer/BasicFloat.java
jdk/test/java/nio/Buffer/BasicInt.java
jdk/test/java/nio/Buffer/BasicLong.java
jdk/test/java/nio/Buffer/BasicShort.java
--- a/jdk/src/share/classes/java/nio/X-Buffer.java.template	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/src/share/classes/java/nio/X-Buffer.java.template	Fri Sep 21 15:39:10 2012 +0100
@@ -741,6 +741,8 @@
     public $Type$Buffer put($Type$Buffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
@@ -888,6 +890,8 @@
      */
     public $Type$Buffer put(String src, int start, int end) {
         checkBounds(start, end - start, src.length());
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         if (end - start > remaining())
             throw new BufferOverflowException();
         for (int i = start; i < end; i++)
--- a/jdk/test/java/nio/Buffer/Basic-X.java.template	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/Basic-X.java.template	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch($type$ [] t, Class ex, Runnable thunk) {
+    private static void tryCatch($type$ [] t, Class<?> ex, Runnable thunk) {
         tryCatch($Type$Buffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put($Type$Buffer) should not change source position
+        final $Type$Buffer src = $Type$Buffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 #end[byte]
 
+#if[char]
+
+        // 7199551
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+            public void run() {
+                String s = new String(new char[rb.remaining() + 1]);
+                rb.put(s);
+            }});
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+            public void run() {
+                String s = new String(new char[rb.remaining() + 1]);
+                rb.append(s);
+            }});
+
+#end[char]
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/Basic.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/Basic.java	Fri Sep 21 15:39:10 2012 +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 6795561 7190219
+ *      6221101 6234263 6535542 6591971 6593946 6795561 7190219 7199551
  * @author Mark Reinhold
  */
 
--- a/jdk/test/java/nio/Buffer/BasicByte.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicByte.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(byte [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(byte [] t, Class<?> ex, Runnable thunk) {
         tryCatch(ByteBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(ByteBuffer) should not change source position
+        final ByteBuffer src = ByteBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicChar.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicChar.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(char [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(char [] t, Class<?> ex, Runnable thunk) {
         tryCatch(CharBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(CharBuffer) should not change source position
+        final CharBuffer src = CharBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+        // 7199551
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+            public void run() {
+                String s = new String(new char[rb.remaining() + 1]);
+                rb.put(s);
+            }});
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+            public void run() {
+                String s = new String(new char[rb.remaining() + 1]);
+                rb.append(s);
+            }});
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicDouble.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicDouble.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(double [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(double [] t, Class<?> ex, Runnable thunk) {
         tryCatch(DoubleBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(DoubleBuffer) should not change source position
+        final DoubleBuffer src = DoubleBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicFloat.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicFloat.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(float [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(float [] t, Class<?> ex, Runnable thunk) {
         tryCatch(FloatBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(FloatBuffer) should not change source position
+        final FloatBuffer src = FloatBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicInt.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicInt.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(int [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(int [] t, Class<?> ex, Runnable thunk) {
         tryCatch(IntBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(IntBuffer) should not change source position
+        final IntBuffer src = IntBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicLong.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicLong.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(long [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(long [] t, Class<?> ex, Runnable thunk) {
         tryCatch(LongBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(LongBuffer) should not change source position
+        final LongBuffer src = LongBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
--- a/jdk/test/java/nio/Buffer/BasicShort.java	Thu Sep 20 13:01:01 2012 -0700
+++ b/jdk/test/java/nio/Buffer/BasicShort.java	Fri Sep 21 15:39:10 2012 +0100
@@ -335,7 +335,7 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
-    private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
+    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
             thunk.run();
@@ -350,7 +350,7 @@
             fail(ex.getName() + " not thrown", b);
     }
 
-    private static void tryCatch(short [] t, Class ex, Runnable thunk) {
+    private static void tryCatch(short [] t, Class<?> ex, Runnable thunk) {
         tryCatch(ShortBuffer.wrap(t), ex, thunk);
     }
 
@@ -681,6 +681,14 @@
                     bulkPutBuffer(rb);
                 }});
 
+        // put(ShortBuffer) should not change source position
+        final ShortBuffer src = ShortBuffer.allocate(1);
+        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
+                public void run() {
+                    rb.put(src);
+                 }});
+        ck(src, src.position(), 0);
+
         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
                 public void run() {
                     rb.compact();
@@ -744,6 +752,22 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 
             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {