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