Class ByteArraySegment

    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArraySegment​(byte[] array)
      Creates a new instance of the ByteArraySegment class that wraps the entire given array.
      ByteArraySegment​(byte[] array, int startOffset, int length)
      Creates a new instance of the ByteArraySegment class that wraps the given array range.
      ByteArraySegment​(java.nio.ByteBuffer buff)
      Creates a new instance of the ByteArraySegment class that wraps an array backed ByteBuffer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] array()
      Gets a reference to the backing array for this ArrayView.
      int arrayOffset()
      Gets a value indicating the offset in the backing array where this ArrayView starts at.
      java.nio.ByteBuffer asByteBuffer()
      Returns a new ByteBuffer that wraps the contents of this ArrayView.
      <ExceptionT extends java.lang.Exception>
      void
      collect​(BufferView.Collector<ExceptionT> bufferCollector)
      Iterates through each of the buffers that make up this BufferView, in order, and invokes the given BufferView.Collector on each.
      void copyFrom​(ArrayView source, int targetOffset, int length)
      Copies a specified number of bytes from the given ArrayView into this ByteArraySegment.
      void copyFrom​(ArrayView source, int sourceOffset, int targetOffset, int length)
      Copies a specified number of bytes from the given ArrayView into this ByteArraySegment.
      void copyTo​(byte[] target, int targetOffset, int length)
      Copies a specified number of bytes from this ArrayView into the given target array.
      void copyTo​(java.io.OutputStream stream)
      Writes the entire contents of this ByteArraySegment to the given OutputStream.
      int copyTo​(java.nio.ByteBuffer target)
      Copies the contents of this BufferView to the given ByteBuffer.
      boolean equals​(BufferView other)
      Checks for equality against another BufferView instance.
      byte get​(int index)
      Gets the value at the specified index.
      int getAllocatedLength()
      Gets a value indicating the amount of memory (in bytes) allocated for this BufferView.
      io.pravega.common.util.ByteArraySegment.Reader getBufferViewReader()
      Creates a new BufferView.Reader that can be used to read this BufferView.
      byte[] getCopy()
      Returns a copy of the contents of this BufferView.
      int getInt​(int index)
      Gets the 32 bit Int value at the specified index.
      int getLength()
      Gets a value representing the length of this BufferView.
      long getLong​(int index)
      Gets the 64 bit Long value at the specified index.
      java.io.InputStream getReader()
      Creates an InputStream that can be used to read the contents of this BufferView.
      java.io.InputStream getReader​(int offset, int length)
      Creates an InputStream that can be used to read the contents of this BufferView.
      short getShort​(int index)
      Gets the 16 bit Short value at the specified index.
      java.util.Iterator<java.nio.ByteBuffer> iterateBuffers()
      Gets an Iterator through each of the ByteBuffers that make up this BufferView, in order.
      void set​(int index, byte value)
      Sets the value at the specified index.
      void setInt​(int index, int value)
      Sets a 32 bit Integer value at the specified index.
      void setLong​(int index, long value)
      Sets a 64 bit Long value at the specified index.
      void setShort​(int index, short value)
      Sets a Short value at the specified index.
      ByteArraySegment slice​(int offset, int length)
      Creates a new ArrayView that represents a sub-range of this ArrayView instance.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ByteArraySegment

        public ByteArraySegment​(byte[] array)
        Creates a new instance of the ByteArraySegment class that wraps the entire given array.
        Parameters:
        array - The array to wrap.
        Throws:
        java.lang.NullPointerException - If the array is null.
      • ByteArraySegment

        public ByteArraySegment​(java.nio.ByteBuffer buff)
        Creates a new instance of the ByteArraySegment class that wraps an array backed ByteBuffer.
        Parameters:
        buff - The ByteBuffer to wrap.
        Throws:
        java.lang.NullPointerException - If the array is null.
        java.lang.UnsupportedOperationException - If buff is not backed by an array.
      • ByteArraySegment

        public ByteArraySegment​(byte[] array,
                                int startOffset,
                                int length)
        Creates a new instance of the ByteArraySegment class that wraps the given array range.
        Parameters:
        array - The array to wrap.
        startOffset - The offset within the array to start the segment at.
        length - The length of the segment.
        Throws:
        java.lang.NullPointerException - If the array is null.
        java.lang.ArrayIndexOutOfBoundsException - If StartOffset or Length have invalid values.
    • Method Detail

      • getAllocatedLength

        public int getAllocatedLength()
        Description copied from interface: BufferView
        Gets a value indicating the amount of memory (in bytes) allocated for this BufferView.
        Specified by:
        getAllocatedLength in interface BufferView
        Returns:
        The allocated memory size.
      • get

        public byte get​(int index)
        Description copied from interface: StructuredReadableBuffer
        Gets the value at the specified index.
        Specified by:
        get in interface StructuredReadableBuffer
        Parameters:
        index - The index to get the value at.
        Returns:
        The byte at the specified index.
      • getShort

        public short getShort​(int index)
        Description copied from interface: StructuredReadableBuffer
        Gets the 16 bit Short value at the specified index.
        Specified by:
        getShort in interface StructuredReadableBuffer
        Parameters:
        index - The index to get the value at.
        Returns:
        The Short at the specified index.
      • getInt

        public int getInt​(int index)
        Description copied from interface: StructuredReadableBuffer
        Gets the 32 bit Int value at the specified index.
        Specified by:
        getInt in interface StructuredReadableBuffer
        Parameters:
        index - The index to get the value at.
        Returns:
        The Integer at the specified index.
      • getLong

        public long getLong​(int index)
        Description copied from interface: StructuredReadableBuffer
        Gets the 64 bit Long value at the specified index.
        Specified by:
        getLong in interface StructuredReadableBuffer
        Parameters:
        index - The index to get the value at.
        Returns:
        The Long at the specified index.
      • array

        public byte[] array()
        Description copied from interface: ArrayView
        Gets a reference to the backing array for this ArrayView. This should be used in conjunction with arrayOffset() in order to determine where in the array this ArrayView starts at. NOTE: Care must be taken when using this array. Just like any other array in Java, it is modifiable and changes to it will be reflected in this ArrayView.
        Specified by:
        array in interface ArrayView
        Returns:
        The backing array.
      • arrayOffset

        public int arrayOffset()
        Description copied from interface: ArrayView
        Gets a value indicating the offset in the backing array where this ArrayView starts at.
        Specified by:
        arrayOffset in interface ArrayView
        Returns:
        The offset in the backing array.
      • getReader

        public java.io.InputStream getReader()
        Description copied from interface: BufferView
        Creates an InputStream that can be used to read the contents of this BufferView. The InputStream returned spans the entire BufferView.
        Specified by:
        getReader in interface BufferView
        Returns:
        The InputStream.
      • getReader

        public java.io.InputStream getReader​(int offset,
                                             int length)
        Description copied from interface: BufferView
        Creates an InputStream that can be used to read the contents of this BufferView.
        Specified by:
        getReader in interface BufferView
        Parameters:
        offset - The starting offset of the section to read.
        length - The length of the section to read.
        Returns:
        The InputStream.
      • slice

        public ByteArraySegment slice​(int offset,
                                      int length)
        Description copied from interface: ArrayView
        Creates a new ArrayView that represents a sub-range of this ArrayView instance. The new instance will share the same backing array as this one, so a change to one will be reflected in the other.
        Specified by:
        slice in interface ArrayView
        Specified by:
        slice in interface BufferView
        Parameters:
        offset - The starting offset to begin the slice at.
        length - The sliced length.
        Returns:
        A new ArrayView.
      • asByteBuffer

        public java.nio.ByteBuffer asByteBuffer()
        Description copied from interface: ArrayView
        Returns a new ByteBuffer that wraps the contents of this ArrayView.
        Specified by:
        asByteBuffer in interface ArrayView
        Returns:
        A ByteBuffer that shares the same backing array as this ArrayView. Any changes made to the ByteBuffer will be reflected in this ArrayView and viceversa.
      • getCopy

        public byte[] getCopy()
        Description copied from interface: BufferView
        Returns a copy of the contents of this BufferView.
        Specified by:
        getCopy in interface BufferView
        Returns:
        A byte array with the same length as this BufferView, containing a copy of the data within it.
      • copyTo

        public void copyTo​(byte[] target,
                           int targetOffset,
                           int length)
        Description copied from interface: ArrayView
        Copies a specified number of bytes from this ArrayView into the given target array.
        Specified by:
        copyTo in interface ArrayView
        Parameters:
        target - The target array.
        targetOffset - The offset within the target array to start copying data at.
        length - The number of bytes to copy.
      • copyTo

        public int copyTo​(java.nio.ByteBuffer target)
        Description copied from interface: BufferView
        Copies the contents of this BufferView to the given ByteBuffer.
        Specified by:
        copyTo in interface BufferView
        Parameters:
        target - The ByteBuffer to copy to. This buffer must have sufficient capacity to allow the entire contents of the BufferView to be written. If less needs to be copied, consider using BufferView.slice() to select a sub-range of this BufferView.
        Returns:
        The number of bytes copied.
      • copyTo

        public void copyTo​(java.io.OutputStream stream)
                    throws java.io.IOException
        Writes the entire contents of this ByteArraySegment to the given OutputStream. Only copies the contents of the ByteArraySegment, and writes no other data (such as the length of the Segment or any other info).
        Specified by:
        copyTo in interface BufferView
        Parameters:
        stream - The OutputStream to write to.
        Throws:
        java.io.IOException - If the OutputStream threw one.
      • equals

        public boolean equals​(BufferView other)
        Description copied from class: AbstractBufferView
        Checks for equality against another BufferView instance. Note: this method provides a general implementation of equality. Derived classes should override this method with more efficient implementations if available.
        Overrides:
        equals in class AbstractBufferView
        Parameters:
        other - The other BufferView instance.
        Returns:
        True if this instance and the other instance have the same contents.
      • collect

        public <ExceptionT extends java.lang.Exception> void collect​(BufferView.Collector<ExceptionT> bufferCollector)
                                                              throws ExceptionT extends java.lang.Exception
        Description copied from interface: BufferView
        Iterates through each of the buffers that make up this BufferView, in order, and invokes the given BufferView.Collector on each.
        Specified by:
        collect in interface BufferView
        Type Parameters:
        ExceptionT - Type of exception that the BufferView.Collector function throws, if any.
        Parameters:
        bufferCollector - A BufferView.Collector function that will be invoked for each component. Each ByteBuffer passed as an argument to this function is a direct pointer to the data contained within the BufferView (i.e., they are not copies of the data).
        Throws:
        ExceptionT - If the BufferView.Collector function throws an exception of this type, the iteration will end and the exception will be bubbled up.
        ExceptionT extends java.lang.Exception
      • iterateBuffers

        public java.util.Iterator<java.nio.ByteBuffer> iterateBuffers()
        Description copied from interface: BufferView
        Gets an Iterator through each of the ByteBuffers that make up this BufferView, in order.
        Specified by:
        iterateBuffers in interface BufferView
        Returns:
        A Iterator.
      • set

        public void set​(int index,
                        byte value)
        Description copied from interface: StructuredWritableBuffer
        Sets the value at the specified index.
        Specified by:
        set in interface StructuredWritableBuffer
        Parameters:
        index - The index to set the value at.
        value - The Byte value to set.
      • setShort

        public void setShort​(int index,
                             short value)
        Description copied from interface: StructuredWritableBuffer
        Sets a Short value at the specified index.
        Specified by:
        setShort in interface StructuredWritableBuffer
        Parameters:
        index - The index to set the value at.
        value - The Short value to set.
      • setInt

        public void setInt​(int index,
                           int value)
        Description copied from interface: StructuredWritableBuffer
        Sets a 32 bit Integer value at the specified index.
        Specified by:
        setInt in interface StructuredWritableBuffer
        Parameters:
        index - The index to set the value at.
        value - The Integer value to set.
      • setLong

        public void setLong​(int index,
                            long value)
        Description copied from interface: StructuredWritableBuffer
        Sets a 64 bit Long value at the specified index.
        Specified by:
        setLong in interface StructuredWritableBuffer
        Parameters:
        index - The index to set the value at.
        value - The Long value to set.
      • copyFrom

        public void copyFrom​(ArrayView source,
                             int targetOffset,
                             int length)
        Copies a specified number of bytes from the given ArrayView into this ByteArraySegment.
        Parameters:
        source - The ArrayView to copy bytes from.
        targetOffset - The offset within this ByteArraySegment to start copying at.
        length - The number of bytes to copy.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If targetOffset or length are invalid.
      • copyFrom

        public void copyFrom​(ArrayView source,
                             int sourceOffset,
                             int targetOffset,
                             int length)
        Copies a specified number of bytes from the given ArrayView into this ByteArraySegment.
        Parameters:
        source - The ArrayView to copy bytes from.
        sourceOffset - The offset within source to start copying from.
        targetOffset - The offset within this ByteArraySegment to start copying at.
        length - The number of bytes to copy.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - If targetOffset or length are invalid.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getLength

        public int getLength()
        Description copied from interface: BufferView
        Gets a value representing the length of this BufferView.
        Specified by:
        getLength in interface BufferView
        Returns:
        The length.