Top Description Fields Constructors Methods
org.bouncycastle.util.encoders

public Class UTF8

extends Object
Class Inheritance

Utilities for working with UTF-8 encodings.

Decoding of UTF-8 is based on a presentation by Bob Steagall at CppCon2018 (see https://github.com/BobSteagall/CppCon2018). It uses a Deterministic Finite Automaton (DFA) to recognize and decode multi-byte code points.

Field Summary

Modifier and TypeField and Description
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final short[]
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte
private static final byte[]

Constructor Summary

AccessConstructor and Description
public
UTF8()

Method Summary

Modifier and TypeMethod and Description
private static void
fill(byte[] table, int first, int last, byte b)

public static int

Returns:

The number of UTF-16 code units written to utf16 (beginning from index 0), or else -1 if the input was either malformed or encoded any unmappable characters, or if the utf16 is too small.
transcodeToUTF16
(byte[]
A non-null array containing a well-formed UTF-8 encoding.
utf8
,
char[]
A non-null array, at least as long as the utf8 array in order to ensure the output will fit.
utf16
)

Transcode a UTF-8 encoding into a UTF-16 representation.

public static int

Returns:

The number of UTF-16 code units written to utf16 (beginning from index 0), or else -1 if the input was either malformed or encoded any unmappable characters, or if the utf16 is too small.
transcodeToUTF16
(byte[]
A non-null array containing a well-formed UTF-8 encoding.
utf8
,
int
start position in the array for the well-formed encoding.
utf8Off
,
int
length in bytes of the well-formed encoding.
utf8Length
,
char[]
A non-null array, at least as long as the utf8 array in order to ensure the output will fit.
utf16
)

Transcode a UTF-8 encoding into a UTF-16 representation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

C_CR1back to summary
private static final byte C_CR1
C_CR2back to summary
private static final byte C_CR2
C_CR3back to summary
private static final byte C_CR3
C_ILLback to summary
private static final byte C_ILL
C_L2Aback to summary
private static final byte C_L2A
C_L3Aback to summary
private static final byte C_L3A
C_L3Bback to summary
private static final byte C_L3B
C_L3Cback to summary
private static final byte C_L3C
C_L4Aback to summary
private static final byte C_L4A
C_L4Bback to summary
private static final byte C_L4B
C_L4Cback to summary
private static final byte C_L4C
firstUnitTableback to summary
private static final short[] firstUnitTable
S_CS1back to summary
private static final byte S_CS1
S_CS2back to summary
private static final byte S_CS2
S_CS3back to summary
private static final byte S_CS3
S_ENDback to summary
private static final byte S_END
S_ERRback to summary
private static final byte S_ERR
S_P3Aback to summary
private static final byte S_P3A
S_P3Bback to summary
private static final byte S_P3B
S_P4Aback to summary
private static final byte S_P4A
S_P4Bback to summary
private static final byte S_P4B
transitionTableback to summary
private static final byte[] transitionTable

Constructor Detail

UTF8back to summary
public UTF8()

Method Detail

fillback to summary
private static void fill(byte[] table, int first, int last, byte b)
transcodeToUTF16back to summary
public static int transcodeToUTF16(byte[] utf8, char[] utf16)

Transcode a UTF-8 encoding into a UTF-16 representation. In the general case the output utf16 array should be at least as long as the input utf8 one to handle arbitrary inputs. The number of output UTF-16 code units is returned, or -1 if any errors are encountered (in which case an arbitrary amount of data may have been written into the output array). Errors that will be detected are malformed UTF-8, including incomplete, truncated or "overlong" encodings, and unmappable code points. In particular, no unmatched surrogates will be produced. An error will also result if utf16 is found to be too small to store the complete output.

Parameters
utf8:byte[]

A non-null array containing a well-formed UTF-8 encoding.

utf16:char[]

A non-null array, at least as long as the utf8 array in order to ensure the output will fit.

Returns:int

The number of UTF-16 code units written to utf16 (beginning from index 0), or else -1 if the input was either malformed or encoded any unmappable characters, or if the utf16 is too small.

transcodeToUTF16back to summary
public static int transcodeToUTF16(byte[] utf8, int utf8Off, int utf8Length, char[] utf16)

Transcode a UTF-8 encoding into a UTF-16 representation. In the general case the output utf16 array should be at least as long as the input length from utf8 to handle arbitrary inputs. The number of output UTF-16 code units is returned, or -1 if any errors are encountered (in which case an arbitrary amount of data may have been written into the output array). Errors that will be detected are malformed UTF-8, including incomplete, truncated or "overlong" encodings, and unmappable code points. In particular, no unmatched surrogates will be produced. An error will also result if utf16 is found to be too small to store the complete output.

Parameters
utf8:byte[]

A non-null array containing a well-formed UTF-8 encoding.

utf8Off:int

start position in the array for the well-formed encoding.

utf8Length:int

length in bytes of the well-formed encoding.

utf16:char[]

A non-null array, at least as long as the utf8 array in order to ensure the output will fit.

Returns:int

The number of UTF-16 code units written to utf16 (beginning from index 0), or else -1 if the input was either malformed or encoded any unmappable characters, or if the utf16 is too small.