8219876: (bf) Improve IndexOutOfBoundsException messages in $Type$Buffer classes
authorbpb
Fri, 15 Mar 2019 16:24:07 -0700
changeset 54151 d2f8b7b33013
parent 54150 5529640c5f67
child 54152 5274462d5725
8219876: (bf) Improve IndexOutOfBoundsException messages in $Type$Buffer classes Reviewed-by: alanb, rriggs
src/java.base/share/classes/java/nio/Buffer.java
src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template
src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template
src/java.base/share/classes/java/nio/StringCharBuffer.java
src/java.base/share/classes/java/nio/X-Buffer.java.template
--- a/src/java.base/share/classes/java/nio/Buffer.java	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Fri Mar 15 16:24:07 2019 -0700
@@ -731,11 +731,6 @@
         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();
-    }
-
     static {
         // setup access to this package in SharedSecrets
         SharedSecrets.setJavaNioAccess(
--- a/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template	Fri Mar 15 16:24:07 2019 -0700
@@ -206,8 +206,7 @@
 #if[char]
 
     public String toString(int start, int end) {
-        if ((end > limit()) || (start > end))
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromToIndex(start, end, limit());
         try {
             int len = end - start;
             char[] ca = new char[len];
@@ -232,8 +231,7 @@
         pos = (pos <= lim ? pos : lim);
         int len = lim - pos;
 
-        if ((start < 0) || (end > len) || (start > end))
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromToIndex(start, end, len);
         return new ByteBufferAsCharBuffer$RW$$BO$(bb,
                                                   -1,
                                                   pos + start,
--- a/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Mar 15 16:24:07 2019 -0700
@@ -290,7 +290,7 @@
     public $Type$Buffer get($type$[] dst, int offset, int length) {
 #if[rw]
         if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
-            checkBounds(offset, length, dst.length);
+            Objects.checkFromIndexSize(offset, length, dst.length);
             int pos = position();
             int lim = limit();
             assert (pos <= lim);
@@ -439,7 +439,7 @@
     public $Type$Buffer put($type$[] src, int offset, int length) {
 #if[rw]
         if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
-            checkBounds(offset, length, src.length);
+            Objects.checkFromIndexSize(offset, length, src.length);
             int pos = position();
             int lim = limit();
             assert (pos <= lim);
@@ -545,8 +545,7 @@
 #if[char]
 
     public String toString(int start, int end) {
-        if ((end > limit()) || (start > end))
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromToIndex(start, end, limit());
         try {
             int len = end - start;
             char[] ca = new char[len];
@@ -571,8 +570,7 @@
         pos = (pos <= lim ? pos : lim);
         int len = lim - pos;
 
-        if ((start < 0) || (end > len) || (start > end))
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromToIndex(start, end, len);
         return new DirectCharBuffer$RW$$BO$(this,
                                             -1,
                                             pos + start,
--- a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Fri Mar 15 16:24:07 2019 -0700
@@ -173,7 +173,7 @@
 #end[streamableType]
 
     public $Type$Buffer get($type$[] dst, int offset, int length) {
-        checkBounds(offset, length, dst.length);
+        Objects.checkFromIndexSize(offset, length, dst.length);
         int pos = position();
         if (length > limit() - pos)
             throw new BufferUnderflowException();
@@ -219,7 +219,7 @@
 
     public $Type$Buffer put($type$[] src, int offset, int length) {
 #if[rw]
-        checkBounds(offset, length, src.length);
+        Objects.checkFromIndexSize(offset, length, src.length);
         int pos = position();
         if (length > limit() - pos)
             throw new BufferOverflowException();
@@ -277,7 +277,7 @@
 
     public $Type$Buffer put(String src, int start, int end) {
         int length = end - start;
-        checkBounds(start, length, src.length());
+        Objects.checkFromIndexSize(start, length, src.length());
         if (isReadOnly())
             throw new ReadOnlyBufferException();
         int pos = position();
@@ -659,11 +659,8 @@
     // --- Methods to support CharSequence ---
 
     public CharBuffer subSequence(int start, int end) {
-        if ((start < 0)
-            || (end > length())
-            || (start > end))
-            throw new IndexOutOfBoundsException();
         int pos = position();
+        Objects.checkFromToIndex(start, end, limit() - pos);
         return new HeapCharBuffer$RW$(hb,
                                       -1,
                                       pos + start,
--- a/src/java.base/share/classes/java/nio/StringCharBuffer.java	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/StringCharBuffer.java	Fri Mar 15 16:24:07 2019 -0700
@@ -37,8 +37,7 @@
     StringCharBuffer(CharSequence s, int start, int end) { // package-private
         super(-1, start, end, s.length());
         int n = s.length();
-        if ((start < 0) || (start > n) || (end < start) || (end > n))
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromToIndex(start, end, n);
         str = s;
         this.isReadOnly = true;
     }
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Fri Mar 15 16:00:18 2019 -0400
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Fri Mar 15 16:24:07 2019 -0700
@@ -772,7 +772,7 @@
      *          parameters do not hold
      */
     public $Type$Buffer get($type$[] dst, int offset, int length) {
-        checkBounds(offset, length, dst.length);
+        Objects.checkFromIndexSize(offset, length, dst.length);
         if (length > remaining())
             throw new BufferUnderflowException();
         int end = offset + length;
@@ -996,7 +996,7 @@
      *          If this buffer is read-only
      */
     public $Type$Buffer put($type$[] src, int offset, int length) {
-        checkBounds(offset, length, src.length);
+        Objects.checkFromIndexSize(offset, length, src.length);
         if (length > remaining())
             throw new BufferOverflowException();
         int end = offset + length;
@@ -1176,7 +1176,7 @@
      *          If this buffer is read-only
      */
     public $Type$Buffer put(String src, int start, int end) {
-        checkBounds(start, end - start, src.length());
+        Objects.checkFromIndexSize(start, end - start, src.length());
         if (isReadOnly())
             throw new ReadOnlyBufferException();
         if (end - start > remaining())