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)`
`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`
`getLength()`
Deprecated.
call `getByteLength()` instead.
`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.
`Utf8`
`setLength(int newLength)`
Deprecated.
call `setByteLength(int)` instead.
`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()`.

### getLength

@Deprecated public int getLength()
Deprecated.
call `getByteLength()` instead.
Return length in bytes.

### getByteLength

public int getByteLength()
Return length in bytes.

### setLength

@Deprecated public Utf8 setLength(int newLength)
Deprecated.
call `setByteLength(int)` instead.
Set length in bytes. Should called whenever byte content changes, even if the length does not change, as this also clears the cached String.

### setByteLength

public Utf8 setByteLength(int newLength)
Set length in bytes. Should called whenever byte content changes, even if the length does not change, as this also clears the cached String.

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