瀏覽代碼

Replace some bounds checking with Objects.checkFromIndexSize (#46953)

Part of #36108. Replace some manual bounds checks with a utility method,
from `Objects`.
Rory Hunter 6 年之前
父節點
當前提交
96ae2257ea

+ 2 - 2
server/src/main/java/org/elasticsearch/common/bytes/ByteBufferReference.java

@@ -20,9 +20,9 @@
 package org.elasticsearch.common.bytes;
 
 import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.FutureObjects;
 
 import java.nio.ByteBuffer;
+import java.util.Objects;
 
 /**
  * This is a {@link BytesReference} backed by a {@link ByteBuffer}. The byte buffer can either be a heap or
@@ -58,7 +58,7 @@ public class ByteBufferReference extends BytesReference {
 
     @Override
     public BytesReference slice(int from, int length) {
-        FutureObjects.checkFromIndexSize(from, length, this.length);
+        Objects.checkFromIndexSize(from, length, this.length);
         buffer.position(from);
         buffer.limit(from + length);
         ByteBufferReference newByteBuffer = new ByteBufferReference(buffer);

+ 3 - 4
server/src/main/java/org/elasticsearch/common/bytes/BytesArray.java

@@ -21,6 +21,8 @@ package org.elasticsearch.common.bytes;
 
 import org.apache.lucene.util.BytesRef;
 
+import java.util.Objects;
+
 public final class BytesArray extends BytesReference {
 
     public static final BytesArray EMPTY = new BytesArray(BytesRef.EMPTY_BYTES, 0, 0);
@@ -67,10 +69,7 @@ public final class BytesArray extends BytesReference {
 
     @Override
     public BytesReference slice(int from, int length) {
-        if (from < 0 || (from + length) > this.length) {
-            throw new IllegalArgumentException("can't slice a buffer with length [" + this.length +
-                "], with slice parameters from [" + from + "], length [" + length + "]");
-        }
+        Objects.checkFromIndexSize(from, length, this.length);
         return new BytesArray(bytes, offset + from, length);
     }
 

+ 1 - 2
server/src/main/java/org/elasticsearch/common/bytes/CompositeBytesReference.java

@@ -22,7 +22,6 @@ package org.elasticsearch.common.bytes;
 import org.apache.lucene.util.BytesRef;
 import org.apache.lucene.util.BytesRefBuilder;
 import org.apache.lucene.util.BytesRefIterator;
-import org.apache.lucene.util.FutureObjects;
 import org.apache.lucene.util.RamUsageEstimator;
 
 import java.io.IOException;
@@ -78,7 +77,7 @@ public final class CompositeBytesReference extends BytesReference {
 
     @Override
     public BytesReference slice(int from, int length) {
-        FutureObjects.checkFromIndexSize(from, length, this.length);
+        Objects.checkFromIndexSize(from, length, this.length);
 
         if (length == 0) {
             return BytesArray.EMPTY;

+ 2 - 4
server/src/main/java/org/elasticsearch/common/bytes/PagedBytesReference.java

@@ -25,6 +25,7 @@ import org.elasticsearch.common.util.ByteArray;
 import org.elasticsearch.common.util.PageCacheRecycler;
 
 import java.io.IOException;
+import java.util.Objects;
 
 /**
  * A page based bytes reference, internally holding the bytes in a paged
@@ -60,10 +61,7 @@ public class PagedBytesReference extends BytesReference {
 
     @Override
     public BytesReference slice(int from, int length) {
-        if (from < 0 || (from + length) > length()) {
-            throw new IllegalArgumentException("can't slice a buffer with length [" + length() +
-                "], with slice parameters from [" + from + "], length [" + length + "]");
-        }
+        Objects.checkFromIndexSize(from, length, this.length);
         return new PagedBytesReference(byteArray, offset + from, length);
     }