Class StringShort

All Implemented Interfaces:
IValidated, IWriteable, Comparable<AString>

public class StringShort extends AString
Class representing a short CVM string, backed by a flat Blob USed for most small strings, and acts as the leaf chunk for StringTrees
  • Field Details

    • MAX_EMBEDDED_STRING_LENGTH

      public static final int MAX_EMBEDDED_STRING_LENGTH
      Length of longest StringShort value that is embedded Basically max embedded length minus tag byte and 2-byte length
      See Also:
    • MAX_LENGTH

      public static final int MAX_LENGTH
      Length of longest StringShort value in bytes. Use Blob as base.
      See Also:
    • MAX_ENCODING_LENGTH

      public static final int MAX_ENCODING_LENGTH
    • EMPTY

      public static final StringShort EMPTY
      The canonical empty String
  • Constructor Details

    • StringShort

      protected StringShort(Blob data)
    • StringShort

      protected StringShort(byte[] data)
    • StringShort

      protected StringShort(byte[] data, int offset, int length)
  • Method Details

    • create

      public static StringShort create(String string)
      Creates a StringShort instance from a regular Java String
      Parameters:
      string - String to wrap as StringShort
      Returns:
      StringShort instance, or null if String is of invalid size
    • create

      public static StringShort create(AArrayBlob b)
      Creates a StringShort instance from a Blob of UTF-8 data. Shares underlying array.
      Parameters:
      b - Array Blob to convert to StringShort
      Returns:
      StringShort instance
    • byteAt

      protected byte byteAt(long index)
      Description copied from class: AString
      Gets a byte at the specified index. Returns -1 (0xff) if outside String.
      Specified by:
      byteAt in class AString
      Parameters:
      index - Index into String (byte position)
      Returns:
      Raw byte value
    • intAt

      public int intAt(long index)
      Description copied from class: AString
      Gets 32 bytes integer at given position. Extends with 255 (invalid UTF-8) if needed. The main purpose of this function is to enable fast peeking at UTF-8 characters
      Overrides:
      intAt in class AString
      Parameters:
      index - Index into String (byte position)
      Returns:
      Raw integer value
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Description copied from class: AString
      Encode the data of this String. Assumes tag already written
      Specified by:
      encodeRaw in class AString
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRawData

      public int encodeRawData(byte[] bs, int pos)
      Description copied from class: AString
      Encode the raw UTF-8 data of this String. Assumes tag/length already written
      Specified by:
      encodeRawData in class AString
      Parameters:
      bs - Byte array to encode to
      pos - Position in target array to write to
      Returns:
      End position in array after encoding
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.
      Returns:
      The estimated size for the binary representation of this object.
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      Specified by:
      isCanonical in class ACell
      Returns:
      true if the object is in canonical format, false otherwise
    • isCVMValue

      public final boolean isCVMValue()
      Description copied from class: ACell
      Returns true if this Cell represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false, pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures
      Specified by:
      isCVMValue in class ACell
      Returns:
      true if the object is a CVM Value, false otherwise
    • isEmbedded

      public boolean isEmbedded()
      Description copied from class: ACell
      Determines if this Cell Represents an embedded object. Embedded objects are encoded directly into the encoding of the containing Cell (avoiding the need for a hashed reference). Subclasses should override this if they have a cheap O(1) way to determine if they are embedded or otherwise.
      Overrides:
      isEmbedded in class ACell
      Returns:
      true if Cell is embedded, false otherwise
    • getRefCount

      public int getRefCount()
      Description copied from class: ACell
      Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • read

      public static AString read(long length, ByteBuffer bb)
      Read a StringShort from a ByteBuffer. Assumes tag and length already read and correct.
      Parameters:
      length - Length in number of chars to read
      bb - ByteBuffer to read from
      Returns:
      AString instance
    • toBlob

      public Blob toBlob()
      Description copied from class: AString
      Converts this String to a Blob byte representation. Must round trip all values.
      Specified by:
      toBlob in class AString
      Returns:
      Blob representation of UTF-8 String
    • equals

      public boolean equals(ACell a)
      Description copied from class: ACell
      Checks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store.
      Overrides:
      equals in class ACell
      Parameters:
      a - Cell to compare with. May be null.
      Returns:
      True if this cell is equal to the other object
    • equals

      public boolean equals(StringShort a)
    • compareTo

      public int compareTo(AString o)
      Specified by:
      compareTo in interface Comparable<AString>
      Specified by:
      compareTo in class AString
    • toCanonical

      public ACell toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Returns this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class ACell
      Returns:
      Canonical version of Cell
    • writeToBuffer

      protected void writeToBuffer(ByteBuffer bb)
      Specified by:
      writeToBuffer in class AString
    • slice

      public AString slice(long start, long end)
      Description copied from class: AString
      Gets a slice of this string, or null if not a valid slice
      Specified by:
      slice in class AString
      Parameters:
      start - Start index (inclusive)
      end - End index (Exclusive)
      Returns:
      Specified substring
    • printEscaped

      protected void printEscaped(BlobBuilder sb, long start, long end)
      Description copied from class: AString
      Prints this string as escaped UTF-8
      Specified by:
      printEscaped in class AString