diff --git a/src/share/classes/java/nio/X-Buffer.java.template b/src/share/classes/java/nio/X-Buffer.java.template index 4150b1edcba0688240fec329b5028680fd7e30aa..322692428d70d476af9d727b050a82ff33d74188 100644 --- a/src/share/classes/java/nio/X-Buffer.java.template +++ b/src/share/classes/java/nio/X-Buffer.java.template @@ -741,6 +741,8 @@ public abstract class $Type$Buffer 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 abstract class $Type$Buffer */ 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++) diff --git a/test/java/nio/Buffer/Basic-X.java.template b/test/java/nio/Buffer/Basic-X.java.template index 54435c2c49f0951efff116507360b77f5b21c6a0..1548a9f8277057457aee06182a0aa0247c964137 100644 --- a/test/java/nio/Buffer/Basic-X.java.template +++ b/test/java/nio/Buffer/Basic-X.java.template @@ -335,7 +335,7 @@ public class Basic$Type$ 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 @@ public class Basic$Type$ 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 @@ public class Basic$Type$ 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 @@ public class Basic$Type$ #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() { diff --git a/test/java/nio/Buffer/Basic.java b/test/java/nio/Buffer/Basic.java index 86c1b4436123997e4637fdb5872d3ec68d4a2d58..0dba4c123b43bea0e892941fa262189fb6e477bf 100644 --- a/test/java/nio/Buffer/Basic.java +++ b/test/java/nio/Buffer/Basic.java @@ -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 */ diff --git a/test/java/nio/Buffer/BasicByte.java b/test/java/nio/Buffer/BasicByte.java index 34856fa14b4c6dced457a9a8c4699489a3687bcb..861a4335b5bfe351ebff4479fdccfc2db91dec69 100644 --- a/test/java/nio/Buffer/BasicByte.java +++ b/test/java/nio/Buffer/BasicByte.java @@ -335,7 +335,7 @@ public class BasicByte 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 @@ public class BasicByte 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 @@ public class BasicByte 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 @@ public class BasicByte + + + + + + + + + + + + + + + + if (rb.getClass().getName().startsWith("java.nio.Heap")) { tryCatch(b, ReadOnlyBufferException.class, new Runnable() { diff --git a/test/java/nio/Buffer/BasicChar.java b/test/java/nio/Buffer/BasicChar.java index d06772a961c7a923bcc609c4150a12374acde69b..b89916101bf95d6c5a7d60e25ca8705df64fd2d6 100644 --- a/test/java/nio/Buffer/BasicChar.java +++ b/test/java/nio/Buffer/BasicChar.java @@ -335,7 +335,7 @@ public class BasicChar 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 @@ public class BasicChar 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 @@ public class BasicChar 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(); @@ -741,6 +749,22 @@ public class BasicChar + + + + + + // 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); + }}); diff --git a/test/java/nio/Buffer/BasicDouble.java b/test/java/nio/Buffer/BasicDouble.java index 8ff9eb1f716ca464750a520d6e88c35cd39757d1..97efe8f6847a728c6c941cbe6a1d8d57c93082c2 100644 --- a/test/java/nio/Buffer/BasicDouble.java +++ b/test/java/nio/Buffer/BasicDouble.java @@ -335,7 +335,7 @@ public class BasicDouble 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 @@ public class BasicDouble 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 @@ public class BasicDouble 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(); @@ -727,6 +735,22 @@ public class BasicDouble + + + + + + + + + + + + + + + + diff --git a/test/java/nio/Buffer/BasicFloat.java b/test/java/nio/Buffer/BasicFloat.java index 98e1989f2a15b4845b3e56d9c4fe6b19b0ba2b66..2f954934a054de79a78ea7d39c5c7295a4fa47bd 100644 --- a/test/java/nio/Buffer/BasicFloat.java +++ b/test/java/nio/Buffer/BasicFloat.java @@ -335,7 +335,7 @@ public class BasicFloat 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 @@ public class BasicFloat 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 @@ public class BasicFloat 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(); @@ -727,6 +735,22 @@ public class BasicFloat + + + + + + + + + + + + + + + + diff --git a/test/java/nio/Buffer/BasicInt.java b/test/java/nio/Buffer/BasicInt.java index 571d62f61bc3aa9b2f6196205c336dc5eae55493..a9ce0fb450badb92fbae2b10efbe28f694834a2c 100644 --- a/test/java/nio/Buffer/BasicInt.java +++ b/test/java/nio/Buffer/BasicInt.java @@ -335,7 +335,7 @@ public class BasicInt 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 @@ public class BasicInt 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 @@ public class BasicInt 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(); @@ -727,6 +735,22 @@ public class BasicInt + + + + + + + + + + + + + + + + diff --git a/test/java/nio/Buffer/BasicLong.java b/test/java/nio/Buffer/BasicLong.java index 7ea5d0326ffb9cc1de6600d52e966190835c802f..82fb25b8a2d57f0ecfb13322ab1a10a7d5c92d55 100644 --- a/test/java/nio/Buffer/BasicLong.java +++ b/test/java/nio/Buffer/BasicLong.java @@ -335,7 +335,7 @@ public class BasicLong 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 @@ public class BasicLong 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 @@ public class BasicLong 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(); @@ -727,6 +735,22 @@ public class BasicLong + + + + + + + + + + + + + + + + diff --git a/test/java/nio/Buffer/BasicShort.java b/test/java/nio/Buffer/BasicShort.java index 176a53fe544501000cf8ae3e1b2b95f040a35c94..9f8579d7510b93cf203ee18196150af8ea90c466 100644 --- a/test/java/nio/Buffer/BasicShort.java +++ b/test/java/nio/Buffer/BasicShort.java @@ -335,7 +335,7 @@ public class BasicShort 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 @@ public class BasicShort 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 @@ public class BasicShort 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(); @@ -727,6 +735,22 @@ public class BasicShort + + + + + + + + + + + + + + + +