Class ByteString.LiteralByteString

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<java.lang.Byte>
    Enclosing class:
    ByteString

    private static final class ByteString.LiteralByteString
    extends ByteString.LeafByteString
    This class implements a ByteString backed by a single array of bytes, contiguous in memory. It supports substring by pointing to only a sub-range of the underlying byte array, meaning that a substring will reference the full byte-array of the string it's made from, exactly as with String.
    • Field Detail

      • bytes

        private final byte[] bytes
    • Constructor Detail

      • LiteralByteString

        LiteralByteString​(byte[] bytes)
        Creates a LiteralByteString backed by the given array, without copying.
        Parameters:
        bytes - array to wrap
    • Method Detail

      • internalByteAt

        byte internalByteAt​(int index)
        Description copied from class: ByteString
        Gets the byte at the given index, assumes bounds checking has already been performed.
        Specified by:
        internalByteAt in class ByteString
        Parameters:
        index - index of byte
        Returns:
        the value
      • size

        public int size()
        Description copied from class: ByteString
        Gets the number of bytes.
        Specified by:
        size in class ByteString
        Returns:
        size in bytes
      • substring

        public ByteString substring​(int beginIndex,
                                    int endIndex)
        Description copied from class: ByteString
        Return the substring from beginIndex, inclusive, to endIndex, exclusive.
        Specified by:
        substring in class ByteString
        Parameters:
        beginIndex - start at this index
        endIndex - the last character is the one before this index
        Returns:
        substring sharing underlying data
      • copyToInternal

        protected void copyToInternal​(byte[] target,
                                      int sourceOffset,
                                      int targetOffset,
                                      int numberToCopy)
        Description copied from class: ByteString
        Internal (package private) implementation of ByteString.copyTo(byte[],int,int,int). It assumes that all error checking has already been performed and that numberToCopy > 0.
        Specified by:
        copyToInternal in class ByteString
      • copyTo

        public void copyTo​(java.nio.ByteBuffer target)
        Description copied from class: ByteString
        Copies bytes into a ByteBuffer.

        To copy a subset of bytes, you call this method on the return value of ByteString.substring(int, int). Example: byteString.substring(start, end).copyTo(target)

        Specified by:
        copyTo in class ByteString
        Parameters:
        target - ByteBuffer to copy into.
      • asReadOnlyByteBuffer

        public java.nio.ByteBuffer asReadOnlyByteBuffer()
        Description copied from class: ByteString
        Constructs a read-only java.nio.ByteBuffer whose content is equal to the contents of this byte string. The result uses the same backing array as the byte string, if possible.
        Specified by:
        asReadOnlyByteBuffer in class ByteString
        Returns:
        wrapped bytes
      • asReadOnlyByteBufferList

        public java.util.List<java.nio.ByteBuffer> asReadOnlyByteBufferList()
        Description copied from class: ByteString
        Constructs a list of read-only java.nio.ByteBuffer objects such that the concatenation of their contents is equal to the contents of this byte string. The result uses the same backing arrays as the byte string.

        By returning a list, implementations of this method may be able to avoid copying even when there are multiple backing arrays.

        Specified by:
        asReadOnlyByteBufferList in class ByteString
        Returns:
        a list of wrapped bytes
      • writeTo

        public void writeTo​(java.io.OutputStream outputStream)
                     throws java.io.IOException
        Description copied from class: ByteString
        Writes a copy of the contents of this byte string to the specified output stream argument.
        Specified by:
        writeTo in class ByteString
        Parameters:
        outputStream - the output stream to which to write the data.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • writeToInternal

        void writeToInternal​(java.io.OutputStream outputStream,
                             int sourceOffset,
                             int numberToWrite)
                      throws java.io.IOException
        Description copied from class: ByteString
        Internal version of ByteString.writeTo(OutputStream,int,int) that assumes all error checking has already been done.
        Specified by:
        writeToInternal in class ByteString
        Throws:
        java.io.IOException
      • toStringInternal

        protected java.lang.String toStringInternal​(java.nio.charset.Charset charset)
        Description copied from class: ByteString
        Constructs a new String by decoding the bytes using the specified charset.
        Specified by:
        toStringInternal in class ByteString
        Parameters:
        charset - encode using this charset
        Returns:
        new string
      • isValidUtf8

        public boolean isValidUtf8()
        Description copied from class: ByteString
        Tells whether this ByteString represents a well-formed UTF-8 byte sequence, such that the original bytes can be converted to a String object and then round tripped back to bytes without loss.

        More precisely, returns true whenever:

        
         Arrays.equals(byteString.toByteArray(),
             new String(byteString.toByteArray(), UTF_8).getBytes(UTF_8))
         

        This method returns false for "overlong" byte sequences, as well as for 3-byte sequences that would map to a surrogate character, in accordance with the restricted definition of UTF-8 introduced in Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but (as of 2011) still accepts 3-byte surrogate character byte sequences.

        See the Unicode Standard, Table 3-6 UTF-8 Bit Distribution, and Table 3-7 Well Formed UTF-8 Byte Sequences.

        Specified by:
        isValidUtf8 in class ByteString
        Returns:
        whether the bytes in this ByteString are a well-formed UTF-8 byte sequence
      • equalsInternal

        protected final boolean equalsInternal​(ByteString other)
        Description copied from class: ByteString
        Internal portion of the equals check: as a precondition the caller has already checked most fast properties that are common (including reference identity, null, size, cached hash codes if available) are the same. This method is when we need to check the actual string contents from there.
        Specified by:
        equalsInternal in class ByteString
      • equalsRange

        boolean equalsRange​(ByteString other,
                            int offset,
                            int length)
        Check equality of the substring of given length of this object starting at zero with another ByteString substring starting at offset.
        Specified by:
        equalsRange in class ByteString.LeafByteString
        Parameters:
        other - what to compare a substring in
        offset - offset into other
        length - number of bytes to compare
        Returns:
        true for equality of substrings, else false.
      • partialHash

        protected int partialHash​(int h,
                                  int offset,
                                  int length)
        Description copied from class: ByteString
        Compute the hash across the value bytes starting with the given hash, and return the result. This is used to compute the hash across strings represented as a set of pieces by allowing the hash computation to be continued from piece to piece.
        Specified by:
        partialHash in class ByteString
        Parameters:
        h - starting hash value
        offset - offset into this value to start looking at data values
        length - number of data values to include in the hash computation
        Returns:
        ending hash value
      • newInput

        public java.io.InputStream newInput()
        Description copied from class: ByteString
        Creates an InputStream which can be used to read the bytes.

        The InputStream returned by this method is guaranteed to be completely non-blocking. The method InputStream.available() returns the number of bytes remaining in the stream. The methods InputStream.read(byte[]), InputStream.read(byte[],int,int) and InputStream.skip(long) will read/skip as many bytes as are available. The method InputStream.markSupported() returns true.

        The methods in the returned InputStream might not be thread safe.

        Specified by:
        newInput in class ByteString
        Returns:
        an input stream that returns the bytes of this byte string.