Class Utf8

java.lang.Object
org.apache.avro.util.Utf8
All Implemented Interfaces:
Externalizable, Serializable, CharSequence, Comparable<Utf8>

public class Utf8 extends Object implements Comparable<Utf8>, CharSequence, Externalizable
A Utf8 string. Unlike String, instances are mutable. This is more efficient than String when reading or writing a sequence of values, as a single instance may be reused.
See Also:
  • Constructor Details

    • Utf8

      public Utf8()
    • Utf8

      public Utf8(String string)
    • Utf8

      public Utf8(Utf8 other)
    • Utf8

      public Utf8(byte[] bytes)
  • Method Details

    • getBytes

      public byte[] getBytes()
      Return UTF-8 encoded bytes. Only valid through getByteLength() assuming the bytes have been fully copied into the underlying buffer from the source.
      Returns:
      a reference to the underlying byte array
      See Also:
    • getByteLength

      public int getByteLength()
      Return length in bytes.
    • setByteLength

      public Utf8 setByteLength(int newLength)
      Set length in bytes. When calling this method, even if the new length is the same as the current length, the cached contents of this Utf8 object will be wiped out. After calling this method, no assumptions should be made about the internal state (e.g., contents, hashcode, equality, etc.) of this Utf8 String other than the internal buffer being large enough to accommodate a String of the new length. This should be called immediately before reading a String from the underlying data source.
      Parameters:
      newLength - the new length of the underlying buffer
      Returns:
      a reference to this object.
      See Also:
    • set

      public Utf8 set(String string)
      Set to the contents of a String.
    • set

      public Utf8 set(Utf8 other)
    • toString

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Utf8 that)
      Specified by:
      compareTo in interface Comparable<Utf8>
    • charAt

      public char charAt(int index)
      Specified by:
      charAt in interface CharSequence
    • length

      public int length()
      Specified by:
      length in interface CharSequence
    • subSequence

      public CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
    • getBytesFor

      public static byte[] getBytesFor(String str)
      Gets the UTF-8 bytes for a String
    • writeExternal

      public void writeExternal(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException