Package org.apache.avro.util

# 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:
  - Serialized Form

## Constructor Summary

Constructors
Constructor
Description
`Utf8()`
`Utf8(byte[] bytes)`
`Utf8(String string)`
`Utf8(Utf8 other)`

## Method Summary

Modifier and Type
Method
Description
`char`
`charAt(int index)`
`static int`
`compareSequences(CharSequence cs1, CharSequence cs2)`
`int`
`compareTo(Utf8 that)`
`boolean`
`equals(Object o)`
`int`
`getByteLength()`
Return length in bytes.
`byte[]`
`getBytes()`
Return UTF-8 encoded bytes.
`static byte[]`
`getBytesFor(String str)`
Gets the UTF-8 bytes for a String
`int`
`hashCode()`
`int`
`length()`
`void`
`readExternal(ObjectInput in)`
`Utf8`
`set(String string)`
Set to the contents of a String.
`Utf8`
`set(Utf8 other)`
`Utf8`
`setByteLength(int newLength)`
Set length in bytes.
`CharSequence`
`subSequence(int start, int end)`
`String`
`toString()`
`void`
`writeExternal(ObjectOutput out)`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.lang.CharSequence

`chars, codePoints`

## 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:
  - `setByteLength(int)`

### 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:
  - `BinaryDecoder.readString(Utf8)`

### 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`

### compareSequences

public static int compareSequences(CharSequence cs1, CharSequence cs2)