NativeConverter.java revision ccb8b92211a3e87acaf6486c8d4423c2053b8b5e
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;
13bcf7c66e617ad0c33bb320184bb2401def517342Elliott Hughes
14ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughespublic final class NativeConverter {
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Converts an array of bytes containing characters in an external
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * encoding into an array of Unicode characters.  This  method allows
18ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * buffer-by-buffer conversion of a data stream.  The state of the
19ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * conversion is saved between calls.  Among other things,
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * this means multibyte input sequences can be split between calls.
21ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * If a call to results in an error, the conversion may be
22ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * continued by calling this method again with suitably modified parameters.
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * All conversions should be finished with a call to the flush method.
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param converterHandle Address of converter object created by C code
26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param input byte array containing text to be converted.
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param inEnd stop conversion at this offset in input array (exclusive).
28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param output character array to receive conversion result.
29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param outEnd stop writing to output array at this offset (exclusive).
30ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * @param data integer array containing the following data
31adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[0] = inputOffset
32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[1] = outputOffset
33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return int error code returned by ICU
34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @internal ICU 2.4
35adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
36ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int decode(long converterHandle, byte[] input, int inEnd,
37ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes            char[] output, int outEnd, int[] data, boolean flush);
38ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
39adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
40ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * Converts an array of Unicode chars to an array of bytes in an external encoding.
41ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * This  method allows a buffer by buffer conversion of a data stream.  The state of the
42adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * conversion is saved between calls to convert.  Among other things,
43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * this means multibyte input sequences can be split between calls.
44ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * If a call results in an error, the conversion may be
45ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * continued by calling this method again with suitably modified parameters.
46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * All conversions should be finished with a call to the flush method.
47adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param converterHandle Address of converter object created by C code
49adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param input char array containing text to be converted.
50adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param inEnd stop conversion at this offset in input array (exclusive).
51adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param output byte array to receive conversion result.
52adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param outEnd stop writing to output array at this offset (exclusive).
53ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * @param data integer array containing the following data
54adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[0] = inputOffset
55adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[1] = outputOffset
56adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return int error code returned by ICU
57adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @internal ICU 2.4
58ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     */
59ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int encode(long converterHandle, char[] input, int inEnd,
60ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes            byte[] output, int outEnd, int[] data, boolean flush);
61ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
62adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
63adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Writes any remaining output to the output buffer and resets the
64ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * converter to its initial state.
65adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param converterHandle Address of converter object created by C code
67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param output byte array to receive flushed output.
68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param outEnd stop writing to output array at this offset (exclusive).
69adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return int error code returned by ICU
70ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * @param data integer array containing the following data
71adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[0] = inputOffset
72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[1] = outputOffset
73adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @internal ICU 2.4
74ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     */
75ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int flushCharToByte(long converterHandle, byte[] output, int outEnd, int[] data);
76ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
77adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
78adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Writes any remaining output to the output buffer and resets the
79ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * converter to its initial state.
80adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
81adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param converterHandle Address of converter object created by the native code
82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param output char array to receive flushed output.
83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param outEnd stop writing to output array at this offset (exclusive).
84adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return int error code returned by ICU
85ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes     * @param data integer array containing the following data
86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[0] = inputOffset
87adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *        data[1] = outputOffset
88adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @internal ICU 2.4
89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
90ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int flushByteToChar(long converterHandle, char[] output,  int outEnd, int[] data);
91bcf7c66e617ad0c33bb320184bb2401def517342Elliott Hughes
92ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native long openConverter(String encoding);
93ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native void closeConverter(long converterHandle);
94ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
95ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native void resetByteToChar(long  converterHandle);
96ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native void resetCharToByte(long  converterHandle);
97ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
98ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int setSubstitutionChars(long converterHandle, char[] subChars,int length);
99ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int setSubstitutionBytes(long converterHandle, byte[] subChars,int length);
100ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native byte[] getSubstitutionBytes(long converterHandle);
101ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
102ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int getMaxBytesPerChar(long converterHandle);
103ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int getMinBytesPerChar(long converterHandle);
104ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native float getAveBytesPerChar(long converterHandle);
105ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int getMaxCharsPerByte(long converterHandle);
106ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native float getAveCharsPerByte(long converterHandle);
107ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
108ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native boolean contains(long converterHandle1, long converterHandle2);
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
115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public static final int STOP_CALLBACK = 0;//CodingErrorAction.REPORT
116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public static final int SKIP_CALLBACK = 1;//CodingErrorAction.IGNORE
117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public static final int SUBSTITUTE_CALLBACK = 2;//CodingErrorAction.REPLACE
118ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, char[] subChars, int length);
119ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes, int length);
120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
121