Class Slice

java.lang.Object
io.airlift.slice.Slice
All Implemented Interfaces:
Comparable<Slice>

public final class Slice extends Object implements Comparable<Slice>
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the byte array wrapped by this Slice, if any.
    int
    Returns the start index the content of this slice within the byte array wrapped by this slice.
    void
    Fill the slice with zeros;
    void
    clear(int offset, int length)
     
    int
    compareTo(int offset, int length, Slice that, int otherOffset, int otherLength)
    Compares a portion of this slice with a portion of the specified slice.
    int
    Compares the content of the specified buffer to the content of this buffer.
    boolean
    equals(int offset, int length, Slice that, int otherOffset, int otherLength)
    Compares a portion of this slice with a portion of the specified slice.
    boolean
    Compares the specified object with this slice for equality.
    void
    fill(byte value)
    Fill the slice with the specified value;
    long
    Return the address offset of this Slice.
    Returns the base object of this Slice, or null.
    byte
    getByte(int index)
    Gets a byte at the specified absolute index in this buffer.
    byte[]
    Returns a copy of this buffer as a byte array.
    void
    getBytes(int index, byte[] destination)
    Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
    void
    getBytes(int index, byte[] destination, int destinationIndex, int length)
    Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
    byte[]
    getBytes(int index, int length)
    Returns a copy of this buffer as a byte array.
    void
    getBytes(int index, Slice destination)
    Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
    void
    getBytes(int index, Slice destination, int destinationIndex, int length)
    Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
    void
    getBytes(int index, OutputStream out, int length)
    Transfers a portion of data from this slice into the specified stream starting at the specified absolute index.
    double
    getDouble(int index)
    Gets a 64-bit double at the specified absolute index in this buffer.
    float
    getFloat(int index)
    Gets a 32-bit float at the specified absolute index in this buffer.
    Creates a slice input backed by this slice.
    int
    getInt(int index)
    Gets a 32-bit integer at the specified absolute index in this buffer.
    long
    getLong(int index)
    Gets a 64-bit long integer at the specified absolute index in this buffer.
    Creates a slice output backed by this slice.
    long
    Approximate number of bytes retained by this slice.
    short
    getShort(int index)
    Gets a 16-bit short integer at the specified absolute index in this slice.
    short
    getUnsignedByte(int index)
    Gets an unsigned byte at the specified absolute index in this buffer.
    long
    getUnsignedInt(int index)
    Gets an unsigned 32-bit integer at the specified absolute index in this buffer.
    int
    getUnsignedShort(int index)
    Gets an unsigned 16-bit short integer at the specified absolute index in this slice.
    boolean
     
    int
    Returns the hash code of this slice.
    int
    hashCode(int offset, int length)
    Returns the hash code of a portion of this slice.
    int
    indexOf(Slice slice)
    Returns the index of the first occurrence of the pattern with this slice.
    int
    indexOf(Slice pattern, int offset)
    Returns the index of the first occurrence of the pattern with this slice.
    int
    indexOfByte(byte b)
     
    int
    indexOfByte(int b)
     
    boolean
    A slice is considered compact if the base object is an array and it contains the whole array.
    int
    Length of this slice.
    void
    setByte(int index, int value)
    Sets the specified byte at the specified absolute index in this buffer.
    void
    setBytes(int index, byte[] source)
    Transfers data from the specified slice into this buffer starting at the specified absolute index.
    void
    setBytes(int index, byte[] source, int sourceIndex, int length)
    Transfers data from the specified array into this buffer starting at the specified absolute index.
    void
    setBytes(int index, Slice source)
    Transfers data from the specified slice into this buffer starting at the specified absolute index.
    void
    setBytes(int index, Slice source, int sourceIndex, int length)
    Transfers data from the specified slice into this buffer starting at the specified absolute index.
    void
    setBytes(int index, InputStream in, int length)
    Transfers data from the specified input stream into this slice starting at the specified absolute index.
    void
    setDouble(int index, double value)
    Sets the specified 64-bit double at the specified absolute index in this buffer.
    void
    setFloat(int index, float value)
    Sets the specified 32-bit float at the specified absolute index in this buffer.
    void
    setInt(int index, int value)
    Sets the specified 32-bit integer at the specified absolute index in this buffer.
    void
    setLong(int index, long value)
    Sets the specified 64-bit long integer at the specified absolute index in this buffer.
    void
    setShort(int index, int value)
    Sets the specified 16-bit short integer at the specified absolute index in this buffer.
    slice(int index, int length)
    Returns a slice of this buffer's sub-region.
     
    toByteBuffer(int index, int length)
     
    Decodes the a portion of this slice into a string with the specified character set name.
    toString(int index, int length, Charset charset)
    Decodes the a portion of this slice into a string with the specified character set name.
    toString(Charset charset)
    Decodes the contents of this slice into a string with the specified character set name.
    Decodes the contents of this slice into a string using the US_ASCII character set.
    toStringAscii(int index, int length)
     
    Decodes the contents of this slice into a string using the UTF-8 character set.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • getBase

      public Object getBase()
      Returns the base object of this Slice, or null. This is appropriate for use with Unsafe if you wish to avoid all the safety belts e.g. bounds checks.
    • getAddress

      public long getAddress()
      Return the address offset of this Slice. This is appropriate for use with Unsafe if you wish to avoid all the safety belts e.g. bounds checks.
    • length

      public int length()
      Length of this slice.
    • getRetainedSize

      public long getRetainedSize()
      Approximate number of bytes retained by this slice.
    • isCompact

      public boolean isCompact()
      A slice is considered compact if the base object is an array and it contains the whole array. As a result, it cannot be a view of a bigger slice.
    • hasByteArray

      public boolean hasByteArray()
    • byteArray

      public byte[] byteArray() throws UnsupportedOperationException
      Returns the byte array wrapped by this Slice, if any. Callers are expected to check hasByteArray() before calling this method since not all instances are backed by a byte array. Callers should also take care to use byteArrayOffset() since the contents of this Slice may not start at array index 0.
      Throws:
      UnsupportedOperationException - if this Slice has no underlying byte array
    • byteArrayOffset

      public int byteArrayOffset() throws UnsupportedOperationException
      Returns the start index the content of this slice within the byte array wrapped by this slice. Callers should check hasByteArray() before calling this method since not all Slices wrap a heap byte array
      Throws:
      UnsupportedOperationException - if this Slice has no underlying byte array
    • fill

      public void fill(byte value)
      Fill the slice with the specified value;
    • clear

      public void clear()
      Fill the slice with zeros;
    • clear

      public void clear(int offset, int length)
    • getByte

      public byte getByte(int index)
      Gets a byte at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 1 is greater than this.length()
    • getUnsignedByte

      public short getUnsignedByte(int index)
      Gets an unsigned byte at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 1 is greater than this.length()
    • getShort

      public short getShort(int index)
      Gets a 16-bit short integer at the specified absolute index in this slice.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 2 is greater than this.length()
    • getUnsignedShort

      public int getUnsignedShort(int index)
      Gets an unsigned 16-bit short integer at the specified absolute index in this slice.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 2 is greater than this.length()
    • getInt

      public int getInt(int index)
      Gets a 32-bit integer at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 4 is greater than this.length()
    • getUnsignedInt

      public long getUnsignedInt(int index)
      Gets an unsigned 32-bit integer at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 4 is greater than this.length()
    • getLong

      public long getLong(int index)
      Gets a 64-bit long integer at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 8 is greater than this.length()
    • getFloat

      public float getFloat(int index)
      Gets a 32-bit float at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 4 is greater than this.length()
    • getDouble

      public double getDouble(int index)
      Gets a 64-bit double at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 8 is greater than this.length()
    • getBytes

      public void getBytes(int index, Slice destination)
      Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, or if index + destination.length() is greater than this.length()
    • getBytes

      public void getBytes(int index, Slice destination, int destinationIndex, int length)
      Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
      Parameters:
      destinationIndex - the first index of the destination
      length - the number of bytes to transfer
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, if the specified destinationIndex is less than 0, if index + length is greater than this.length(), or if destinationIndex + length is greater than destination.length()
    • getBytes

      public void getBytes(int index, byte[] destination)
      Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, or if index + destination.length is greater than this.length()
    • getBytes

      public void getBytes(int index, byte[] destination, int destinationIndex, int length)
      Transfers portion of data from this slice into the specified destination starting at the specified absolute index.
      Parameters:
      destinationIndex - the first index of the destination
      length - the number of bytes to transfer
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, if the specified destinationIndex is less than 0, if index + length is greater than this.length(), or if destinationIndex + length is greater than destination.length
    • getBytes

      public byte[] getBytes()
      Returns a copy of this buffer as a byte array.
    • getBytes

      public byte[] getBytes(int index, int length)
      Returns a copy of this buffer as a byte array.
      Parameters:
      index - the absolute index to start at
      length - the number of bytes to return
      Throws:
      IndexOutOfBoundsException - if the specified index is less then 0, or if the specified index + length is greater than this.length()
    • getBytes

      public void getBytes(int index, OutputStream out, int length) throws IOException
      Transfers a portion of data from this slice into the specified stream starting at the specified absolute index.
      Parameters:
      length - the number of bytes to transfer
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + length is greater than this.length()
      IOException - if the specified stream threw an exception during I/O
    • setByte

      public void setByte(int index, int value)
      Sets the specified byte at the specified absolute index in this buffer. The 24 high-order bits of the specified value are ignored.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 1 is greater than this.length()
    • setShort

      public void setShort(int index, int value)
      Sets the specified 16-bit short integer at the specified absolute index in this buffer. The 16 high-order bits of the specified value are ignored.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 2 is greater than this.length()
    • setInt

      public void setInt(int index, int value)
      Sets the specified 32-bit integer at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 4 is greater than this.length()
    • setLong

      public void setLong(int index, long value)
      Sets the specified 64-bit long integer at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 8 is greater than this.length()
    • setFloat

      public void setFloat(int index, float value)
      Sets the specified 32-bit float at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 4 is greater than this.length()
    • setDouble

      public void setDouble(int index, double value)
      Sets the specified 64-bit double at the specified absolute index in this buffer.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or index + 8 is greater than this.length()
    • setBytes

      public void setBytes(int index, Slice source)
      Transfers data from the specified slice into this buffer starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, or if index + source.length() is greater than this.length()
    • setBytes

      public void setBytes(int index, Slice source, int sourceIndex, int length)
      Transfers data from the specified slice into this buffer starting at the specified absolute index.
      Parameters:
      sourceIndex - the first index of the source
      length - the number of bytes to transfer
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, if the specified sourceIndex is less than 0, if index + length is greater than this.length(), or if sourceIndex + length is greater than source.length()
    • setBytes

      public void setBytes(int index, byte[] source)
      Transfers data from the specified slice into this buffer starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, or if index + source.length is greater than this.length()
    • setBytes

      public void setBytes(int index, byte[] source, int sourceIndex, int length)
      Transfers data from the specified array into this buffer starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, if the specified sourceIndex is less than 0, if index + length is greater than this.length(), or if sourceIndex + length is greater than source.length
    • setBytes

      public void setBytes(int index, InputStream in, int length) throws IOException
      Transfers data from the specified input stream into this slice starting at the specified absolute index.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0, or if index + source.length is greater than this.length()
      IOException
    • slice

      public Slice slice(int index, int length)
      Returns a slice of this buffer's sub-region. Modifying the content of the returned buffer or this buffer affects each other's content.
    • indexOfByte

      public int indexOfByte(int b)
    • indexOfByte

      public int indexOfByte(byte b)
    • indexOf

      public int indexOf(Slice slice)
      Returns the index of the first occurrence of the pattern with this slice. If the pattern is not found -1 is returned. If patten is empty, zero is returned.
    • indexOf

      public int indexOf(Slice pattern, int offset)
      Returns the index of the first occurrence of the pattern with this slice. If the pattern is not found -1 is returned. If patten is empty, the offset is returned.
    • compareTo

      public int compareTo(Slice that)
      Compares the content of the specified buffer to the content of this buffer. This comparison is performed byte by byte using an unsigned comparison.
      Specified by:
      compareTo in interface Comparable<Slice>
    • compareTo

      public int compareTo(int offset, int length, Slice that, int otherOffset, int otherLength)
      Compares a portion of this slice with a portion of the specified slice. Equality is solely based on the contents of the slice.
    • equals

      public boolean equals(Object o)
      Compares the specified object with this slice for equality. Equality is solely based on the contents of the slice.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Returns the hash code of this slice. The hash code is cached once calculated and any future changes to the slice will not effect the hash code.
      Overrides:
      hashCode in class Object
    • hashCode

      public int hashCode(int offset, int length)
      Returns the hash code of a portion of this slice.
    • equals

      public boolean equals(int offset, int length, Slice that, int otherOffset, int otherLength)
      Compares a portion of this slice with a portion of the specified slice. Equality is solely based on the contents of the slice.
    • getInput

      public BasicSliceInput getInput()
      Creates a slice input backed by this slice. Any changes to this slice will be immediately visible to the slice input.
    • getOutput

      public SliceOutput getOutput()
      Creates a slice output backed by this slice. Any data written to the slice output will be immediately visible in this slice.
    • toString

      public String toString(Charset charset)
      Decodes the contents of this slice into a string with the specified character set name.
    • toStringUtf8

      public String toStringUtf8()
      Decodes the contents of this slice into a string using the UTF-8 character set.
    • toStringAscii

      public String toStringAscii()
      Decodes the contents of this slice into a string using the US_ASCII character set. The low order 7 bits if each byte are converted directly into a code point for the string.
    • toStringAscii

      public String toStringAscii(int index, int length)
    • toString

      public String toString(int index, int length, Charset charset)
      Decodes the a portion of this slice into a string with the specified character set name.
    • toByteBuffer

      public ByteBuffer toByteBuffer()
    • toByteBuffer

      public ByteBuffer toByteBuffer(int index, int length)
    • toString

      public String toString()
      Decodes the a portion of this slice into a string with the specified character set name.
      Overrides:
      toString in class Object