Class ReleasableBytesReference

java.lang.Object
org.elasticsearch.common.bytes.ReleasableBytesReference
All Implemented Interfaces:
Closeable, AutoCloseable, Comparable<BytesReference>, BytesReference, RefCounted, Releasable, ToXContent, ToXContentFragment

public final class ReleasableBytesReference extends Object implements RefCounted, Releasable, BytesReference
An extension to BytesReference that requires releasing its content. This class exists to make it explicit when a bytes reference needs to be released, and when not.
  • Constructor Details

  • Method Details

    • empty

      public static ReleasableBytesReference empty()
    • wrap

      public static ReleasableBytesReference wrap(BytesReference reference)
    • incRef

      public void incRef()
      Specified by:
      incRef in interface RefCounted
    • tryIncRef

      public boolean tryIncRef()
      Specified by:
      tryIncRef in interface RefCounted
    • decRef

      public boolean decRef()
      Specified by:
      decRef in interface RefCounted
    • hasReferences

      public boolean hasReferences()
      Specified by:
      hasReferences in interface RefCounted
    • retain

      public ReleasableBytesReference retain()
    • retainedSlice

      public ReleasableBytesReference retainedSlice(int from, int length)
      Same as slice(int, int) except that the slice is not guaranteed to share the same underlying reference count as this instance. This method is equivalent to calling .slice(from, length).retain() but might be more efficient through the avoidance of retaining unnecessary buffers.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Releasable
    • get

      public byte get(int index)
      Description copied from interface: BytesReference
      Returns the byte at the specified index. Need to be between 0 and length.
      Specified by:
      get in interface BytesReference
    • getInt

      public int getInt(int index)
      Description copied from interface: BytesReference
      Returns the integer read from the 4 bytes (BE) starting at the given index.
      Specified by:
      getInt in interface BytesReference
    • getIntLE

      public int getIntLE(int index)
      Description copied from interface: BytesReference
      Returns the integer read from the 4 bytes (LE) starting at the given index.
      Specified by:
      getIntLE in interface BytesReference
    • getLongLE

      public long getLongLE(int index)
      Description copied from interface: BytesReference
      Returns the long read from the 8 bytes (LE) starting at the given index.
      Specified by:
      getLongLE in interface BytesReference
    • getDoubleLE

      public double getDoubleLE(int index)
      Description copied from interface: BytesReference
      Returns the double read from the 8 bytes (LE) starting at the given index.
      Specified by:
      getDoubleLE in interface BytesReference
    • indexOf

      public int indexOf(byte marker, int from)
      Description copied from interface: BytesReference
      Finds the index of the first occurrence of the given marker between within the given bounds.
      Specified by:
      indexOf in interface BytesReference
      Parameters:
      marker - marker byte to search
      from - lower bound for the index to check (inclusive)
      Returns:
      first index of the marker or -1 if not found
    • length

      public int length()
      Description copied from interface: BytesReference
      The length.
      Specified by:
      length in interface BytesReference
    • slice

      public ReleasableBytesReference slice(int from, int length)
      Slice the bytes from the from index up to length. The returned bytes reference will share the reference count of this instance and as such any ref-counting operations on the return are shared with this instance and vice versa. Using retainedSlice(int, int) might be more efficient in situations where the return of this method is subsequently retained by increasing its ref-count.
      Specified by:
      slice in interface BytesReference
    • ramBytesUsed

      public long ramBytesUsed()
      Description copied from interface: BytesReference
      The amount of memory used by this BytesReference.

      Note that this is not always the same as length and can vary by implementation.

      Specified by:
      ramBytesUsed in interface BytesReference
    • streamInput

      public StreamInput streamInput() throws IOException
      Description copied from interface: BytesReference
      A stream input of the bytes.
      Specified by:
      streamInput in interface BytesReference
      Throws:
      IOException
    • writeTo

      public void writeTo(OutputStream os) throws IOException
      Description copied from interface: BytesReference
      Writes the bytes directly to the output stream.
      Specified by:
      writeTo in interface BytesReference
      Throws:
      IOException
    • utf8ToString

      public String utf8ToString()
      Description copied from interface: BytesReference
      Interprets the referenced bytes as UTF8 bytes, returning the resulting string
      Specified by:
      utf8ToString in interface BytesReference
    • toBytesRef

      public org.apache.lucene.util.BytesRef toBytesRef()
      Description copied from interface: BytesReference
      Converts to Lucene BytesRef.
      Specified by:
      toBytesRef in interface BytesReference
    • iterator

      public org.apache.lucene.util.BytesRefIterator iterator()
      Description copied from interface: BytesReference
      Returns a BytesRefIterator for this BytesReference. This method allows access to the internal pages of this reference without copying them. It must return direct references to the pages, not copies. Use with care!
      Specified by:
      iterator in interface BytesReference
      See Also:
      • BytesRefIterator
    • compareTo

      public int compareTo(BytesReference o)
      Specified by:
      compareTo in interface Comparable<BytesReference>
    • toXContent

      public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException
      Specified by:
      toXContent in interface ToXContent
      Throws:
      IOException
    • isFragment

      public boolean isFragment()
      Specified by:
      isFragment in interface ToXContent
      Specified by:
      isFragment in interface ToXContentFragment
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • hasArray

      public boolean hasArray()
      Specified by:
      hasArray in interface BytesReference
      Returns:
      true if this instance is backed by a byte array
    • array

      public byte[] array()
      Specified by:
      array in interface BytesReference
      Returns:
      backing byte array for this instance
    • arrayOffset

      public int arrayOffset()
      Specified by:
      arrayOffset in interface BytesReference
      Returns:
      offset of the first byte of this instance in the backing byte array