Top Description Constructors Methods
com.google.common.base

public final Class Utf8

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkPositionIndexes, java.lang.Character.MAX_SURROGATE, .Character.MIN_SURROGATE

Low-level, high-performance utility methods related to the UTF-8 character encoding. UTF-8 is defined in section D92 of The Unicode Standard Core Specification, Chapter 3.

The variant of UTF-8 implemented by this class is the restricted definition of UTF-8 introduced in Unicode 3.1. One implication of this is that it rejects "non-shortest form" byte sequences, even though the JDK decoder may accept them.

Authors
Martin Buchholz, Clément Roux
Since
16.0

Constructor Summary

AccessConstructor and Description
private
Utf8()

Method Summary

Modifier and TypeMethod and Description
public static int
encodedLength(CharSequence sequence)

Returns the number of bytes in the UTF-8-encoded form of sequence.

private static int
encodedLengthGeneral(CharSequence sequence, int start)

public static boolean
isWellFormed(byte[] bytes)

Returns true if bytes is a well-formed UTF-8 byte sequence according to Unicode 6.0.

public static boolean
isWellFormed(byte[]
the input buffer
bytes
,
int
the offset in the buffer of the first byte to read
off
,
int
the number of bytes to read from the buffer
len
)

Returns whether the given byte array slice is a well-formed UTF-8 byte sequence, as defined by isWellFormed(byte[]).

private static boolean
isWellFormedSlowPath(byte[] bytes, int off, int end)

private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Utf8back to summary
private Utf8()

Method Detail

encodedLengthback to summary
public static int encodedLength(CharSequence sequence)

Returns the number of bytes in the UTF-8-encoded form of sequence. For a string, this method is equivalent to string.getBytes(UTF_8).length, but is more efficient in both time and space.

Exceptions
IllegalArgumentException:
if sequence contains ill-formed UTF-16 (unpaired surrogates)
encodedLengthGeneralback to summary
private static int encodedLengthGeneral(CharSequence sequence, int start)
isWellFormedback to summary
public static boolean isWellFormed(byte[] bytes)

Returns true if bytes is a well-formed UTF-8 byte sequence according to Unicode 6.0. Note that this is a stronger criterion than simply whether the bytes can be decoded. For example, some versions of the JDK decoder will accept "non-shortest form" byte sequences, but encoding never reproduces these. Such byte sequences are not considered well-formed.

This method returns true if and only if Arrays.equals(bytes, new String(bytes, UTF_8).getBytes(UTF_8)) does, but is more efficient in both time and space.

isWellFormedback to summary
public static boolean isWellFormed(byte[] bytes, int off, int len)

Returns whether the given byte array slice is a well-formed UTF-8 byte sequence, as defined by isWellFormed(byte[]). Note that this can be false even when isWellFormed(bytes) is true.

Parameters
bytes:byte[]

the input buffer

off:int

the offset in the buffer of the first byte to read

len:int

the number of bytes to read from the buffer

isWellFormedSlowPathback to summary
private static boolean isWellFormedSlowPath(byte[] bytes, int off, int end)
unpairedSurrogateMsgback to summary
private static String unpairedSurrogateMsg(int i)