Package io.airlift.slice
Class Slice
java.lang.Object
io.airlift.slice.Slice
- All Implemented Interfaces:
Comparable<Slice>
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]Returns the byte array wrapped by this Slice, if any.intReturns the start index the content of this slice within the byte array wrapped by this slice.voidclear()Fill the slice with zeros;voidclear(int offset, int length) intCompares a portion of this slice with a portion of the specified slice.intCompares the content of the specified buffer to the content of this buffer.booleanCompares a portion of this slice with a portion of the specified slice.booleanCompares the specified object with this slice for equality.voidfill(byte value) Fill the slice with the specified value;longReturn the address offset of this Slice.getBase()Returns the base object of this Slice, or null.bytegetByte(int index) Gets a byte at the specified absoluteindexin this buffer.byte[]getBytes()Returns a copy of this buffer as a byte array.voidgetBytes(int index, byte[] destination) Transfers portion of data from this slice into the specified destination starting at the specified absoluteindex.voidgetBytes(int index, byte[] destination, int destinationIndex, int length) Transfers portion of data from this slice into the specified destination starting at the specified absoluteindex.byte[]getBytes(int index, int length) Returns a copy of this buffer as a byte array.voidTransfers portion of data from this slice into the specified destination starting at the specified absoluteindex.voidTransfers portion of data from this slice into the specified destination starting at the specified absoluteindex.voidgetBytes(int index, OutputStream out, int length) Transfers a portion of data from this slice into the specified stream starting at the specified absoluteindex.doublegetDouble(int index) Gets a 64-bit double at the specified absoluteindexin this buffer.floatgetFloat(int index) Gets a 32-bit float at the specified absoluteindexin this buffer.getInput()Creates a slice input backed by this slice.intgetInt(int index) Gets a 32-bit integer at the specified absoluteindexin this buffer.longgetLong(int index) Gets a 64-bit long integer at the specified absoluteindexin this buffer.Creates a slice output backed by this slice.longApproximate number of bytes retained by this slice.shortgetShort(int index) Gets a 16-bit short integer at the specified absoluteindexin this slice.shortgetUnsignedByte(int index) Gets an unsigned byte at the specified absoluteindexin this buffer.longgetUnsignedInt(int index) Gets an unsigned 32-bit integer at the specified absoluteindexin this buffer.intgetUnsignedShort(int index) Gets an unsigned 16-bit short integer at the specified absoluteindexin this slice.booleaninthashCode()Returns the hash code of this slice.inthashCode(int offset, int length) Returns the hash code of a portion of this slice.intReturns the index of the first occurrence of the pattern with this slice.intReturns the index of the first occurrence of the pattern with this slice.intindexOfByte(byte b) intindexOfByte(int b) booleanA slice is considered compact if the base object is an array and it contains the whole array.intlength()Length of this slice.voidsetByte(int index, int value) Sets the specified byte at the specified absoluteindexin this buffer.voidsetBytes(int index, byte[] source) Transfers data from the specified slice into this buffer starting at the specified absoluteindex.voidsetBytes(int index, byte[] source, int sourceIndex, int length) Transfers data from the specified array into this buffer starting at the specified absoluteindex.voidTransfers data from the specified slice into this buffer starting at the specified absoluteindex.voidTransfers data from the specified slice into this buffer starting at the specified absoluteindex.voidsetBytes(int index, InputStream in, int length) Transfers data from the specified input stream into this slice starting at the specified absoluteindex.voidsetDouble(int index, double value) Sets the specified 64-bit double at the specified absoluteindexin this buffer.voidsetFloat(int index, float value) Sets the specified 32-bit float at the specified absoluteindexin this buffer.voidsetInt(int index, int value) Sets the specified 32-bit integer at the specified absoluteindexin this buffer.voidsetLong(int index, long value) Sets the specified 64-bit long integer at the specified absoluteindexin this buffer.voidsetShort(int index, int value) Sets the specified 16-bit short integer at the specified absoluteindexin this buffer.slice(int index, int length) Returns a slice of this buffer's sub-region.toByteBuffer(int index, int length) toString()Decodes the a portion of this slice into a string with the specified character set name.Decodes the a portion of this slice into a string with the specified character set name.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.
-
Method Details
-
getBase
Returns the base object of this Slice, or null. This is appropriate for use withUnsafeif 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 withUnsafeif 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
Returns the byte array wrapped by this Slice, if any. Callers are expected to checkhasByteArray()before calling this method since not all instances are backed by a byte array. Callers should also take care to usebyteArrayOffset()since the contents of this Slice may not start at array index 0.- Throws:
UnsupportedOperationException- if this Slice has no underlying byte array
-
byteArrayOffset
Returns the start index the content of this slice within the byte array wrapped by this slice. Callers should checkhasByteArray()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 absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 1is greater thanthis.length()
-
getUnsignedByte
public short getUnsignedByte(int index) Gets an unsigned byte at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 1is greater thanthis.length()
-
getShort
public short getShort(int index) Gets a 16-bit short integer at the specified absoluteindexin this slice.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 2is greater thanthis.length()
-
getUnsignedShort
public int getUnsignedShort(int index) Gets an unsigned 16-bit short integer at the specified absoluteindexin this slice.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 2is greater thanthis.length()
-
getInt
public int getInt(int index) Gets a 32-bit integer at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 4is greater thanthis.length()
-
getUnsignedInt
public long getUnsignedInt(int index) Gets an unsigned 32-bit integer at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 4is greater thanthis.length()
-
getLong
public long getLong(int index) Gets a 64-bit long integer at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 8is greater thanthis.length()
-
getFloat
public float getFloat(int index) Gets a 32-bit float at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 4is greater thanthis.length()
-
getDouble
public double getDouble(int index) Gets a 64-bit double at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 8is greater thanthis.length()
-
getBytes
Transfers portion of data from this slice into the specified destination starting at the specified absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, or ifindex + destination.length()is greater thanthis.length()
-
getBytes
Transfers portion of data from this slice into the specified destination starting at the specified absoluteindex.- Parameters:
destinationIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifieddestinationIndexis less than0, ifindex + lengthis greater thanthis.length(), or ifdestinationIndex + lengthis greater thandestination.length()
-
getBytes
public void getBytes(int index, byte[] destination) Transfers portion of data from this slice into the specified destination starting at the specified absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, or ifindex + destination.lengthis greater thanthis.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 absoluteindex.- Parameters:
destinationIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifieddestinationIndexis less than0, ifindex + lengthis greater thanthis.length(), or ifdestinationIndex + lengthis greater thandestination.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 atlength- the number of bytes to return- Throws:
IndexOutOfBoundsException- if the specifiedindexis less then0, or if the specifiedindex + lengthis greater thanthis.length()
-
getBytes
Transfers a portion of data from this slice into the specified stream starting at the specified absoluteindex.- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + lengthis greater thanthis.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 absoluteindexin this buffer. The 24 high-order bits of the specified value are ignored.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 1is greater thanthis.length()
-
setShort
public void setShort(int index, int value) Sets the specified 16-bit short integer at the specified absoluteindexin this buffer. The 16 high-order bits of the specified value are ignored.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 2is greater thanthis.length()
-
setInt
public void setInt(int index, int value) Sets the specified 32-bit integer at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 4is greater thanthis.length()
-
setLong
public void setLong(int index, long value) Sets the specified 64-bit long integer at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 8is greater thanthis.length()
-
setFloat
public void setFloat(int index, float value) Sets the specified 32-bit float at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 4is greater thanthis.length()
-
setDouble
public void setDouble(int index, double value) Sets the specified 64-bit double at the specified absoluteindexin this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 8is greater thanthis.length()
-
setBytes
Transfers data from the specified slice into this buffer starting at the specified absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, or ifindex + source.length()is greater thanthis.length()
-
setBytes
Transfers data from the specified slice into this buffer starting at the specified absoluteindex.- Parameters:
sourceIndex- the first index of the sourcelength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifiedsourceIndexis less than0, ifindex + lengthis greater thanthis.length(), or ifsourceIndex + lengthis greater thansource.length()
-
setBytes
public void setBytes(int index, byte[] source) Transfers data from the specified slice into this buffer starting at the specified absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, or ifindex + source.lengthis greater thanthis.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 absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifiedsourceIndexis less than0, ifindex + lengthis greater thanthis.length(), or ifsourceIndex + lengthis greater thansource.length
-
setBytes
Transfers data from the specified input stream into this slice starting at the specified absoluteindex.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, or ifindex + source.lengthis greater thanthis.length()IOException
-
slice
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
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
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
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:
compareToin interfaceComparable<Slice>
-
compareTo
Compares a portion of this slice with a portion of the specified slice. Equality is solely based on the contents of the slice. -
equals
Compares the specified object with this slice for equality. Equality is solely based on the contents of the slice. -
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. -
hashCode
public int hashCode(int offset, int length) Returns the hash code of a portion of this slice. -
equals
Compares a portion of this slice with a portion of the specified slice. Equality is solely based on the contents of the slice. -
getInput
Creates a slice input backed by this slice. Any changes to this slice will be immediately visible to the slice input. -
getOutput
Creates a slice output backed by this slice. Any data written to the slice output will be immediately visible in this slice. -
toString
Decodes the contents of this slice into a string with the specified character set name. -
toStringUtf8
Decodes the contents of this slice into a string using the UTF-8 character set. -
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
-
toString
Decodes the a portion of this slice into a string with the specified character set name. -
toByteBuffer
-
toByteBuffer
-
toString
Decodes the a portion of this slice into a string with the specified character set name.
-