1 /**
2 *******************************************************************************
3 * Copyright (C) 1996-2006, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 *
7 *******************************************************************************
8 */
9 
10 package libcore.icu;
11 
12 import libcore.util.NativeAllocationRegistry;
13 
14 import java.nio.charset.Charset;
15 import java.nio.charset.CharsetDecoder;
16 import java.nio.charset.CharsetEncoder;
17 import java.nio.charset.CodingErrorAction;
18 
19 public final class NativeConverter {
20     private static final NativeAllocationRegistry registry = new NativeAllocationRegistry(
21             NativeConverter.class.getClassLoader(), getNativeFinalizer(), getNativeSize());
22 
decode(long converterHandle, byte[] input, int inEnd, char[] output, int outEnd, int[] data, boolean flush)23     public static native int decode(long converterHandle, byte[] input, int inEnd,
24             char[] output, int outEnd, int[] data, boolean flush);
25 
encode(long converterHandle, char[] input, int inEnd, byte[] output, int outEnd, int[] data, boolean flush)26     public static native int encode(long converterHandle, char[] input, int inEnd,
27             byte[] output, int outEnd, int[] data, boolean flush);
28 
openConverter(String charsetName)29     public static native long openConverter(String charsetName);
closeConverter(long converterHandle)30     public static native void closeConverter(long converterHandle);
31 
registerConverter(Object referrent, long converterHandle)32     public static void registerConverter(Object referrent, long converterHandle) {
33         registry.registerNativeAllocation(referrent, converterHandle);
34     }
35 
resetByteToChar(long converterHandle)36     public static native void resetByteToChar(long converterHandle);
resetCharToByte(long converterHandle)37     public static native void resetCharToByte(long converterHandle);
38 
getSubstitutionBytes(long converterHandle)39     public static native byte[] getSubstitutionBytes(long converterHandle);
40 
getMaxBytesPerChar(long converterHandle)41     public static native int getMaxBytesPerChar(long converterHandle);
getMinBytesPerChar(long converterHandle)42     public static native int getMinBytesPerChar(long converterHandle);
getAveBytesPerChar(long converterHandle)43     public static native float getAveBytesPerChar(long converterHandle);
getAveCharsPerByte(long converterHandle)44     public static native float getAveCharsPerByte(long converterHandle);
45 
contains(String converterName1, String converterName2)46     public static native boolean contains(String converterName1, String converterName2);
47 
getAvailableCharsetNames()48     public static native String[] getAvailableCharsetNames();
charsetForName(String charsetName)49     public static native Charset charsetForName(String charsetName);
50 
51     // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp".
translateCodingErrorAction(CodingErrorAction action)52     private static int translateCodingErrorAction(CodingErrorAction action) {
53         if (action == CodingErrorAction.REPORT) {
54             return 0;
55         } else if (action == CodingErrorAction.IGNORE) {
56             return 1;
57         } else if (action == CodingErrorAction.REPLACE) {
58             return 2;
59         } else {
60             throw new AssertionError(); // Someone changed the enum.
61         }
62     }
63 
setCallbackDecode(long converterHandle, CharsetDecoder decoder)64     public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) {
65         setCallbackDecode(converterHandle,
66                           translateCodingErrorAction(decoder.malformedInputAction()),
67                           translateCodingErrorAction(decoder.unmappableCharacterAction()),
68                           decoder.replacement());
69     }
setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars)70     private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars);
71 
setCallbackEncode(long converterHandle, CharsetEncoder encoder)72     public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
73         setCallbackEncode(converterHandle,
74                           translateCodingErrorAction(encoder.malformedInputAction()),
75                           translateCodingErrorAction(encoder.unmappableCharacterAction()),
76                           encoder.replacement());
77     }
setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes)78     private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
79 
getNativeFinalizer()80     public static native long getNativeFinalizer();
getNativeSize()81     public static native long getNativeSize();
82 }
83