NativeConverter.java revision 644ccb2c3d69ee6f3a69996ca7651b84d409fe41
1/** 2******************************************************************************* 3* Copyright (C) 1996-2006, International Business Machines Corporation and * 4* others. All Rights Reserved. * 5******************************************************************************* 6* 7******************************************************************************* 8*/ 9 10package libcore.icu; 11 12import java.nio.charset.Charset; 13import java.nio.charset.CharsetDecoder; 14import java.nio.charset.CharsetEncoder; 15import java.nio.charset.CodingErrorAction; 16 17public final class NativeConverter { 18 /** 19 * Converts an array of bytes containing characters in an external 20 * encoding into an array of Unicode characters. This method allows 21 * buffer-by-buffer conversion of a data stream. The state of the 22 * conversion is saved between calls. Among other things, 23 * this means multibyte input sequences can be split between calls. 24 * If a call to results in an error, the conversion may be 25 * continued by calling this method again with suitably modified parameters. 26 * All conversions should be finished with a call to the flush method. 27 * 28 * @param converterHandle Address of converter object created by C code 29 * @param input byte array containing text to be converted. 30 * @param inEnd stop conversion at this offset in input array (exclusive). 31 * @param output character array to receive conversion result. 32 * @param outEnd stop writing to output array at this offset (exclusive). 33 * @param data integer array containing the following data 34 * data[0] = inputOffset 35 * data[1] = outputOffset 36 * @return int error code returned by ICU 37 * @internal ICU 2.4 38 */ 39 public static native int decode(long converterHandle, byte[] input, int inEnd, 40 char[] output, int outEnd, int[] data, boolean flush); 41 42 /** 43 * Converts an array of Unicode chars to an array of bytes in an external encoding. 44 * This method allows a buffer by buffer conversion of a data stream. The state of the 45 * conversion is saved between calls to convert. Among other things, 46 * this means multibyte input sequences can be split between calls. 47 * If a call results in an error, the conversion may be 48 * continued by calling this method again with suitably modified parameters. 49 * All conversions should be finished with a call to the flush method. 50 * 51 * @param converterHandle Address of converter object created by C code 52 * @param input char array containing text to be converted. 53 * @param inEnd stop conversion at this offset in input array (exclusive). 54 * @param output byte array to receive conversion result. 55 * @param outEnd stop writing to output array at this offset (exclusive). 56 * @param data integer array containing the following data 57 * data[0] = inputOffset 58 * data[1] = outputOffset 59 * @return int error code returned by ICU 60 * @internal ICU 2.4 61 */ 62 public static native int encode(long converterHandle, char[] input, int inEnd, 63 byte[] output, int outEnd, int[] data, boolean flush); 64 65 /** 66 * Writes any remaining output to the output buffer and resets the 67 * converter to its initial state. 68 * 69 * @param converterHandle Address of converter object created by C code 70 * @param output byte array to receive flushed output. 71 * @param outEnd stop writing to output array at this offset (exclusive). 72 * @return int error code returned by ICU 73 * @param data integer array containing the following data 74 * data[0] = inputOffset 75 * data[1] = outputOffset 76 * @internal ICU 2.4 77 */ 78 public static native int flushCharToByte(long converterHandle, byte[] output, int outEnd, int[] data); 79 80 /** 81 * Writes any remaining output to the output buffer and resets the 82 * converter to its initial state. 83 * 84 * @param converterHandle Address of converter object created by the native code 85 * @param output char array to receive flushed output. 86 * @param outEnd stop writing to output array at this offset (exclusive). 87 * @return int error code returned by ICU 88 * @param data integer array containing the following data 89 * data[0] = inputOffset 90 * data[1] = outputOffset 91 * @internal ICU 2.4 92 */ 93 public static native int flushByteToChar(long converterHandle, char[] output, int outEnd, int[] data); 94 95 public static native long openConverter(String encoding); 96 public static native void closeConverter(long converterHandle); 97 98 public static native void resetByteToChar(long converterHandle); 99 public static native void resetCharToByte(long converterHandle); 100 101 public static native byte[] getSubstitutionBytes(long converterHandle); 102 103 public static native int getMaxBytesPerChar(long converterHandle); 104 public static native int getMinBytesPerChar(long converterHandle); 105 public static native float getAveBytesPerChar(long converterHandle); 106 public static native float getAveCharsPerByte(long converterHandle); 107 108 public static native boolean contains(String converterName1, String converterName2); 109 110 public static native boolean canEncode(long converterHandle, int codeUnit); 111 112 public static native String[] getAvailableCharsetNames(); 113 public static native Charset charsetForName(String charsetName); 114 115 // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp". 116 private static int translateCodingErrorAction(CodingErrorAction action) { 117 if (action == CodingErrorAction.REPORT) { 118 return 0; 119 } else if (action == CodingErrorAction.IGNORE) { 120 return 1; 121 } else if (action == CodingErrorAction.REPLACE) { 122 return 2; 123 } else { 124 throw new AssertionError(); // Someone changed the enum. 125 } 126 } 127 128 public static int setCallbackDecode(long converterHandle, CharsetDecoder decoder) { 129 return setCallbackDecode(converterHandle, 130 translateCodingErrorAction(decoder.malformedInputAction()), 131 translateCodingErrorAction(decoder.unmappableCharacterAction()), 132 decoder.replacement().toCharArray()); 133 } 134 private static native int setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, char[] subChars); 135 136 public static int setCallbackEncode(long converterHandle, CharsetEncoder encoder) { 137 return setCallbackEncode(converterHandle, 138 translateCodingErrorAction(encoder.malformedInputAction()), 139 translateCodingErrorAction(encoder.unmappableCharacterAction()), 140 encoder.replacement()); 141 } 142 private static native int setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes); 143} 144