2993 |
{ |
{ |
2994 |
return (char) (((val >> 8) & 0xff) | ((val << 8) & 0xff00)); |
return (char) (((val >> 8) & 0xff) | ((val << 8) & 0xff00)); |
2995 |
} |
} |
2996 |
|
|
2997 |
|
/** |
2998 |
|
* Converts a unicode code point to a UTF-16 representation of that |
2999 |
|
* code point. |
3000 |
|
* |
3001 |
|
* @param codePoint the unicode code point |
3002 |
|
* |
3003 |
|
* @return the UTF-16 representation of that code point |
3004 |
|
* |
3005 |
|
* @throws IllegalArgumentException if the code point is not a valid |
3006 |
|
* unicode code point |
3007 |
|
* |
3008 |
|
* @since 1.5 |
3009 |
|
*/ |
3010 |
|
public static char[] toChars(int codePoint) |
3011 |
|
{ |
3012 |
|
char[] result = new char[charCount(codePoint)]; |
3013 |
|
int ignore = toChars(codePoint, result, 0); |
3014 |
|
return result; |
3015 |
|
} |
3016 |
|
|
3017 |
|
/** |
3018 |
|
* Converts a unicode code point to its UTF-16 representation. |
3019 |
|
* |
3020 |
|
* @param codePoint the unicode code point |
3021 |
|
* @param dst the target char array |
3022 |
|
* @param dstIndex the start index for the target |
3023 |
|
* |
3024 |
|
* @return number of characters written to <code>dst</code> |
3025 |
|
* |
3026 |
|
* @throws IllegalArgumentException if <code>codePoint</code> is not a |
3027 |
|
* valid unicode code point |
3028 |
|
* @throws NullPointerException if <code>dst</code> is <code>null</code> |
3029 |
|
* @throws IndexOutOfBoundsException if <code>dstIndex</code> is not valid |
3030 |
|
* in <code>dst</code> or if the UTF-16 representation does not |
3031 |
|
* fit into <code>dst</code> |
3032 |
|
* |
3033 |
|
* @since 1.5 |
3034 |
|
*/ |
3035 |
|
public static int toChars(int codePoint, char[] dst, int dstIndex) |
3036 |
|
{ |
3037 |
|
if (!isValidCodePoint(codePoint)) |
3038 |
|
{ |
3039 |
|
throw new IllegalArgumentException("not a valid code point: " |
3040 |
|
+ codePoint); |
3041 |
|
} |
3042 |
|
|
3043 |
|
int result; |
3044 |
|
if (isSupplementaryCodePoint(codePoint)) |
3045 |
|
{ |
3046 |
|
// Write second char first to cause IndexOutOfBoundsException |
3047 |
|
// immediately. |
3048 |
|
dst[dstIndex + 1] = (char) ((codePoint & 0x3ff) |
3049 |
|
+ (int) MIN_LOW_SURROGATE ); |
3050 |
|
dst[dstIndex] = (char) ((codePoint >> 10) + (int) MIN_HIGH_SURROGATE); |
3051 |
|
result = 2; |
3052 |
|
} |
3053 |
|
else |
3054 |
|
{ |
3055 |
|
dst[dstIndex] = (char) codePoint; |
3056 |
|
result = 1; |
3057 |
|
} |
3058 |
|
return result; |
3059 |
|
} |
3060 |
|
|
3061 |
|
/** |
3062 |
|
* Return number of 16-bit characters required to represent the given |
3063 |
|
* code point. |
3064 |
|
* |
3065 |
|
* @param codePoint a unicode code point |
3066 |
|
* |
3067 |
|
* @return 2 if codePoint >= 0x10000, 1 otherwise. |
3068 |
|
* |
3069 |
|
* @since 1.5 |
3070 |
|
*/ |
3071 |
|
public static int charCount(int codePoint) |
3072 |
|
{ |
3073 |
|
return |
3074 |
|
(codePoint >= MIN_SUPPLEMENTARY_CODE_POINT) |
3075 |
|
? 2 |
3076 |
|
: 1; |
3077 |
|
} |
3078 |
|
|
3079 |
|
/** |
3080 |
|
* Determines whether the specified code point is |
3081 |
|
* in the range 0x10000 .. 0x10FFFF, i.e. the character is within the Unicode |
3082 |
|
* supplementary character range. |
3083 |
|
* |
3084 |
|
* @param codePoint a Unicode code point |
3085 |
|
* |
3086 |
|
* @return <code>true</code> if code point is in supplementary range |
3087 |
|
* |
3088 |
|
* @since 1.5 |
3089 |
|
*/ |
3090 |
|
public static boolean isSupplementaryCodePoint(int codePoint) |
3091 |
|
{ |
3092 |
|
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT |
3093 |
|
&& codePoint <= MAX_CODE_POINT; |
3094 |
|
} |
3095 |
|
|
3096 |
|
/** |
3097 |
|
* Determines whether the specified code point is |
3098 |
|
* in the range 0x0000 .. 0x10FFFF, i.e. it is a valid Unicode code point. |
3099 |
|
* |
3100 |
|
* @param codePoint a Unicode code point |
3101 |
|
* |
3102 |
|
* @return <code>true</code> if code point is valid |
3103 |
|
* |
3104 |
|
* @since 1.5 |
3105 |
|
*/ |
3106 |
|
public static boolean isValidCodePoint(int codePoint) |
3107 |
|
{ |
3108 |
|
return codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT; |
3109 |
|
} |
3110 |
} // class Character |
} // class Character |