6593946: (bf) X-Buffer.compact() does not discard mark as specified
Summary: InvalidMarkException now correctly thrown. Thanks to keiths@redhat.com for the bug report and initial fix.
Reviewed-by: sherman, darcy
--- a/jdk/src/share/classes/java/nio/Buffer.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/src/share/classes/java/nio/Buffer.java Tue Nov 25 19:26:54 2008 +0000
@@ -543,6 +543,10 @@
return mark;
}
+ final void discardMark() { // package-private
+ mark = -1;
+ }
+
static void checkBounds(int off, int len, int size) { // package-private
if ((off | len | (off + len) | (size - (off + len))) < 0)
throw new IndexOutOfBoundsException();
--- a/jdk/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java Tue Nov 25 19:26:54 2008 +0000
@@ -150,6 +150,7 @@
sb.compact();
position(rem);
limit(capacity());
+ discardMark();
return this;
#else[rw]
throw new ReadOnlyBufferException();
--- a/jdk/src/share/classes/java/nio/Direct-X-Buffer.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/src/share/classes/java/nio/Direct-X-Buffer.java Tue Nov 25 19:26:54 2008 +0000
@@ -365,6 +365,7 @@
unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$);
position(rem);
limit(capacity());
+ discardMark();
return this;
#else[rw]
throw new ReadOnlyBufferException();
--- a/jdk/src/share/classes/java/nio/Heap-X-Buffer.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/src/share/classes/java/nio/Heap-X-Buffer.java Tue Nov 25 19:26:54 2008 +0000
@@ -222,6 +222,7 @@
System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
position(remaining());
limit(capacity());
+ discardMark();
return this;
#else[rw]
throw new ReadOnlyBufferException();
--- a/jdk/test/java/nio/Buffer/Basic-X.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/Basic-X.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
#warn This file is preprocessed before being compiled
import java.nio.*;
+import java.lang.reflect.Method;
public class Basic$Type$
@@ -184,32 +185,57 @@
b.position(p);
}
+ private static void compact(Buffer b) {
+ try {
+ Class<?> cl = b.getClass();
+ Method m = cl.getDeclaredMethod("compact");
+ m.setAccessible(true);
+ m.invoke(b);
+ } catch (Exception e) {
+ fail(e.getMessage(), b);
+ }
+ }
+
+ private static void checkInvalidMarkException(final Buffer b) {
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.mark();
+ compact(b);
+ b.reset();
+ }});
+ }
+
private static void testViews(int level, ByteBuffer b, boolean direct) {
ShortBuffer sb = b.asShortBuffer();
BasicShort.test(level, sb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) });
+ checkInvalidMarkException(sb);
CharBuffer cb = b.asCharBuffer();
BasicChar.test(level, cb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) });
+ checkInvalidMarkException(cb);
IntBuffer ib = b.asIntBuffer();
BasicInt.test(level, ib, direct);
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
+ checkInvalidMarkException(ib);
LongBuffer lb = b.asLongBuffer();
BasicLong.test(level, lb, direct);
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
+ checkInvalidMarkException(lb);
FloatBuffer fb = b.asFloatBuffer();
BasicFloat.test(level, fb, direct);
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
+ checkInvalidMarkException(fb);
DoubleBuffer db = b.asDoubleBuffer();
BasicDouble.test(level, db, direct);
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
-
+ checkInvalidMarkException(db);
}
private static void testHet(int level, ByteBuffer b) {
@@ -288,8 +314,11 @@
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), ($type$)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/Basic.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/Basic.java Tue Nov 25 19:26:54 2008 +0000
@@ -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
+ * 6221101 6234263 6535542 6591971 6593946
* @author Mark Reinhold
*/
--- a/jdk/test/java/nio/Buffer/BasicByte.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicByte.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicByte
@@ -184,32 +185,57 @@
b.position(p);
}
+ private static void compact(Buffer b) {
+ try {
+ Class<?> cl = b.getClass();
+ Method m = cl.getDeclaredMethod("compact");
+ m.setAccessible(true);
+ m.invoke(b);
+ } catch (Exception e) {
+ fail(e.getMessage(), b);
+ }
+ }
+
+ private static void checkInvalidMarkException(final Buffer b) {
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.mark();
+ compact(b);
+ b.reset();
+ }});
+ }
+
private static void testViews(int level, ByteBuffer b, boolean direct) {
ShortBuffer sb = b.asShortBuffer();
BasicShort.test(level, sb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) });
+ checkInvalidMarkException(sb);
CharBuffer cb = b.asCharBuffer();
BasicChar.test(level, cb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) });
+ checkInvalidMarkException(cb);
IntBuffer ib = b.asIntBuffer();
BasicInt.test(level, ib, direct);
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
+ checkInvalidMarkException(ib);
LongBuffer lb = b.asLongBuffer();
BasicLong.test(level, lb, direct);
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
+ checkInvalidMarkException(lb);
FloatBuffer fb = b.asFloatBuffer();
BasicFloat.test(level, fb, direct);
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
+ checkInvalidMarkException(fb);
DoubleBuffer db = b.asDoubleBuffer();
BasicDouble.test(level, db, direct);
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
-
+ checkInvalidMarkException(db);
}
private static void testHet(int level, ByteBuffer b) {
@@ -288,8 +314,11 @@
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (byte)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicChar.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicChar.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicChar
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (char)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicDouble.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicDouble.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicDouble
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (double)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicFloat.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicFloat.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicFloat
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (float)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicInt.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicInt.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicInt
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (int)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicLong.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicLong.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicLong
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (long)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/BasicShort.java Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/BasicShort.java Tue Nov 25 19:26:54 2008 +0000
@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- //
import java.nio.*;
+import java.lang.reflect.Method;
public class BasicShort
@@ -283,13 +284,41 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
boolean caught = false;
try {
thunk.run();
} catch (Throwable x) {
- if (ex.isAssignableFrom(x.getClass()))
+ if (ex.isAssignableFrom(x.getClass())) {
caught = true;
+ } else {
+ fail(x.getMessage() + " not expected");
+ }
}
if (!caught)
fail(ex.getName() + " not thrown", b);
@@ -356,7 +385,6 @@
// Exceptions
- boolean caught = false;
relPut(b);
b.limit(b.capacity() / 2);
b.position(b.limit());
@@ -386,6 +414,14 @@
b.put(b.limit(), (short)42);
}});
+ tryCatch(b, InvalidMarkException.class, new Runnable() {
+ public void run() {
+ b.position(0);
+ b.mark();
+ b.compact();
+ b.reset();
+ }});
+
// Values
b.clear();
--- a/jdk/test/java/nio/Buffer/genBasic.sh Tue Nov 25 10:09:26 2008 -0800
+++ b/jdk/test/java/nio/Buffer/genBasic.sh Tue Nov 25 19:26:54 2008 +0000
@@ -23,7 +23,7 @@
# have any questions.
#
-javac -d . ../../../../make/tools/src/build/tools/spp/Spp.java > Spp.java
+javac -d . ../../../../make/tools/src/build/tools/spp/Spp.java
gen() {
java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -DFulltype=$3 <Basic-X.java >Basic$2.java