diff --git a/server/src/main/java/org/elasticsearch/common/bytes/BytesReferenceStreamInput.java b/server/src/main/java/org/elasticsearch/common/bytes/BytesReferenceStreamInput.java index fa006ca1c1085..2fca882724bbd 100644 --- a/server/src/main/java/org/elasticsearch/common/bytes/BytesReferenceStreamInput.java +++ b/server/src/main/java/org/elasticsearch/common/bytes/BytesReferenceStreamInput.java @@ -213,13 +213,6 @@ public void reset() throws IOException { } } - @Override - protected BytesReference readPagedBytesReference(int length) throws IOException { - final int offset = offset(); - skip(length); // advance stream - return bytesReference.copy(offset, length); - } - @Override public boolean markSupported() { return true; diff --git a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 6e906e2f66b45..1506b5ade7bb2 100644 --- a/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -164,31 +164,25 @@ public BytesReference readBytesReference(int length) throws IOException { if (length == 0) { return BytesArray.EMPTY; } else if (length < ByteSizeValue.ofMb(1).getBytes()) { - // if the length is small enough we can just copy the bytes in a single array + // if the bytes reference is small enough we can just copy the bytes in a single array byte[] bytes = new byte[length]; readBytes(bytes, 0, length); return new BytesArray(bytes, 0, length); } else { - return readPagedBytesReference(length); + // paginate the bytes reference to avoid allocating a single byte array that is too large + final BytesReference br = BytesReference.fromByteArray(BigArrays.NON_RECYCLING_INSTANCE.newByteArray(length), length); + final BytesRefIterator iterator = br.iterator(); + BytesRef bytesRef; + int offset = 0; + while ((bytesRef = iterator.next()) != null) { + final int len = Math.min(bytesRef.length, length - offset); + readBytes(bytesRef.bytes, bytesRef.offset, len); + offset += len; + } + return br; } } - /** - * Reads a bytes reference using pagination if necessary. - */ - protected BytesReference readPagedBytesReference(int length) throws IOException { - final BytesReference bytesReference = BytesReference.fromByteArray(BigArrays.NON_RECYCLING_INSTANCE.newByteArray(length), length); - final BytesRefIterator iterator = bytesReference.iterator(); - BytesRef bytesRef; - int offset = 0; - while ((bytesRef = iterator.next()) != null) { - final int len = Math.min(bytesRef.length, length - offset); - readBytes(bytesRef.bytes, bytesRef.offset, len); - offset += len; - } - return bytesReference; - } - public BytesRef readBytesRef() throws IOException { int length = readArraySize(); return readBytesRef(length);