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
10644ccb2c3d69ee6f3a69996ca7651b84d409fe41Elliott Hughespackage libcore.icu;
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
121bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamathimport libcore.util.NativeAllocationRegistry;
131bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath
14bcf7c66e617ad0c33bb320184bb2401def517342Elliott Hughesimport java.nio.charset.Charset;
15cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughesimport java.nio.charset.CharsetDecoder;
16cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughesimport java.nio.charset.CharsetEncoder;
17cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughesimport java.nio.charset.CodingErrorAction;
18bcf7c66e617ad0c33bb320184bb2401def517342Elliott Hughes
19ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughespublic final class NativeConverter {
201bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath    private static final NativeAllocationRegistry registry = new NativeAllocationRegistry(
21111fdf10801861427f59e42c34c40d5df484053bRichard Uhler            NativeConverter.class.getClassLoader(), getNativeFinalizer(), getNativeSize());
221bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath
23ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int decode(long converterHandle, byte[] input, int inEnd,
24ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes            char[] output, int outEnd, int[] data, boolean flush);
25ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
26ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int encode(long converterHandle, char[] input, int inEnd,
27ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes            byte[] output, int outEnd, int[] data, boolean flush);
28ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
29be4a7d686edf15a7fbdd00b40cf78cb26d105d0eElliott Hughes    public static native long openConverter(String charsetName);
30ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native void closeConverter(long converterHandle);
31ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
321bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath    public static void registerConverter(Object referrent, long converterHandle) {
331bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath        registry.registerNativeAllocation(referrent, converterHandle);
341bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath    }
351bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath
36cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    public static native void resetByteToChar(long converterHandle);
37cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    public static native void resetCharToByte(long converterHandle);
38ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
39ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native byte[] getSubstitutionBytes(long converterHandle);
40ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
41ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int getMaxBytesPerChar(long converterHandle);
42ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native int getMinBytesPerChar(long converterHandle);
43ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native float getAveBytesPerChar(long converterHandle);
44ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native float getAveCharsPerByte(long converterHandle);
45ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
467d52d787302b862019da41aa753646d88d43fd61Elliott Hughes    public static native boolean contains(String converterName1, String converterName2);
47ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes
48ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native String[] getAvailableCharsetNames();
49ccb8b92211a3e87acaf6486c8d4423c2053b8b5eElliott Hughes    public static native Charset charsetForName(String charsetName);
50bcf7c66e617ad0c33bb320184bb2401def517342Elliott Hughes
51cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp".
52cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    private static int translateCodingErrorAction(CodingErrorAction action) {
53cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes        if (action == CodingErrorAction.REPORT) {
54cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes            return 0;
55cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes        } else if (action == CodingErrorAction.IGNORE) {
56cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes            return 1;
57cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes        } else if (action == CodingErrorAction.REPLACE) {
58cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes            return 2;
59cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes        } else {
60cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes            throw new AssertionError(); // Someone changed the enum.
61cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes        }
62cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    }
63cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes
645ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) {
655ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes        setCallbackDecode(converterHandle,
665ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          translateCodingErrorAction(decoder.malformedInputAction()),
675ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          translateCodingErrorAction(decoder.unmappableCharacterAction()),
685ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          decoder.replacement());
69cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    }
705ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars);
71cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes
725ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
735ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes        setCallbackEncode(converterHandle,
745ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          translateCodingErrorAction(encoder.malformedInputAction()),
755ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          translateCodingErrorAction(encoder.unmappableCharacterAction()),
765ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes                          encoder.replacement());
77cf6c3a752da274cc5025191d3bcd62e6222f4a4cElliott Hughes    }
785ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
791bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath
801bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath    public static native long getNativeFinalizer();
811bdc6bc1c72b033ed860360e69fc9f1f1121579bNarayan Kamath    public static native long getNativeSize();
82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
83