Class AbstractByteRange

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] bytes
      The array containing the bytes in this range.
      protected int hash
      Variable for lazy-caching the hashCode of this range.
      protected int length
      The number of bytes in the range.
      protected int offset
      The index of the first byte in this range.
      static int UNSET_HASH_VALUE  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void clearHashCache()  
      int compareTo​(ByteRange other)
      Bitwise comparison of each byte in the array.
      void deepCopySubRangeTo​(int innerOffset, int copyLength, byte[] destination, int destinationOffset)
      Wrapper for System.arraycopy.
      void deepCopyTo​(byte[] destination, int destinationOffset)
      Wrapper for System.arraycopy.
      byte[] deepCopyToNewArray()
      Instantiate a new byte[] with exact length, which is at least 24 bytes + length.
      byte get​(int index)
      Retrieve the byte at index.
      ByteRange get​(int index, byte[] dst)
      Fill dst with bytes from the range, starting from index.
      ByteRange get​(int index, byte[] dst, int offset, int length)
      Fill dst with bytes from the range, starting from index.
      byte[] getBytes()
      The underlying byte[].
      int getInt​(int index)
      Retrieve the int value at index
      int getLength()
      The length of the range.
      long getLong​(int index)
      Retrieve the long value at index
      int getOffset()
      The offset, the index into the underlying byte[] at which this range begins.
      short getShort​(int index)
      Retrieve the short value at index
      long getVLong​(int index)
      Retrieve the long value at index which is stored as VLong
      static int getVLongSize​(long val)  
      int hashCode()  
      boolean isEmpty()  
      static boolean isEmpty​(ByteRange range)  
      protected boolean isHashCached()  
      abstract ByteRange put​(int index, byte val)
      Store val at index.
      abstract ByteRange put​(int index, byte[] val)
      Store val at index.
      abstract ByteRange put​(int index, byte[] val, int offset, int length)
      Store length bytes from val into this range, starting at index.
      abstract ByteRange putInt​(int index, int val)
      Store the int value at index
      abstract ByteRange putLong​(int index, long val)
      Store the long value at index
      abstract ByteRange putShort​(int index, short val)
      Store the short value at index
      abstract int putVLong​(int index, long val)
      Store the long value at index as a VLong
      ByteRange set​(byte[] bytes)
      Reuse this ByteRange over a new byte[].
      ByteRange set​(byte[] bytes, int offset, int length)
      Reuse this ByteRange over a new byte[].
      ByteRange set​(int capacity)
      Reuse this ByteRange over a new byte[].
      ByteRange setLength​(int length)
      Update the length of this range.
      ByteRange setOffset​(int offset)
      Update the beginning of this range.
      java.lang.String toString()  
      abstract ByteRange unset()
      Nullifies this ByteRange.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • bytes

        protected byte[] bytes
        The array containing the bytes in this range. It will be >= length.
      • offset

        protected int offset
        The index of the first byte in this range. ByteRange.get(0) will return bytes[offset].
      • length

        protected int length
        The number of bytes in the range. Offset + length must be <= bytes.length
      • hash

        protected int hash
        Variable for lazy-caching the hashCode of this range. Useful for frequently used ranges, long-lived ranges, or long ranges.
    • Constructor Detail

      • AbstractByteRange

        public AbstractByteRange()
    • Method Detail

      • getBytes

        public byte[] getBytes()
        Description copied from interface: ByteRange
        The underlying byte[].
        Specified by:
        getBytes in interface ByteRange
      • unset

        public abstract ByteRange unset()
        Description copied from interface: ByteRange
        Nullifies this ByteRange. That is, it becomes a husk, being a range over no byte[] whatsoever.
        Specified by:
        unset in interface ByteRange
        Returns:
        this
      • set

        public ByteRange set​(int capacity)
        Description copied from interface: ByteRange
        Reuse this ByteRange over a new byte[]. offset is set to 0 and length is set to capacity.
        Specified by:
        set in interface ByteRange
        Parameters:
        capacity - the size of a new byte[].
        Returns:
        this
      • set

        public ByteRange set​(byte[] bytes)
        Description copied from interface: ByteRange
        Reuse this ByteRange over a new byte[]. offset is set to 0 and length is set to bytes.length. A null bytes IS supported, in which case this method will behave equivalently to ByteRange.unset().
        Specified by:
        set in interface ByteRange
        Parameters:
        bytes - the array to wrap.
        Returns:
        this
      • set

        public ByteRange set​(byte[] bytes,
                             int offset,
                             int length)
        Description copied from interface: ByteRange
        Reuse this ByteRange over a new byte[]. A null bytes IS supported, in which case this method will behave equivalently to ByteRange.unset(), regardless of the values of offset and length.
        Specified by:
        set in interface ByteRange
        Parameters:
        bytes - The array to wrap.
        offset - The offset into bytes considered the beginning of this range.
        length - The length of this range.
        Returns:
        this.
      • getOffset

        public int getOffset()
        Description copied from interface: ByteRange
        The offset, the index into the underlying byte[] at which this range begins.
        Specified by:
        getOffset in interface ByteRange
        See Also:
        ByteRange.getBytes()
      • setOffset

        public ByteRange setOffset​(int offset)
        Description copied from interface: ByteRange
        Update the beginning of this range. offset + length may not be greater than bytes.length.
        Specified by:
        setOffset in interface ByteRange
        Parameters:
        offset - the new start of this range.
        Returns:
        this.
      • getLength

        public int getLength()
        Description copied from interface: ByteRange
        The length of the range.
        Specified by:
        getLength in interface ByteRange
      • setLength

        public ByteRange setLength​(int length)
        Description copied from interface: ByteRange
        Update the length of this range. offset + length should not be greater than bytes.length.
        Specified by:
        setLength in interface ByteRange
        Parameters:
        length - The new length of this range.
        Returns:
        this.
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface ByteRange
        Returns:
        true when this range is of zero length, false otherwise.
      • isEmpty

        public static boolean isEmpty​(ByteRange range)
        Returns:
        true when range is of zero length, false otherwise.
      • get

        public byte get​(int index)
        Description copied from interface: ByteRange
        Retrieve the byte at index.
        Specified by:
        get in interface ByteRange
        Parameters:
        index - zero-based index into this range.
        Returns:
        single byte at index.
      • get

        public ByteRange get​(int index,
                             byte[] dst)
        Description copied from interface: ByteRange
        Fill dst with bytes from the range, starting from index.
        Specified by:
        get in interface ByteRange
        Parameters:
        index - zero-based index into this range.
        dst - the destination of the copy.
        Returns:
        this.
      • get

        public ByteRange get​(int index,
                             byte[] dst,
                             int offset,
                             int length)
        Description copied from interface: ByteRange
        Fill dst with bytes from the range, starting from index. length bytes are copied into dst, starting at offset.
        Specified by:
        get in interface ByteRange
        Parameters:
        index - zero-based index into this range.
        dst - the destination of the copy.
        offset - the offset into dst to start the copy.
        length - the number of bytes to copy into dst.
        Returns:
        this.
      • getShort

        public short getShort​(int index)
        Description copied from interface: ByteRange
        Retrieve the short value at index
        Specified by:
        getShort in interface ByteRange
        Parameters:
        index - zero-based index into this range
        Returns:
        the short value at index
      • getInt

        public int getInt​(int index)
        Description copied from interface: ByteRange
        Retrieve the int value at index
        Specified by:
        getInt in interface ByteRange
        Parameters:
        index - zero-based index into this range
        Returns:
        the int value at index
      • getLong

        public long getLong​(int index)
        Description copied from interface: ByteRange
        Retrieve the long value at index
        Specified by:
        getLong in interface ByteRange
        Parameters:
        index - zero-based index into this range
        Returns:
        the long value at index
      • getVLong

        public long getVLong​(int index)
        Description copied from interface: ByteRange
        Retrieve the long value at index which is stored as VLong
        Specified by:
        getVLong in interface ByteRange
        Parameters:
        index - zero-based index into this range
        Returns:
        the long value at index which is stored as VLong
      • getVLongSize

        public static int getVLongSize​(long val)
      • put

        public abstract ByteRange put​(int index,
                                      byte val)
        Description copied from interface: ByteRange
        Store val at index.
        Specified by:
        put in interface ByteRange
        Parameters:
        index - the index in the range where val is stored.
        val - the value to store.
        Returns:
        this.
      • put

        public abstract ByteRange put​(int index,
                                      byte[] val)
        Description copied from interface: ByteRange
        Store val at index.
        Specified by:
        put in interface ByteRange
        Parameters:
        index - the index in the range where val is stored.
        val - the value to store.
        Returns:
        this.
      • put

        public abstract ByteRange put​(int index,
                                      byte[] val,
                                      int offset,
                                      int length)
        Description copied from interface: ByteRange
        Store length bytes from val into this range, starting at index. Bytes from val are copied starting at offset into the range.
        Specified by:
        put in interface ByteRange
        Parameters:
        index - position in this range to start the copy.
        val - the value to store.
        offset - the offset in val from which to start copying.
        length - the number of bytes to copy from val.
        Returns:
        this.
      • putInt

        public abstract ByteRange putInt​(int index,
                                         int val)
        Description copied from interface: ByteRange
        Store the int value at index
        Specified by:
        putInt in interface ByteRange
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putLong

        public abstract ByteRange putLong​(int index,
                                          long val)
        Description copied from interface: ByteRange
        Store the long value at index
        Specified by:
        putLong in interface ByteRange
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putShort

        public abstract ByteRange putShort​(int index,
                                           short val)
        Description copied from interface: ByteRange
        Store the short value at index
        Specified by:
        putShort in interface ByteRange
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        this
      • putVLong

        public abstract int putVLong​(int index,
                                     long val)
        Description copied from interface: ByteRange
        Store the long value at index as a VLong
        Specified by:
        putVLong in interface ByteRange
        Parameters:
        index - the index in the range where val is stored
        val - the value to store
        Returns:
        number of bytes written
      • deepCopyToNewArray

        public byte[] deepCopyToNewArray()
        Description copied from interface: ByteRange
        Instantiate a new byte[] with exact length, which is at least 24 bytes + length. Copy the contents of this range into it.
        Specified by:
        deepCopyToNewArray in interface ByteRange
        Returns:
        The newly cloned byte[].
      • deepCopyTo

        public void deepCopyTo​(byte[] destination,
                               int destinationOffset)
        Description copied from interface: ByteRange
        Wrapper for System.arraycopy. Copy the contents of this range into the provided array.
        Specified by:
        deepCopyTo in interface ByteRange
        Parameters:
        destination - Copy to this array
        destinationOffset - First index in the destination array.
      • deepCopySubRangeTo

        public void deepCopySubRangeTo​(int innerOffset,
                                       int copyLength,
                                       byte[] destination,
                                       int destinationOffset)
        Description copied from interface: ByteRange
        Wrapper for System.arraycopy. Copy the contents of this range into the provided array.
        Specified by:
        deepCopySubRangeTo in interface ByteRange
        Parameters:
        innerOffset - Start copying from this index in this source ByteRange. First byte copied is bytes[offset + innerOffset]
        copyLength - Copy this many bytes
        destination - Copy to this array
        destinationOffset - First index in the destination array.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • isHashCached

        protected boolean isHashCached()
      • clearHashCache

        protected void clearHashCache()
      • compareTo

        public int compareTo​(ByteRange other)
        Bitwise comparison of each byte in the array. Unsigned comparison, not paying attention to java's signed bytes.
        Specified by:
        compareTo in interface java.lang.Comparable<ByteRange>
      • toString

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