/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.bytes;

import org.apache.lucene.util.BytesRef;

import java.nio.ByteBuffer;

/**
 * This is a {@link BytesReference} backed by a {@link ByteBuffer}. The byte buffer can either be a heap or
 * direct byte buffer. The reference is composed of the space between the {@link ByteBuffer#position()} and
 * {@link ByteBuffer#limit()} at construction time. If the position or limit of the underlying byte buffer is
 * changed, those changes will not be reflected in this reference. However, modifying the limit or position
 * of the underlying byte buffer is not recommended as those can be used during {@link ByteBuffer#get()}
 * bounds checks. Use {@link ByteBuffer#duplicate()} at creation time if you plan on modifying the markers of
 * the underlying byte buffer. Any changes to the underlying data in the byte buffer will be reflected.
 */
public class ByteBufferReference extends BytesReference {

    private final ByteBuffer buffer;
    private final int offset;
    private final int length;

    public ByteBufferReference(ByteBuffer buffer) {
        this.buffer = buffer;
        this.offset = buffer.position();
        this.length = buffer.remaining();
    }

    @Override
    public byte get(int index) {
        return buffer.get(index + offset);
    }

    @Override
    public int length() {
        return length;
    }

    @Override
    public BytesReference slice(int from, int length) {
        if (from < 0 || (from + length) > this.length) {
            throw new IndexOutOfBoundsException("can't slice a buffer with length [" + this.length + "], with slice parameters from ["
                + from + "], length [" + length + "]");
        }
        ByteBuffer newByteBuffer = buffer.duplicate();
        newByteBuffer.position(offset + from);
        newByteBuffer.limit(offset + from + length);
        return new ByteBufferReference(newByteBuffer);
    }

    /**
     * This will return a bytes ref composed of the bytes. If this is a direct byte buffer, the bytes will
     * have to be copied.
     *
     * @return the bytes ref
     */
    @Override
    public BytesRef toBytesRef() {
        if (buffer.hasArray()) {
            return new BytesRef(buffer.array(), buffer.arrayOffset() + offset, length);
        }
        final byte[] copy = new byte[length];
        buffer.get(copy, offset, length);
        return new BytesRef(copy);
    }

    @Override
    public long ramBytesUsed() {
        return buffer.capacity();
    }
}

@@ -863,16 +863,61 @@
 ytesRef;
+%0Aimport org.apache.lucene.util.FutureObjects;
 %0A%0Aimport
@@ -1340,336 +1340,52 @@
 ce. 
-However, modifying the limit or position%0A * of the underlying byte buffer is not recommended as those can be used during %7B@link ByteBuffer#get()%7D%0A * bounds checks. Use %7B@link ByteBuffer#duplicate()%7D at creation time if you plan on modifying the markers of%0A * the underlying byte buffer. Any changes to the underlying data in the
+Any changes to the underlying data in the%0A *
  byt
@@ -1410,16 +1410,34 @@
 eflected
+ in this reference
 .%0A */%0Apu
@@ -1534,38 +1534,8 @@
 er;%0A
-    private final int offset;%0A
     
@@ -1561,31 +1561,24 @@
 ength;%0A%0A    
-public 
 ByteBufferRe
@@ -1638,47 +1638,14 @@
 ffer
-;%0A        this.offset = buffer.position
+.slice
 ();%0A
@@ -1771,25 +1771,16 @@
 et(index
- + offset
 );%0A    %7D
@@ -1933,41 +1933,54 @@
     
-if (from %3C 0 %7C%7C 
+FutureObjects.checkFromIndexSize
 (from
- +
+,
  length
-) %3E
+,
  thi
@@ -1988,29 +1988,26 @@
 .length)
- %7B%0A  
+;%0A
         
   throw 
@@ -2002,171 +2002,56 @@
     
-  throw new IndexOutOfBoundsException(%22can't slice a buffer with length %5B%22 + this.length + %22%5D, with slice parameters from %5B%22%0A                + from + %22%5D, length %5B%22
+buffer.position(from);%0A        buffer.limit(from
  + l
@@ -2059,27 +2059,11 @@
 ngth
- + %22%5D%22
 );%0A
-        %7D%0A
     
@@ -2076,16 +2076,25 @@
 teBuffer
+Reference
  newByte
@@ -2106,25 +2106,38 @@
 r = 
-buffer.duplicate(
+new ByteBufferReference(buffer
 );%0A 
@@ -2139,32 +2139,25 @@
 r);%0A        
-newByteB
+b
 uffer.positi
@@ -2159,29 +2159,17 @@
 osition(
-offset + from
+0
 );%0A     
@@ -2171,24 +2171,17 @@
         
-newByteB
+b
 uffer.li
@@ -2188,24 +2188,13 @@
 mit(
-offset + from + 
+this.
 leng
@@ -2212,25 +2212,24 @@
   return new
- 
 ByteBufferRe
@@ -2230,32 +2230,8 @@
 ffer
-Reference(newByteBuffer)
 ;%0A  
@@ -2574,17 +2574,8 @@
 et()
- + offset
 , le
@@ -2666,14 +2666,9 @@
 py, 
-offset
+0
 , le

Use ByteBuffer#slice in BytesReference wrapper (#36862)