--- old/src/java.base/share/classes/java/nio/Buffer.java 2015-03-23 16:12:46.000000000 -0700 +++ new/src/java.base/share/classes/java/nio/Buffer.java 2015-03-23 16:12:46.000000000 -0700 @@ -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 @@ -195,8 +195,9 @@ // after checking invariants. // Buffer(int mark, int pos, int lim, int cap) { // package-private - if (cap < 0) - throw new IllegalArgumentException("Negative capacity: " + cap); + if (cap < 0) { + throw negativeCapacityException(cap); + } this.capacity = cap; limit(lim); position(pos); @@ -240,14 +241,37 @@ * If the preconditions on newPosition do not hold */ public Buffer position(int newPosition) { - if ((newPosition > limit) || (newPosition < 0)) - throw new IllegalArgumentException(); + if ((newPosition > limit) || (newPosition < 0)) { + throw positionOutOfBoundsException(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 positionOutOfBoundsException(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. * * @return The limit of this buffer @@ -271,8 +295,9 @@ * If the preconditions on newLimit do not hold */ public Buffer limit(int newLimit) { - if ((newLimit > capacity) || (newLimit < 0)) - throw new IllegalArgumentException(); + if (newLimit > capacity || newLimit < 0) { + throw limitOutOfBoundsException(newLimit); + } limit = newLimit; if (position > limit) position = limit; if (mark > limit) mark = -1; @@ -280,6 +305,28 @@ } /** + * Verify that {@code 0 < newLimit <= capacity} + * + * @param newLimit + * The new limit value + * + * @throws IllegalArgumentException + * If the specified limit is out of bounds. + */ + private IllegalArgumentException limitOutOfBoundsException(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. * * @return This buffer @@ -485,7 +532,6 @@ */ public abstract boolean isDirect(); - // -- Package-private methods for bounds checking, etc. -- /** @@ -567,4 +613,35 @@ throw new IndexOutOfBoundsException(); } + /** + * Verify that the parameter is not this {@code Buffer}. Intended for + * checking that in {@code put(src)} the parameter is not the {@code Buffer} + * on which the method is being invoked. + * + * @param src + * The buffer to compare with. + * + * @throws IllegalArgumentException + * If the source buffer is this buffer + */ + void checkSourceBufferNotThisBuffer(Buffer src) { // package-private + if (src == this) { + throw 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 capacity is a negative integer + */ + static IllegalArgumentException negativeCapacityException(int capacity) { // package-private + assert capacity < 0 : "capacity expected to be negative"; + return new IllegalArgumentException("capacity < 0: (" + + capacity + " < 0)"); + } } --- old/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template 2015-03-23 16:12:47.000000000 -0700 +++ new/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template 2015-03-23 16:12:47.000000000 -0700 @@ -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 @@ -313,8 +313,7 @@ public $Type$Buffer put($Type$Buffer src) { #if[rw] if (src instanceof Direct$Type$Buffer$BO$) { - if (src == this) - throw new IllegalArgumentException(); + checkSourceBufferNotThisBuffer(src); Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src; int spos = sb.position(); --- old/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-23 16:12:48.000000000 -0700 +++ new/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-23 16:12:48.000000000 -0700 @@ -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 @@ -198,8 +198,7 @@ public $Type$Buffer put($Type$Buffer src) { #if[rw] if (src instanceof Heap$Type$Buffer) { - if (src == this) - throw new IllegalArgumentException(); + checkSourceBufferNotThisBuffer(src); Heap$Type$Buffer sb = (Heap$Type$Buffer)src; int n = sb.remaining(); if (n > remaining()) --- old/src/java.base/share/classes/java/nio/X-Buffer.java.template 2015-03-23 16:12:49.000000000 -0700 +++ new/src/java.base/share/classes/java/nio/X-Buffer.java.template 2015-03-23 16:12:49.000000000 -0700 @@ -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 @@ -330,8 +330,9 @@ * If the capacity is a negative integer */ public static $Type$Buffer allocate(int capacity) { - if (capacity < 0) - throw new IllegalArgumentException(); + if (capacity < 0) { + throw negativeCapacityException(capacity); + } return new Heap$Type$Buffer(capacity, capacity); } @@ -760,8 +761,7 @@ * If this buffer is read-only */ public $Type$Buffer put($Type$Buffer src) { - if (src == this) - throw new IllegalArgumentException(); + checkSourceBufferNotThisBuffer(src); if (isReadOnly()) throw new ReadOnlyBufferException(); int n = src.remaining(); --- old/test/java/nio/Buffer/Basic-X.java.template 2015-03-23 16:12:50.000000000 -0700 +++ new/test/java/nio/Buffer/Basic-X.java.template 2015-03-23 16:12:50.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2012, 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 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,11 +978,25 @@ 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 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 attempt to allocate negative capacity direct buffer"); + } + } #end[byte] } --- old/test/java/nio/Buffer/BasicByte.java 2015-03-23 16:12:51.000000000 -0700 +++ new/test/java/nio/Buffer/BasicByte.java 2015-03-23 16:12:51.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,11 +978,25 @@ public void run() { ByteBuffer.allocate(-1); }}); + try { + ByteBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected 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 attempt to allocate negative capacity direct buffer"); + } + } } --- old/test/java/nio/Buffer/BasicChar.java 2015-03-23 16:12:52.000000000 -0700 +++ new/test/java/nio/Buffer/BasicChar.java 2015-03-23 16:12:52.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { CharBuffer.allocate(-1); }}); + try { + CharBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + + --- old/test/java/nio/Buffer/BasicDouble.java 2015-03-23 16:12:53.000000000 -0700 +++ new/test/java/nio/Buffer/BasicDouble.java 2015-03-23 16:12:53.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { DoubleBuffer.allocate(-1); }}); + try { + DoubleBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + + --- old/test/java/nio/Buffer/BasicFloat.java 2015-03-23 16:12:54.000000000 -0700 +++ new/test/java/nio/Buffer/BasicFloat.java 2015-03-23 16:12:54.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { FloatBuffer.allocate(-1); }}); + try { + FloatBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + + --- old/test/java/nio/Buffer/BasicInt.java 2015-03-23 16:12:55.000000000 -0700 +++ new/test/java/nio/Buffer/BasicInt.java 2015-03-23 16:12:55.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { IntBuffer.allocate(-1); }}); + try { + IntBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + + --- old/test/java/nio/Buffer/BasicLong.java 2015-03-23 16:12:56.000000000 -0700 +++ new/test/java/nio/Buffer/BasicLong.java 2015-03-23 16:12:56.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { LongBuffer.allocate(-1); }}); + try { + LongBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + + --- old/test/java/nio/Buffer/BasicShort.java 2015-03-23 16:12:57.000000000 -0700 +++ new/test/java/nio/Buffer/BasicShort.java 2015-03-23 16:12:57.000000000 -0700 @@ -128,6 +128,14 @@ c.position(7); b.put(c); b.flip(); + try { + b.put(b); + fail("IllegalArgumentException expected for putting into same buffer"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected from putting into same buffer"); + } + } } //6231529 @@ -464,6 +472,42 @@ b.reset(); }}); + try { + b.position(b.limit() + 1); + fail("IllegalArgumentException expected for setting position beyond limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting position beyond limit"); + } + } + + try { + b.position(-1); + fail("IllegalArgumentException expected for setting negative position"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative position"); + } + } + + try { + b.limit(b.capacity() + 1); + fail("IllegalArgumentException expected for setting limit beyond capacity"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity"); + } + } + + try { + b.limit(-1); + fail("IllegalArgumentException expected for setting negative limit"); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected for setting negative limit"); + } + } + // Values b.clear(); @@ -934,6 +978,20 @@ public void run() { ShortBuffer.allocate(-1); }}); + try { + ShortBuffer.allocate(-1); + } catch (IllegalArgumentException e) { + if (e.getMessage() == null) { + fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer"); + } + } + + + + + + +