1/** 2******************************************************************************* 3* Copyright (C) 1996-2006, International Business Machines Corporation and * 4* others. All Rights Reserved. * 5******************************************************************************* 6* 7******************************************************************************* 8*/ 9 10package com.ibm.icu4jni.charset; 11 12/** 13 * Class for accessing the underlying JNI methods 14 * @internal ICU 2.4 15 */ 16final class NativeConverter{ 17 18 //Native methods 19 20 /** 21 * Converts an array of bytes containing characters in an external 22 * encoding into an array of Unicode characters. This method allows 23 * a buffer by buffer conversion of a data stream. The state of the 24 * conversion is saved between calls to convert. Among other things, 25 * this means multibyte input sequences can be split between calls. 26 * If a call to convert results in an Error, the conversion may be 27 * continued by calling convert again with suitably modified parameters. 28 * All conversions should be finished with a call to the flush method. 29 * 30 * @param converterHandle Address of converter object created by C code 31 * @param input byte array containing text to be converted. 32 * @param inEnd stop conversion at this offset in input array (exclusive). 33 * @param output character array to receive conversion result. 34 * @param outEnd stop writing to output array at this offset (exclusive). 35 * @param data integer array containing the following data 36 * data[0] = inputOffset 37 * data[1] = outputOffset 38 * @return int error code returned by ICU 39 * @internal ICU 2.4 40 */ 41 42 public static final native int convertByteToChar( long converterHandle, 43 byte[] input, int inEnd, 44 char[] output, int outEnd, 45 int[] data, 46 boolean flush); 47 /** 48 * Converts an array of bytes containing characters in an external 49 * encoding into an array of Unicode characters. This method allows 50 * a buffer by buffer conversion of a data stream. The state of the 51 * conversion is saved between calls to convert. Among other things, 52 * this means multibyte input sequences can be split between calls. 53 * If a call to convert results in an Error, the conversion may be 54 * continued by calling convert again with suitably modified parameters. 55 * All conversions should be finished with a call to the flush method. 56 * 57 * @param converterHandle Address of converter object created by C code 58 * @param input byte array containing text to be converted. 59 * @param inEnd stop conversion at this offset in input array (exclusive). 60 * @param output character array to receive conversion result. 61 * @param outEnd stop writing to output array at this offset (exclusive). 62 * @param data integer array containing the following data 63 * data[0] = inputOffset 64 * data[1] = outputOffset 65 * @return int error code returned by ICU 66 * @internal ICU 2.4 67 */ 68 public static final native int decode( long converterHandle, 69 byte[] input, int inEnd, 70 char[] output, int outEnd, 71 int[] data, 72 boolean flush); 73 /** 74 * Converts an array of Unicode chars containing characters in an 75 * external encoding into an array of bytes. This method allows 76 * a buffer by buffer conversion of a data stream. The state of the 77 * conversion is saved between calls to convert. Among other things, 78 * this means multibyte input sequences can be split between calls. 79 * If a call to convert results in an Error, the conversion may be 80 * continued by calling convert again with suitably modified parameters. 81 * All conversions should be finished with a call to the flush method. 82 * 83 * @param converterHandle Address of converter object created by C code 84 * @param input char array containing text to be converted. 85 * @param inEnd stop conversion at this offset in input array (exclusive). 86 * @param output byte array to receive conversion result. 87 * @param outEnd stop writing to output array at this offset (exclusive). 88 * @param data integer array containing the following data 89 * data[0] = inputOffset 90 * data[1] = outputOffset 91 * @return int error code returned by ICU 92 * @internal ICU 2.4 93 */ 94 public static final native int convertCharToByte(long converterHandle, 95 char[] input, int inEnd, 96 byte[] output, int outEnd, 97 int[] data, 98 boolean flush); 99 /** 100 * Converts an array of Unicode chars containing characters in an 101 * external encoding into an array of bytes. This method allows 102 * a buffer by buffer conversion of a data stream. The state of the 103 * conversion is saved between calls to convert. Among other things, 104 * this means multibyte input sequences can be split between calls. 105 * If a call to convert results in an Error, the conversion may be 106 * continued by calling convert again with suitably modified parameters. 107 * All conversions should be finished with a call to the flush method. 108 * 109 * @param converterHandle Address of converter object created by C code 110 * @param input char array containing text to be converted. 111 * @param inEnd stop conversion at this offset in input array (exclusive). 112 * @param output byte array to receive conversion result. 113 * @param outEnd stop writing to output array at this offset (exclusive). 114 * @param data integer array containing the following data 115 * data[0] = inputOffset 116 * data[1] = outputOffset 117 * @return int error code returned by ICU 118 * @internal ICU 2.4 119 */ 120 public static final native int encode(long converterHandle, 121 char[] input, int inEnd, 122 byte[] output, int outEnd, 123 int[] data, 124 boolean flush); 125 /** 126 * Writes any remaining output to the output buffer and resets the 127 * converter to its initial state. 128 * 129 * @param converterHandle Address of converter object created by C code 130 * @param output byte array to receive flushed output. 131 * @param outEnd stop writing to output array at this offset (exclusive). 132 * @return int error code returned by ICU 133 * @param data integer array containing the following data 134 * data[0] = inputOffset 135 * data[1] = outputOffset 136 * @internal ICU 2.4 137 */ 138 public static final native int flushCharToByte(long converterHandle, 139 byte[] output, 140 int outEnd, 141 int[] data); 142 /** 143 * Writes any remaining output to the output buffer and resets the 144 * converter to its initial state. 145 * 146 * @param converterHandle Address of converter object created by the native code 147 * @param output char array to receive flushed output. 148 * @param outEnd stop writing to output array at this offset (exclusive). 149 * @return int error code returned by ICU 150 * @param data integer array containing the following data 151 * data[0] = inputOffset 152 * data[1] = outputOffset 153 * @internal ICU 2.4 154 */ 155 public static final native int flushByteToChar(long converterHandle, 156 char[] output, 157 int outEnd, 158 int[] data); 159 160 /** 161 * Open the converter with the specified encoding 162 * 163 * @param converterHandle long array for recieving the adress of converter object 164 * created by the native code 165 * @param encoding string representing encoding 166 * @return int error code returned by ICU 167 * @internal ICU 2.4 168 */ 169 public static final native long openConverter(String encoding); 170 /** 171 * Resets the ByteToChar (toUnicode) state of specified converter 172 * 173 * @param converterHandle Address of converter object created by the native code 174 * @internal ICU 2.4 175 */ 176 public static final native void resetByteToChar(long converterHandle); 177 178 /** 179 * Resets the CharToByte (fromUnicode) state of specified converter 180 * 181 * @param converterHandle Address of converter object created by the native code 182 * @internal ICU 2.4 183 */ 184 public static final native void resetCharToByte(long converterHandle); 185 186 /** 187 * Closes the specified converter and releases the resources 188 * 189 * @param converterHandle Address of converter object created by the native code 190 * @internal ICU 2.4 191 */ 192 public static final native void closeConverter(long converterHandle); 193 194 /** 195 * Sets the substitution Unicode chars of the specified converter used 196 * by encoder 197 * @param converterHandle Address of converter object created by the native code 198 * @param subChars array of chars to used for substitution 199 * @param length length of the array 200 * @return int error code returned by ICU 201 * @internal ICU 2.4 202 */ 203 public static final native int setSubstitutionChars( long converterHandle, 204 char[] subChars,int length); 205 /** 206 * Sets the substitution bytes of the specified converter used by decoder 207 * 208 * @param converterHandle Address of converter object created by the native code 209 * @param subChars array of bytes to used for substitution 210 * @param length length of the array 211 * @return int error code returned by ICU 212 * @internal ICU 2.4 213 */ 214 public static final native int setSubstitutionBytes( long converterHandle, 215 byte[] subChars,int length); 216 /** 217 * Sets the substitution mode of CharToByte(fromUnicode) for the specified converter 218 * 219 * @param converterHandle Address of converter object created by the native code 220 * @param mode to set the true/false 221 * @return int error code returned by ICU 222 * @internal ICU 2.4 223 */ 224 public static final native int setSubstitutionModeCharToByte(long converterHandle, 225 boolean mode); 226 /** 227 * Sets the substitution mode of CharToByte(fromUnicode) for the specified converter 228 * 229 * @param converterHandle Address of converter object created by the native code 230 * @param mode to set the true/false 231 * @return int error code returned by ICU 232 * @internal ICU 3.6 233 */ 234 public static final native int setSubstitutionModeByteToChar(long converterHandle, 235 boolean mode); 236 /** 237 * Gets the numnber of invalid bytes in the specified converter object 238 * for the last error that has occured 239 * 240 * @param converterHandle Address of converter object created by the native code 241 * @param length array of int to recieve length of the array 242 * @return int error code returned by ICU 243 * @internal ICU 2.4 244 */ 245 public static final native int countInvalidBytes(long converterHandle, int[] length); 246 247 /** 248 * Gets the numnber of invalid chars in the specified converter object 249 * for the last error that has occured 250 * 251 * @param converterHandle Address of converter object created by the native code 252 * @param length array of int to recieve length of the array 253 * @return int error code returned by ICU 254 * @internal ICU 2.4 255 */ 256 public static final native int countInvalidChars(long converterHandle, int[] length); 257 258 /** 259 * Gets the number of bytes needed for converting a char 260 * 261 * @param converterHandle Address of converter object created by the native code 262 * @return number of bytes needed 263 * @internal ICU 2.4 264 */ 265 public static final native int getMaxBytesPerChar(long converterHandle); 266 267 /** 268 * Gets the number of bytes needed for converting a char 269 * 270 * @param converterHandle Address of converter object created by the native code 271 * @return number of bytes needed 272 * @internal ICU 3.2 273 */ 274 public static final native int getMinBytesPerChar(long converterHandle); 275 276 /** 277 * Gets the average numnber of bytes needed for converting a char 278 * 279 * @param converterHandle Address of converter object created by the native code 280 * @return number of bytes needed 281 * @internal ICU 2.4 282 */ 283 public static final native float getAveBytesPerChar(long converterHandle); 284 285 /** 286 * Gets the number of chars needed for converting a byte 287 * 288 * @param converterHandle Address of converter object created by the native code 289 * @return number of bytes needed 290 * @internal ICU 2.4 291 */ 292 public static final native int getMaxCharsPerByte(long converterHandle); 293 294 /** 295 * Gets the average numnber of chars needed for converting a byte 296 * 297 * @param converterHandle Address of converter object created by the native code 298 * @return number of bytes needed 299 * @internal ICU 2.4 300 */ 301 public static final native float getAveCharsPerByte(long converterHandle); 302 303 //CSDL: added by Jack 304 /** 305 * Determines whether charset1 contains charset2. 306 */ 307 public static final native boolean contains(long converterHandle1, long converterHandle2); 308 309 public static final native byte[] getSubstitutionBytes(long converterHandle); 310 311 /** 312 * Ascertains if a given Unicode code unit can 313 * be converted to the target encoding 314 * @param converterHandle Address of converter object created by the native code 315 * @param codeUnit the character to be converted 316 * @return true if a character can be converted 317 * @internal ICU 2.4 318 * 319 */ 320 public static final native boolean canEncode(long converterHandle,int codeUnit); 321 322 /** 323 * Ascertains if a given a byte sequence can be converted to Unicode 324 * @param converterHandle Address of converter object created by the native code 325 * @param bytes the bytes to be converted 326 * @return true if a character can be converted 327 * @internal ICU 2.4 328 * 329 */ 330 public static final native boolean canDecode(long converterHandle,byte[] bytes); 331 332 /** 333 * Gets the canonical names of available converters 334 * @return Object[] names as an object array 335 * @internal ICU 2.4 336 */ 337 public static final native String[] getAvailable(); 338 339 /** 340 * Gets the number of aliases for a converter name 341 * @param enc encoding name 342 * @return number of aliases for the converter 343 * @internal ICU 2.4 344 */ 345 public static final native int countAliases(String enc); 346 347 /** 348 * Gets the aliases associated with the converter name 349 * @param enc converter name 350 * @return converter names as elements in an object array 351 * @internal ICU 2.4 352 */ 353 public static final native String[] getAliases(String enc); 354 355 /** 356 * Gets the canonical name of the converter 357 * @param enc converter name 358 * @return canonical name of the converter 359 * @internal ICU 2.4 360 */ 361 public static final native String getCanonicalName(String enc); 362 363 /** 364 * Gets the canonical name of the converter as defined by Java 365 * @param enc converter name 366 * @return canonical name of the converter 367 * @internal ICU 3.4 368 */ 369 public static final native String getICUCanonicalName(String enc); 370 371 /** 372 * Gets the canonical name of the converter as defined by Java 373 * @param icuCanonicalName converter name 374 * @return canonical name of the converter 375 * @internal ICU 3.4 376 */ 377 public static final native String getJavaCanonicalName(String icuCanonicalName); 378 379 /** 380 * Sets the callback to Unicode for ICU conveter. The default behaviour of ICU callback 381 * is to call the specified callback function for both illegal and unmapped sequences. 382 * @param converterHandle Adress of the converter object created by native code 383 * @param mode call back mode to set. This is either STOP_CALLBACK, SKIP_CALLBACK or SUBSTITUE_CALLBACK 384 * The converter performs the specified callback when an error occurs 385 * @param stopOnIllegal If true sets the alerts the converter callback to stop on an illegal sequence 386 * @return int error code returned by ICU 387 * @internal ICU 2.4 388 */ 389 public static final native int setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, char[] subChars, int length); 390 391 /** 392 * Sets the callback from Unicode for ICU conveter. The default behaviour of ICU callback 393 * is to call the specified callback function for both illegal and unmapped sequences. 394 * @param converterHandle Adress of the converter object created by native code 395 * @param mode call back mode to set. This is either STOP_CALLBACK, SKIP_CALLBACK or SUBSTITUE_CALLBACK 396 * The converter performs the specified callback when an error occurs 397 * @param stopOnIllegal If true sets the alerts the converter callback to stop on an illegal sequence 398 * @return int error code returned by ICU 399 * @internal ICU 2.4 400 */ 401 public static final native int setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes, int length); 402 403 /** 404 * Returns a thread safe clone of the converter 405 * @internal ICU 2.4 406 */ 407 public static final native long safeClone(long converterHandle); 408 409 /** @internal ICU 2.4 */ 410 public static final int STOP_CALLBACK = 0;//CodingErrorAction.REPORT 411 /** @internal ICU 2.4 */ 412 public static final int SKIP_CALLBACK = 1;//CodingErrorAction.IGNORE 413 /** @internal ICU 2.4 */ 414 public static final int SUBSTITUTE_CALLBACK = 2;//CodingErrorAction.REPLACE 415} 416