Interface PrimitiveArrays.Bytes

  • Enclosing class:
    PrimitiveArrays

    public static interface PrimitiveArrays.Bytes
    An array of bytes.

    Implementations will be thread-safe if the underlying data is not mutated. Users should ensure the underlying data is not mutated in order to get predictable behaviour. Any buffering should be done internally.

    Implementations may support arrays > 2GB in size like so:

    
     new Bytes() {
       byte get(long position) {
         if (position < b1.length) {
           return b1[Ints.checkedCast(position)];
         }
         return b2[Ints.checkedCast(position - b2.length)];
       }
       long length() { return b1.length + b2.length; }
     }
     
    • Method Detail

      • get

        byte get​(long position)
        Returns the byte at position position.

        Throws an IndexOutOfBoundsException if the absolute get on the underlying implementation fails.

      • length

        long length()
        Returns the length of this array.
      • fromByteBuffer

        @GwtIncompatible("ByteBuffer")
        static PrimitiveArrays.Bytes fromByteBuffer​(java.nio.ByteBuffer buffer)
        Returns a PrimitiveArrays.Bytes wrapping buffer.

        The returned array starts from index 0 of buffer, and its length is buffer.limit().

      • toInputStream

        default java.io.InputStream toInputStream​(long offset)
        Returns an InputStream wrapping this array starting at offset.
      • toInputStream

        default java.io.InputStream toInputStream​(PrimitiveArrays.Cursor cursor)
        Returns an InputStream wrapping this array starting at cursor.position.

        cursor.position is incremented for each byte read from the returned InputStream.

      • toInputStream

        default java.io.InputStream toInputStream()
        Returns an InputStream wrapping this array starting at the 0th byte.
      • writeTo

        default void writeTo​(java.io.OutputStream output)
                      throws java.io.IOException
        Writes this array to output.
        Throws:
        java.io.IOException
      • readVarint64

        default long readVarint64​(PrimitiveArrays.Cursor cursor)
        Returns a unsigned integer consisting of numBytes bytes read from this array at cursor.position in little-endian format as an unsigned 64-bit integer.

        cursor.position is updated to the index of the first byte following the varint64.

      • readUintWithLength

        default long readUintWithLength​(PrimitiveArrays.Cursor cursor,
                                        int numBytes)
        Returns a unsigned integer consisting of numBytes bytes read from this array at cursor.position in little-endian format as an unsigned 64-bit integer.

        cursor.position is updated to the index of the first byte following the uint.

        This method is not compatible with readVarint64(Cursor).

      • readLittleEndianDouble

        default double readLittleEndianDouble​(long position)
        Returns a little-endian double read from this array at position.