提交 37f4867b 编写于 作者: A alanb

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
上级 fca00a51
...@@ -543,6 +543,10 @@ public abstract class Buffer { ...@@ -543,6 +543,10 @@ public abstract class Buffer {
return mark; return mark;
} }
final void discardMark() { // package-private
mark = -1;
}
static void checkBounds(int off, int len, int size) { // package-private static void checkBounds(int off, int len, int size) { // package-private
if ((off | len | (off + len) | (size - (off + len))) < 0) if ((off | len | (off + len) | (size - (off + len))) < 0)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
......
...@@ -150,6 +150,7 @@ class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private ...@@ -150,6 +150,7 @@ class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private
sb.compact(); sb.compact();
position(rem); position(rem);
limit(capacity()); limit(capacity());
discardMark();
return this; return this;
#else[rw] #else[rw]
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
......
...@@ -365,6 +365,7 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -365,6 +365,7 @@ class Direct$Type$Buffer$RW$$BO$
unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$); unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$);
position(rem); position(rem);
limit(capacity()); limit(capacity());
discardMark();
return this; return this;
#else[rw] #else[rw]
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
......
...@@ -222,6 +222,7 @@ class Heap$Type$Buffer$RW$ ...@@ -222,6 +222,7 @@ class Heap$Type$Buffer$RW$
System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
position(remaining()); position(remaining());
limit(capacity()); limit(capacity());
discardMark();
return this; return this;
#else[rw] #else[rw]
throw new ReadOnlyBufferException(); throw new ReadOnlyBufferException();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#warn This file is preprocessed before being compiled #warn This file is preprocessed before being compiled
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class Basic$Type$ public class Basic$Type$
...@@ -184,32 +185,57 @@ public class Basic$Type$ ...@@ -184,32 +185,57 @@ public class Basic$Type$
b.position(p); 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) { private static void testViews(int level, ByteBuffer b, boolean direct) {
ShortBuffer sb = b.asShortBuffer(); ShortBuffer sb = b.asShortBuffer();
BasicShort.test(level, sb, direct); BasicShort.test(level, sb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, (byte)ic(0) });
checkInvalidMarkException(sb);
CharBuffer cb = b.asCharBuffer(); CharBuffer cb = b.asCharBuffer();
BasicChar.test(level, cb, direct); BasicChar.test(level, cb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, (byte)ic(0) });
checkInvalidMarkException(cb);
IntBuffer ib = b.asIntBuffer(); IntBuffer ib = b.asIntBuffer();
BasicInt.test(level, ib, direct); BasicInt.test(level, ib, direct);
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
checkInvalidMarkException(ib);
LongBuffer lb = b.asLongBuffer(); LongBuffer lb = b.asLongBuffer();
BasicLong.test(level, lb, direct); BasicLong.test(level, lb, direct);
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
checkInvalidMarkException(lb);
FloatBuffer fb = b.asFloatBuffer(); FloatBuffer fb = b.asFloatBuffer();
BasicFloat.test(level, fb, direct); BasicFloat.test(level, fb, direct);
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
checkInvalidMarkException(fb);
DoubleBuffer db = b.asDoubleBuffer(); DoubleBuffer db = b.asDoubleBuffer();
BasicDouble.test(level, db, direct); BasicDouble.test(level, db, direct);
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
checkInvalidMarkException(db);
} }
private static void testHet(int level, ByteBuffer b) { private static void testHet(int level, ByteBuffer b) {
...@@ -288,8 +314,11 @@ public class Basic$Type$ ...@@ -288,8 +314,11 @@ public class Basic$Type$
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class Basic$Type$ ...@@ -356,7 +385,6 @@ public class Basic$Type$
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class Basic$Type$ ...@@ -386,6 +414,14 @@ public class Basic$Type$
b.put(b.limit(), ($type$)42); 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 // Values
b.clear(); b.clear();
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
* @summary Unit test for buffers * @summary Unit test for buffers
* @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725 * @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
* 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 6231529 * 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 6231529
* 6221101 6234263 6535542 6591971 * 6221101 6234263 6535542 6591971 6593946
* @author Mark Reinhold * @author Mark Reinhold
*/ */
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicByte public class BasicByte
...@@ -184,32 +185,57 @@ public class BasicByte ...@@ -184,32 +185,57 @@ public class BasicByte
b.position(p); 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) { private static void testViews(int level, ByteBuffer b, boolean direct) {
ShortBuffer sb = b.asShortBuffer(); ShortBuffer sb = b.asShortBuffer();
BasicShort.test(level, sb, direct); BasicShort.test(level, sb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, (byte)ic(0) });
checkInvalidMarkException(sb);
CharBuffer cb = b.asCharBuffer(); CharBuffer cb = b.asCharBuffer();
BasicChar.test(level, cb, direct); BasicChar.test(level, cb, direct);
checkBytes(b, new byte[] { 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, (byte)ic(0) });
checkInvalidMarkException(cb);
IntBuffer ib = b.asIntBuffer(); IntBuffer ib = b.asIntBuffer();
BasicInt.test(level, ib, direct); BasicInt.test(level, ib, direct);
checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
checkInvalidMarkException(ib);
LongBuffer lb = b.asLongBuffer(); LongBuffer lb = b.asLongBuffer();
BasicLong.test(level, lb, direct); BasicLong.test(level, lb, direct);
checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) }); checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
checkInvalidMarkException(lb);
FloatBuffer fb = b.asFloatBuffer(); FloatBuffer fb = b.asFloatBuffer();
BasicFloat.test(level, fb, direct); BasicFloat.test(level, fb, direct);
checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 }); checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
checkInvalidMarkException(fb);
DoubleBuffer db = b.asDoubleBuffer(); DoubleBuffer db = b.asDoubleBuffer();
BasicDouble.test(level, db, direct); BasicDouble.test(level, db, direct);
checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 }); checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
checkInvalidMarkException(db);
} }
private static void testHet(int level, ByteBuffer b) { private static void testHet(int level, ByteBuffer b) {
...@@ -288,8 +314,11 @@ public class BasicByte ...@@ -288,8 +314,11 @@ public class BasicByte
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicByte ...@@ -356,7 +385,6 @@ public class BasicByte
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicByte ...@@ -386,6 +414,14 @@ public class BasicByte
b.put(b.limit(), (byte)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicChar public class BasicChar
...@@ -262,6 +263,31 @@ public class BasicChar ...@@ -262,6 +263,31 @@ public class BasicChar
...@@ -288,8 +314,11 @@ public class BasicChar ...@@ -288,8 +314,11 @@ public class BasicChar
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicChar ...@@ -356,7 +385,6 @@ public class BasicChar
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicChar ...@@ -386,6 +414,14 @@ public class BasicChar
b.put(b.limit(), (char)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicDouble public class BasicDouble
...@@ -262,6 +263,31 @@ public class BasicDouble ...@@ -262,6 +263,31 @@ public class BasicDouble
...@@ -288,8 +314,11 @@ public class BasicDouble ...@@ -288,8 +314,11 @@ public class BasicDouble
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicDouble ...@@ -356,7 +385,6 @@ public class BasicDouble
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicDouble ...@@ -386,6 +414,14 @@ public class BasicDouble
b.put(b.limit(), (double)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicFloat public class BasicFloat
...@@ -262,6 +263,31 @@ public class BasicFloat ...@@ -262,6 +263,31 @@ public class BasicFloat
...@@ -288,8 +314,11 @@ public class BasicFloat ...@@ -288,8 +314,11 @@ public class BasicFloat
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicFloat ...@@ -356,7 +385,6 @@ public class BasicFloat
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicFloat ...@@ -386,6 +414,14 @@ public class BasicFloat
b.put(b.limit(), (float)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicInt public class BasicInt
...@@ -262,6 +263,31 @@ public class BasicInt ...@@ -262,6 +263,31 @@ public class BasicInt
...@@ -288,8 +314,11 @@ public class BasicInt ...@@ -288,8 +314,11 @@ public class BasicInt
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicInt ...@@ -356,7 +385,6 @@ public class BasicInt
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicInt ...@@ -386,6 +414,14 @@ public class BasicInt
b.put(b.limit(), (int)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicLong public class BasicLong
...@@ -262,6 +263,31 @@ public class BasicLong ...@@ -262,6 +263,31 @@ public class BasicLong
...@@ -288,8 +314,11 @@ public class BasicLong ...@@ -288,8 +314,11 @@ public class BasicLong
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicLong ...@@ -356,7 +385,6 @@ public class BasicLong
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicLong ...@@ -386,6 +414,14 @@ public class BasicLong
b.put(b.limit(), (long)42); 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 // Values
b.clear(); b.clear();
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
// -- This file was mechanically generated: Do not edit! -- // // -- This file was mechanically generated: Do not edit! -- //
import java.nio.*; import java.nio.*;
import java.lang.reflect.Method;
public class BasicShort public class BasicShort
...@@ -262,6 +263,31 @@ public class BasicShort ...@@ -262,6 +263,31 @@ public class BasicShort
...@@ -288,8 +314,11 @@ public class BasicShort ...@@ -288,8 +314,11 @@ public class BasicShort
try { try {
thunk.run(); thunk.run();
} catch (Throwable x) { } catch (Throwable x) {
if (ex.isAssignableFrom(x.getClass())) if (ex.isAssignableFrom(x.getClass())) {
caught = true; caught = true;
} else {
fail(x.getMessage() + " not expected");
}
} }
if (!caught) if (!caught)
fail(ex.getName() + " not thrown", b); fail(ex.getName() + " not thrown", b);
...@@ -356,7 +385,6 @@ public class BasicShort ...@@ -356,7 +385,6 @@ public class BasicShort
// Exceptions // Exceptions
boolean caught = false;
relPut(b); relPut(b);
b.limit(b.capacity() / 2); b.limit(b.capacity() / 2);
b.position(b.limit()); b.position(b.limit());
...@@ -386,6 +414,14 @@ public class BasicShort ...@@ -386,6 +414,14 @@ public class BasicShort
b.put(b.limit(), (short)42); 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 // Values
b.clear(); b.clear();
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
# have any questions. # 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() { gen() {
java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -DFulltype=$3 <Basic-X.java >Basic$2.java java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -DFulltype=$3 <Basic-X.java >Basic$2.java
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册