Class Slice

java.lang.Object
org.iq80.leveldb.util.Slice
All Implemented Interfaces:
Comparable<Slice>

public final class Slice extends Object implements Comparable<Slice>
Little Endian slice of a byte array.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final byte[]
     
    private int
     
    private final int
     
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Slice(byte[] data)
     
    Slice(byte[] data, int offset, int length)
     
    Slice(int length)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Compares the content of the specified buffer to the content of this buffer.
    byte[]
     
    byte[]
    copyBytes(int index, int length)
     
     
    copySlice(int index, int length)
    Returns a copy of this buffer's sub-region.
    boolean
     
    byte
    getByte(int index)
    Gets a byte at the specified absolute index in this buffer.
    byte[]
     
    void
    getBytes(int index, byte[] destination, int destinationIndex, int length)
    Transfers this buffer's data to the specified destination starting at the specified absolute index.
    byte[]
    getBytes(int index, int length)
     
    void
    getBytes(int index, OutputStream out, int length)
    Transfers this buffer's data to the specified stream starting at the specified absolute index.
    void
    getBytes(int index, ByteBuffer destination)
    Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit.
    int
    getBytes(int index, GatheringByteChannel out, int length)
    Transfers this buffer's data to the specified channel starting at the specified absolute index.
    void
    getBytes(int index, Slice dst, int dstIndex, int length)
    Transfers this buffer's data to the specified destination starting at the specified absolute index.
    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.
    byte[]
    Gets the array underlying this slice.
    int
    Gets the offset of this slice in the underlying array.
    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.
    int
     
    Creates an input stream over this slice.
    int
    Length of this slice.
    Creates an output stream over 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, int sourceIndex, int length)
    Transfers the specified source array's data to this buffer starting at the specified absolute index.
    int
    setBytes(int index, InputStream in, int length)
    Transfers the content of the specified source stream to this buffer starting at the specified absolute index.
    void
    setBytes(int index, ByteBuffer source)
    Transfers the specified source buffer's data to this buffer starting at the specified absolute index until the source buffer's position reaches its limit.
    int
    setBytes(int index, FileChannel in, int position, int length)
     
    int
    setBytes(int index, ScatteringByteChannel in, int length)
    Transfers the content of the specified source channel to this buffer starting at the specified absolute index.
    void
    setBytes(int index, Slice src, int srcIndex, int length)
    Transfers the specified source buffer's data to this buffer starting at the specified absolute index.
    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.
    Returns a slice of this buffer's readable bytes.
    slice(int index, int length)
    Returns a slice of this buffer's sub-region.
    Converts this buffer's readable bytes into a NIO buffer.
    toByteBuffer(int index, int length)
    Converts this buffer's sub-region into a NIO buffer.
     
    toString(int index, int length, Charset charset)
    Decodes this buffer's sub-region into a string with the specified character set.
    toString(Charset charset)
    Decodes this buffer's readable bytes into a string with the specified character set name.

    Methods inherited from class java.lang.Object

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

    • data

      private final byte[] data
    • offset

      private final int offset
    • length

      private final int length
    • hash

      private int hash
  • Constructor Details

    • Slice

      public Slice(int length)
    • Slice

      public Slice(byte[] data)
    • Slice

      public Slice(byte[] data, int offset, int length)
  • Method Details

    • length

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

      public byte[] getRawArray()
      Gets the array underlying this slice.
    • getRawOffset

      public int getRawOffset()
      Gets the offset of this slice in the underlying array.
    • 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.capacity
    • 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.capacity
    • 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.capacity
    • 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.capacity
    • 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.capacity
    • getBytes

      public void getBytes(int index, Slice dst, int dstIndex, int length)
      Transfers this buffer's data to the specified destination starting at the specified absolute index.
      Parameters:
      dstIndex - 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 dstIndex is less than 0, if index + length is greater than this.capacity, or if dstIndex + length is greater than dst.capacity
    • getBytes

      public void getBytes(int index, byte[] destination, int destinationIndex, int length)
      Transfers this buffer's data to 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 dstIndex is less than 0, if index + length is greater than this.capacity, or if dstIndex + length is greater than dst.length
    • getBytes

      public byte[] getBytes()
    • getBytes

      public byte[] getBytes(int index, int length)
    • getBytes

      public void getBytes(int index, ByteBuffer destination)
      Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + dst.remaining() is greater than this.capacity
    • getBytes

      public void getBytes(int index, OutputStream out, int length) throws IOException
      Transfers this buffer's data to 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.capacity
      IOException - if the specified stream threw an exception during I/O
    • getBytes

      public int getBytes(int index, GatheringByteChannel out, int length) throws IOException
      Transfers this buffer's data to the specified channel starting at the specified absolute index.
      Parameters:
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes written out to the specified channel
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + length is greater than this.capacity
      IOException - if the specified channel threw an exception during I/O
    • 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.capacity
    • 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.capacity
    • 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.capacity
    • 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.capacity
    • setBytes

      public void setBytes(int index, Slice src, int srcIndex, int length)
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index.
      Parameters:
      srcIndex - 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 srcIndex is less than 0, if index + length is greater than this.capacity, or if srcIndex + length is greater than src.capacity
    • setBytes

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

      public void setBytes(int index, ByteBuffer source)
      Transfers the specified source buffer's data to this buffer starting at the specified absolute index until the source buffer's position reaches its limit.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + src.remaining() is greater than this.capacity
    • setBytes

      public int setBytes(int index, InputStream in, int length) throws IOException
      Transfers the content of the specified source stream to this buffer starting at the specified absolute index.
      Parameters:
      length - the number of bytes to transfer
      Returns:
      the actual number of bytes read in from the specified channel. -1 if the specified channel is closed.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + length is greater than this.capacity
      IOException - if the specified stream threw an exception during I/O
    • setBytes

      public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException
      Transfers the content of the specified source channel to this buffer starting at the specified absolute index.
      Parameters:
      length - the maximum number of bytes to transfer
      Returns:
      the actual number of bytes read in from the specified channel. -1 if the specified channel is closed.
      Throws:
      IndexOutOfBoundsException - if the specified index is less than 0 or if index + length is greater than this.capacity
      IOException - if the specified channel threw an exception during I/O
    • setBytes

      public int setBytes(int index, FileChannel in, int position, int length) throws IOException
      Throws:
      IOException
    • copySlice

      public Slice copySlice()
    • copySlice

      public Slice copySlice(int index, int length)
      Returns a copy of this buffer's sub-region. Modifying the content of the returned buffer or this buffer does not affect each other at all.
    • copyBytes

      public byte[] copyBytes()
    • copyBytes

      public byte[] copyBytes(int index, int length)
    • slice

      public Slice slice()
      Returns a slice of this buffer's readable bytes. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks.
    • 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 while they maintain separate indexes and marks.
    • input

      public SliceInput input()
      Creates an input stream over this slice.
    • output

      public SliceOutput output()
      Creates an output stream over this slice.
    • toByteBuffer

      public ByteBuffer toByteBuffer()
      Converts this buffer's readable bytes into a NIO buffer. The returned buffer shares the content with this buffer.
    • toByteBuffer

      public ByteBuffer toByteBuffer(int index, int length)
      Converts this buffer's sub-region into a NIO buffer. The returned buffer shares the content with this buffer.
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • 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>
    • toString

      public String toString(Charset charset)
      Decodes this buffer's readable bytes into a string with the specified character set name.
    • toString

      public String toString(int index, int length, Charset charset)
      Decodes this buffer's sub-region into a string with the specified character set.
    • toString

      public String toString()
      Overrides:
      toString in class Object