本文整理汇总了Java中sun.net.idn.Punycode类的典型用法代码示例。如果您正苦于以下问题:Java Punycode类的具体用法?Java Punycode怎么用?Java Punycode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Punycode类属于sun.net.idn包,在下文中一共展示了Punycode类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEncoding
import sun.net.idn.Punycode; //导入依赖的package包/类
public String testEncoding(String inputS) {
char input[] = new char[unicode_max_length];
int codept = 0;
char uplus[] = new char[2];
StringBuffer output;
int c;
/* Read the input code points: */
input_length = 0;
Scanner sc = new Scanner(inputS);
while (sc.hasNext()) { // need to stop at end of line
try {
String next = sc.next();
uplus[0] = next.charAt(0);
uplus[1] = next.charAt(1);
codept = Integer.parseInt(next.substring(2), 16);
} catch (Exception ex) {
fail(invalid_input, inputS);
}
if (uplus[1] != '+' || codept > Integer.MAX_VALUE) {
fail(invalid_input, inputS);
}
if (input_length == unicode_max_length) fail(too_big, inputS);
if (uplus[0] == 'u') case_flags[input_length] = false;
else if (uplus[0] == 'U') case_flags[input_length] = true;
else fail(invalid_input, inputS);
input[input_length++] = (char)codept;
}
/* Encode: */
output_length[0] = ace_max_length;
try {
output = Punycode.encode((new StringBuffer()).append(input, 0, input_length), case_flags);
} catch (Exception e) {
fail(invalid_input, inputS);
// never reach here, just to make compiler happy
return null;
}
testCount++;
return output.toString();
}
示例2: testDecoding
import sun.net.idn.Punycode; //导入依赖的package包/类
public String testDecoding(String inputS) {
char input[] = new char[0];
int pp;
StringBuffer output;
/* Read the Punycode input string and convert to ASCII: */
if (inputS.length() <= ace_max_length+2) {
input = inputS.toCharArray();
} else {
fail(invalid_input, inputS);
}
input_length = input.length;
/* Decode: */
output_length[0] = unicode_max_length;
try {
output = Punycode.decode((new StringBuffer()).append(input, 0, input_length), case_flags);
} catch (Exception e) {
fail(invalid_input, inputS);
// never reach here, just to make compiler happy
return null;
}
/* Output the result: */
StringBuffer result = new StringBuffer();
for (j = 0; j < output.length(); ++j) {
result.append(String.format("%s+%04X ",
case_flags[j] ? "U" : "u",
(int)output.charAt(j) ));
}
testCount++;
return result.substring(0, result.length() - 1);
}
示例3: toUnicodeInternal
import sun.net.idn.Punycode; //导入依赖的package包/类
private static String toUnicodeInternal(String label, int flag) {
boolean[] caseFlags = null;
StringBuffer dest;
// step 1
// find out if all the codepoints in input are ASCII
boolean isASCII = isAllASCII(label);
if(!isASCII){
// step 2
// perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
try {
UCharacterIterator iter = UCharacterIterator.getInstance(label);
dest = namePrep.prepare(iter, flag);
} catch (Exception e) {
// toUnicode never fails; if any step fails, return the input string
return label;
}
} else {
dest = new StringBuffer(label);
}
// step 3
// verify ACE Prefix
if(startsWithACEPrefix(dest)) {
// step 4
// Remove the ACE Prefix
String temp = dest.substring(ACE_PREFIX_LENGTH, dest.length());
try {
// step 5
// Decode using punycode
StringBuffer decodeOut = Punycode.decode(new StringBuffer(temp), null);
// step 6
// Apply toASCII
String toASCIIOut = toASCII(decodeOut.toString(), flag);
// step 7
// verify
if (toASCIIOut.equalsIgnoreCase(dest.toString())) {
// step 8
// return output of step 5
return decodeOut.toString();
}
} catch (Exception ignored) {
// no-op
}
}
// just return the input
return label;
}
示例4: toUnicodeInternal
import sun.net.idn.Punycode; //导入依赖的package包/类
private static String toUnicodeInternal(String label, int flag) {
boolean[] caseFlags = null;
StringBuffer dest;
// step 1
// find out if all the codepoints in input are ASCII
boolean isASCII = isAllASCII(label);
if(!isASCII){
// step 2
// perform the nameprep operation; flag ALLOW_UNASSIGNED is used here
try {
UCharacterIterator iter = UCharacterIterator.getInstance(label);
dest = namePrep.prepare(iter, flag);
} catch (Exception e) {
// toUnicode never fails; if any step fails, return the input string
return label;
}
} else {
dest = new StringBuffer(label);
}
// step 3
// verify ACE Prefix
if(startsWithACEPrefix(dest)) {
// step 4
// Remove the ACE Prefix
String temp = dest.substring(ACE_PREFIX_LENGTH, dest.length());
try {
// step 5
// Decode using punycode
StringBuffer decodeOut = Punycode.decode(new StringBuffer(temp), null);
// step 6
// Apply toASCII
String toASCIIOut = toASCII(decodeOut.toString(), flag);
// step 7
// verify
if (toASCIIOut.equalsIgnoreCase(dest.toString())) {
// step 8
// return output of step 5
return decodeOut.toString();
}
} catch (Exception ignored) {
// no-op
}
}
// just return the input
return label;
}