8065556: (bf) Buffer.position and other methods should include detail in IAE

Add messages to IAEs which have none.

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2015-07-24 11:52:30 -07:00
parent 8f15d5a0c2
commit 7cd090f230
12 changed files with 613 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -197,7 +197,7 @@ public abstract class Buffer {
//
Buffer(int mark, int pos, int lim, int cap) { // package-private
if (cap < 0)
throw new IllegalArgumentException("Negative capacity: " + cap);
throw createCapacityException(cap);
this.capacity = cap;
limit(lim);
position(pos);
@ -209,6 +209,34 @@ public abstract class Buffer {
}
}
/**
* Returns an {@code IllegalArgumentException} indicating that the source
* and target are the same {@code Buffer}. Intended for use in
* {@code put(src)} when the parameter is the {@code Buffer} on which the
* method is being invoked.
*
* @returns IllegalArgumentException
* With a message indicating equal source and target buffers
*/
static IllegalArgumentException createSameBufferException() {
return new IllegalArgumentException("The source buffer is this buffer");
}
/**
* Verify that the capacity is nonnegative.
*
* @param capacity
* The new buffer's capacity, in $type$s
*
* @throws IllegalArgumentException
* If the <tt>capacity</tt> is a negative integer
*/
static IllegalArgumentException createCapacityException(int capacity) {
assert capacity < 0 : "capacity expected to be negative";
return new IllegalArgumentException("capacity < 0: ("
+ capacity + " < 0)");
}
/**
* Returns this buffer's capacity.
*
@ -241,13 +269,35 @@ public abstract class Buffer {
* If the preconditions on <tt>newPosition</tt> do not hold
*/
public Buffer position(int newPosition) {
if ((newPosition > limit) || (newPosition < 0))
throw new IllegalArgumentException();
if (newPosition > limit | newPosition < 0)
throw createPositionException(newPosition);
position = newPosition;
if (mark > position) mark = -1;
return this;
}
/**
* Verify that {@code 0 < newPosition <= limit}
*
* @param newPosition
* The new position value
*
* @throws IllegalArgumentException
* If the specified position is out of bounds.
*/
private IllegalArgumentException createPositionException(int newPosition) {
String msg = null;
if (newPosition > limit) {
msg = "newPosition > limit: (" + newPosition + " > " + limit + ")";
} else { // assume negative
assert newPosition < 0 : "newPosition expected to be negative";
msg = "newPosition < 0: (" + newPosition + " < 0)";
}
return new IllegalArgumentException(msg);
}
/**
* Returns this buffer's limit.
*
@ -272,14 +322,36 @@ public abstract class Buffer {
* If the preconditions on <tt>newLimit</tt> do not hold
*/
public Buffer limit(int newLimit) {
if ((newLimit > capacity) || (newLimit < 0))
throw new IllegalArgumentException();
if (newLimit > capacity | newLimit < 0)
throw createLimitException(newLimit);
limit = newLimit;
if (position > limit) position = limit;
if (mark > limit) mark = -1;
return this;
}
/**
* Verify that {@code 0 < newLimit <= capacity}
*
* @param newLimit
* The new limit value
*
* @throws IllegalArgumentException
* If the specified limit is out of bounds.
*/
private IllegalArgumentException createLimitException(int newLimit) {
String msg = null;
if (newLimit > capacity) {
msg = "newLimit > capacity: (" + newLimit + " > " + capacity + ")";
} else { // assume negative
assert newLimit < 0 : "newLimit expected to be negative";
msg = "newLimit < 0: (" + newLimit + " < 0)";
}
return new IllegalArgumentException(msg);
}
/**
* Sets this buffer's mark at its position.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -314,7 +314,7 @@ class Direct$Type$Buffer$RW$$BO$
#if[rw]
if (src instanceof Direct$Type$Buffer$BO$) {
if (src == this)
throw new IllegalArgumentException();
throw createSameBufferException();
Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;
int spos = sb.position();

View File

@ -216,7 +216,7 @@ class Heap$Type$Buffer$RW$
#if[rw]
if (src instanceof Heap$Type$Buffer) {
if (src == this)
throw new IllegalArgumentException();
throw createSameBufferException();
Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
int n = sb.remaining();
if (n > remaining())

View File

@ -339,7 +339,7 @@ public abstract class $Type$Buffer
*/
public static $Type$Buffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
throw createCapacityException(capacity);
return new Heap$Type$Buffer(capacity, capacity);
}
@ -797,7 +797,7 @@ public abstract class $Type$Buffer
*/
public $Type$Buffer put($Type$Buffer src) {
if (src == this)
throw new IllegalArgumentException();
throw createSameBufferException();
if (isReadOnly())
throw new ReadOnlyBufferException();
int n = src.remaining();

View File

@ -128,6 +128,15 @@ public class Basic$Type$
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class Basic$Type$
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -502,7 +551,8 @@ public class Basic$Type$
ck(b, b.get(), -Float.MIN_VALUE);
ck(b, b.get(), Float.NEGATIVE_INFINITY);
ck(b, b.get(), Float.POSITIVE_INFINITY);
if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
if (Float.floatToRawIntBits(v = b.get()) !=
Float.floatToRawIntBits(Float.NaN))
fail(b, (long)Float.NaN, (long)v);
ck(b, b.get(), 0.91697687f);
#end[float]
@ -934,11 +984,27 @@ public class Basic$Type$
public void run() {
$Type$Buffer.allocate(-1);
}});
try {
$Type$Buffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}
#if[byte]
tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
public void run() {
$Type$Buffer.allocateDirect(-1);
}});
try {
$Type$Buffer.allocateDirect(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity direct buffer");
}
}
#end[byte]
}

View File

@ -128,6 +128,15 @@ public class BasicByte
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicByte
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -516,6 +565,7 @@ public class BasicByte
// Comparison
@ -934,11 +984,27 @@ public class BasicByte
public void run() {
ByteBuffer.allocate(-1);
}});
try {
ByteBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}
tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
public void run() {
ByteBuffer.allocateDirect(-1);
}});
try {
ByteBuffer.allocateDirect(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity direct buffer");
}
}
}

View File

@ -128,6 +128,15 @@ public class BasicChar
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicChar
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -516,6 +565,7 @@ public class BasicChar
// Comparison
@ -934,6 +984,22 @@ public class BasicChar
public void run() {
CharBuffer.allocate(-1);
}});
try {
CharBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}

View File

@ -128,6 +128,15 @@ public class BasicDouble
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicDouble
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -507,6 +556,7 @@ public class BasicDouble
ck(b, b.get(), -Double.MAX_VALUE);
ck(b, b.get(), -Double.MIN_VALUE);
ck(b, b.get(), Double.NEGATIVE_INFINITY);
@ -934,6 +984,22 @@ public class BasicDouble
public void run() {
DoubleBuffer.allocate(-1);
}});
try {
DoubleBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}

View File

@ -128,6 +128,15 @@ public class BasicFloat
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicFloat
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -502,7 +551,8 @@ public class BasicFloat
ck(b, b.get(), -Float.MIN_VALUE);
ck(b, b.get(), Float.NEGATIVE_INFINITY);
ck(b, b.get(), Float.POSITIVE_INFINITY);
if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
if (Float.floatToRawIntBits(v = b.get()) !=
Float.floatToRawIntBits(Float.NaN))
fail(b, (long)Float.NaN, (long)v);
ck(b, b.get(), 0.91697687f);
@ -934,6 +984,22 @@ public class BasicFloat
public void run() {
FloatBuffer.allocate(-1);
}});
try {
FloatBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}

View File

@ -128,6 +128,15 @@ public class BasicInt
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicInt
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -516,6 +565,7 @@ public class BasicInt
// Comparison
@ -934,6 +984,22 @@ public class BasicInt
public void run() {
IntBuffer.allocate(-1);
}});
try {
IntBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}

View File

@ -128,6 +128,15 @@ public class BasicLong
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicLong
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -516,6 +565,7 @@ public class BasicLong
// Comparison
@ -934,6 +984,22 @@ public class BasicLong
public void run() {
LongBuffer.allocate(-1);
}});
try {
LongBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}

View File

@ -128,6 +128,15 @@ public class BasicShort
c.position(7);
b.put(c);
b.flip();
try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
//6231529
@ -464,6 +473,46 @@ public class BasicShort
b.reset();
}});
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
try {
b.position(-1);
fail("IllegalArgumentException expected for negative position");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative position");
}
}
try {
b.limit(b.capacity() + 1);
fail("IllegalArgumentException expected for limit beyond capacity");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " limit beyond capacity");
}
}
try {
b.limit(-1);
fail("IllegalArgumentException expected for negative limit");
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " negative limit");
}
}
// Values
b.clear();
@ -516,6 +565,7 @@ public class BasicShort
// Comparison
@ -934,6 +984,22 @@ public class BasicShort
public void run() {
ShortBuffer.allocate(-1);
}});
try {
ShortBuffer.allocate(-1);
} catch (IllegalArgumentException e) {
if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}