APIdia Launch Partner
Top Description Inners Constructors Methods
com.sun.tools.javac.util

public Class Convert

extends Object
Class Inheritance

Utility class for static conversion methods between numbers and strings in various formats.

Note regarding UTF-8. The JVMS defines its own version of the UTF-8 format so that it contains no zero bytes (modified UTF-8). This is not actually the same as Charset.forName("UTF-8").

See also:

The methods here support modified UTF-8.

This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Convert.Validation

Modified UTF-8 decoding validation levels.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static int
chars2utf(char[]
The array holding the characters to convert.
src
,
int
The start index from which characters are converted.
sindex
,
byte[]
The array holding the converted characters..
dst
,
int
The start index from which converted bytes are written.
dindex
,
int
The maximum number of characters to convert.
len
)

Copy characters in source array to bytes in target array, converting them to Utf8 representation.

public static byte[]
chars2utf(char[]
The array holding the characters.
src
,
int
The start index from which characters are converted.
sindex
,
int
The maximum number of characters to convert.
len
)

Return characters as an array of bytes in Utf8 representation.

public static byte[]
chars2utf(char[]
The array holding the characters.
src
)

Return all characters in given array as an array of bytes in Utf8 representation.

public static List<Name>
public static List<Name>
public static String
escapeUnicode(String s)

Escape all unicode characters in string.

private static boolean
isPrintableAscii(char ch)

Is a character printable ASCII?

public static Name
packagePart(Name classname)

Return the package name of a class name, excluding the trailing '.', "" if not existent.

public static String
packagePart(String classname)

public static String
quote(String s)

Escapes each character in a string that has an escape sequence or is non-printable ASCII.

public static String
quote(char ch, boolean charContext)

Escapes a character if it has an escape sequence or is non-printable ASCII.

public static Name

Returns:

the last part of the qualified name
shortName
(Name
the qualified name
name
)

Return the last part of a qualified name.

public static String

Returns:

the last part of the qualified name
shortName
(String
the string representation of the qualified name
name
)

Return the last part of a qualified name from its string representation

public static int
string2int(String s, int radix)

Convert string to integer.

public static long
string2long(String s, int radix)

Convert string to long integer.

public static byte[]
string2utf(String s)

Return string as an array of bytes in in Utf8 representation.

public static int

Returns:

the index in dst just after the last copied char
utf2chars
(byte[]
The array holding the bytes to convert.
src
,
int
The start index from which bytes are converted.
soff
,
char[]
The array holding the converted characters, or null to just validate
dst
,
int
The start index from which converted characters are written.
doff
,
int
The maximum number of bytes to convert.
len
,
Convert.Validation
Level of validation
validation
)

Decode characters encoded in Modified UTF-8 encoding using the given validation level.

public static char[]

Returns:

The decoded characters in an array.
utf2chars
(byte[]
The array holding the bytes.
src
,
int
The start index from which bytes are converted.
sindex
,
int
The maximum number of bytes to convert.
len
,
Convert.Validation
Level of validation
validation
)

Decode characters encoded in Modified UTF-8 encoding.

public static String
utf2string(byte[]
The array holding the bytes.
src
,
int
The start index from which bytes are converted.
sindex
,
int
The maximum number of bytes to convert.
len
,
Convert.Validation
Level of validation
validation
)

Decode a String encoded in Modified UTF-8 encoding.

public static int

Returns:

the number of encoded characters
utfNumChars
(byte[]
data buffer
buf
,
int
starting offset of UTF-8 data
off
,
int
number of bytes of UTF-8 data
len
)

Count the number of characters encoded in a Modified UTF-8 encoding.

public static void
utfValidate(byte[]
Buffer containing data
buf
,
int
Data starting offset
off
,
int
Data length
len
,
Convert.Validation
Level of validation
validation
)

Validate the given Modified UTF-8 encoding using the given validation level.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Convertback to summary
public Convert()

Method Detail

chars2utfback to summary
public static int chars2utf(char[] src, int sindex, byte[] dst, int dindex, int len)

Copy characters in source array to bytes in target array, converting them to Utf8 representation. The target array must be large enough to hold the result. returns first index in `dst' past the last copied byte.

Parameters
src:char[]

The array holding the characters to convert.

sindex:int

The start index from which characters are converted.

dst:byte[]

The array holding the converted characters..

dindex:int

The start index from which converted bytes are written.

len:int

The maximum number of characters to convert.

chars2utfback to summary
public static byte[] chars2utf(char[] src, int sindex, int len)

Return characters as an array of bytes in Utf8 representation.

Parameters
src:char[]

The array holding the characters.

sindex:int

The start index from which characters are converted.

len:int

The maximum number of characters to convert.

chars2utfback to summary
public static byte[] chars2utf(char[] src)

Return all characters in given array as an array of bytes in Utf8 representation.

Parameters
src:char[]

The array holding the characters.

classCandidatesback to summary
public static List<Name> classCandidates(Name name)
enclosingCandidatesback to summary
public static List<Name> enclosingCandidates(Name name)
escapeUnicodeback to summary
public static String escapeUnicode(String s)

Escape all unicode characters in string.

isPrintableAsciiback to summary
private static boolean isPrintableAscii(char ch)

Is a character printable ASCII?

packagePartback to summary
public static Name packagePart(Name classname)

Return the package name of a class name, excluding the trailing '.', "" if not existent.

packagePartback to summary
public static String packagePart(String classname)
quoteback to summary
public static String quote(String s)

Escapes each character in a string that has an escape sequence or is non-printable ASCII. Leaves non-ASCII characters alone.

quoteback to summary
public static String quote(char ch, boolean charContext)

Escapes a character if it has an escape sequence or is non-printable ASCII. Leaves non-ASCII characters alone.

shortNameback to summary
public static Name shortName(Name name)

Return the last part of a qualified name.

Parameters
name:Name

the qualified name

Returns:Name

the last part of the qualified name

shortNameback to summary
public static String shortName(String name)

Return the last part of a qualified name from its string representation

Parameters
name:String

the string representation of the qualified name

Returns:String

the last part of the qualified name

string2intback to summary
public static int string2int(String s, int radix) throws NumberFormatException

Convert string to integer.

string2longback to summary
public static long string2long(String s, int radix) throws NumberFormatException

Convert string to long integer.

string2utfback to summary
public static byte[] string2utf(String s)

Return string as an array of bytes in in Utf8 representation.

utf2charsback to summary
public static int utf2chars(byte[] src, int soff, char[] dst, int doff, int len, Convert.Validation validation) throws InvalidUtfException

Decode characters encoded in Modified UTF-8 encoding using the given validation level. Reject any invalid data by throwing an InvalidUtfException. Parameters are as in System.arraycopy():

Parameters
src:byte[]

The array holding the bytes to convert.

soff:int

The start index from which bytes are converted.

dst:char[]

The array holding the converted characters, or null to just validate

doff:int

The start index from which converted characters are written.

len:int

The maximum number of bytes to convert.

validation:Convert.Validation

Level of validation

Returns:int

the index in dst just after the last copied char

Exceptions
InvalidUtfException:
  • if invalid Modified UTF-8 is encountered
  • if validation is not Validation#NONE and invalid Modified UTF-8 is encountered
utf2charsback to summary
public static char[] utf2chars(byte[] src, int sindex, int len, Convert.Validation validation) throws InvalidUtfException

Decode characters encoded in Modified UTF-8 encoding.

Parameters
src:byte[]

The array holding the bytes.

sindex:int

The start index from which bytes are converted.

len:int

The maximum number of bytes to convert.

validation:Convert.Validation

Level of validation

Returns:char[]

The decoded characters in an array.

Exceptions
InvalidUtfException:
if validation is not Validation#NONE and invalid Modified UTF-8 is encountered
utf2stringback to summary
public static String utf2string(byte[] src, int sindex, int len, Convert.Validation validation) throws InvalidUtfException

Decode a String encoded in Modified UTF-8 encoding.

Parameters
src:byte[]

The array holding the bytes.

sindex:int

The start index from which bytes are converted.

len:int

The maximum number of bytes to convert.

validation:Convert.Validation

Level of validation

Exceptions
InvalidUtfException:
if validation is not Validation#NONE and invalid Modified UTF-8 is encountered
utfNumCharsback to summary
public static int utfNumChars(byte[] buf, int off, int len)

Count the number of characters encoded in a Modified UTF-8 encoding. This method does not check for invalid data.

Parameters
buf:byte[]

data buffer

off:int

starting offset of UTF-8 data

len:int

number of bytes of UTF-8 data

Returns:int

the number of encoded characters

utfValidateback to summary
public static void utfValidate(byte[] buf, int off, int len, Convert.Validation validation) throws InvalidUtfException

Validate the given Modified UTF-8 encoding using the given validation level. Reject invalid data by throwing an InvalidUtfException.

Note

there is no point in calling this method with Validation#NONE.

Parameters
buf:byte[]

Buffer containing data

off:int

Data starting offset

len:int

Data length

validation:Convert.Validation

Level of validation

Exceptions
InvalidUtfException:
if validation is not Validation#NONE and invalid Modified UTF-8 is encountered
com.sun.tools.javac.util back to summary

public final Enum Convert.Validation

extends Enum<Convert.Validation>
Class Inheritance

Modified UTF-8 decoding validation levels.

Field Summary

Modifier and TypeField and Description
private final boolean
private final boolean
private final boolean
public static final Convert.Validation
NONE

Do zero validation of UTF-8, i.e., always decode something without error.

public static final Convert.Validation
PREJDK14

Do validation in accordance with the pre-JDK 1.4 Java class file format, which allows (a) the NUL character \u0000 to be encoded as a single byte and (b) longer-than-necessary encodings (e.g., three bytes instead of two).

public static final Convert.Validation
STRICT

Do strict validation.

Constructor Summary

AccessConstructor and Description
private
Validation(boolean allowSingleByteNul, boolean allowLongEncoding, boolean allowAnything)

Method Summary

Modifier and TypeMethod and Description
public boolean
allowAnything()

Whether to allow anything, including truncated characters and bogus flag bits.

public boolean
allowLongEncoding()

Whether to allow characters to be encoded using more bytes than required.

public boolean
allowSingleByteNul()

Whether to allow the NUL character \u0000 to be encoded as a single byte.

public static Convert.Validation
public static Convert.Validation[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

allowAnythingback to summary
private final boolean allowAnything
allowLongEncodingback to summary
private final boolean allowLongEncoding
allowSingleByteNulback to summary
private final boolean allowSingleByteNul
NONEback to summary
public static final Convert.Validation NONE

Do zero validation of UTF-8, i.e., always decode something without error. When this is used, InvalidUtfException is never thrown.

PREJDK14back to summary
public static final Convert.Validation PREJDK14

Do validation in accordance with the pre-JDK 1.4 Java class file format, which allows (a) the NUL character \u0000 to be encoded as a single byte and (b) longer-than-necessary encodings (e.g., three bytes instead of two).

STRICTback to summary
public static final Convert.Validation STRICT

Do strict validation. At this level, each character has only one valid encoding.

Constructor Detail

Validationback to summary
private Validation(boolean allowSingleByteNul, boolean allowLongEncoding, boolean allowAnything)

Method Detail

allowAnythingback to summary
public boolean allowAnything()

Whether to allow anything, including truncated characters and bogus flag bits.

allowLongEncodingback to summary
public boolean allowLongEncoding()

Whether to allow characters to be encoded using more bytes than required.

allowSingleByteNulback to summary
public boolean allowSingleByteNul()

Whether to allow the NUL character \u0000 to be encoded as a single byte. Modified UTF-8 specifies that it be encoded in two bytes.

valueOfback to summary
public static Convert.Validation valueOf(String name)
valuesback to summary
public static Convert.Validation[] values()