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 java.nio.charset.Charset;
13 import java.nio.charset.CharsetDecoder;
14 import java.nio.charset.CharsetEncoder;
15 import java.nio.charset.CodingErrorAction;
16 
17 public final class NativeConverter {
decode(long converterHandle, byte[] input, int inEnd, char[] output, int outEnd, int[] data, boolean flush)18     public static native int decode(long converterHandle, byte[] input, int inEnd,
19             char[] output, int outEnd, int[] data, boolean flush);
20 
encode(long converterHandle, char[] input, int inEnd, byte[] output, int outEnd, int[] data, boolean flush)21     public static native int encode(long converterHandle, char[] input, int inEnd,
22             byte[] output, int outEnd, int[] data, boolean flush);
23 
openConverter(String charsetName)24     public static native long openConverter(String charsetName);
closeConverter(long converterHandle)25     public static native void closeConverter(long converterHandle);
26 
resetByteToChar(long converterHandle)27     public static native void resetByteToChar(long converterHandle);
resetCharToByte(long converterHandle)28     public static native void resetCharToByte(long converterHandle);
29 
getSubstitutionBytes(long converterHandle)30     public static native byte[] getSubstitutionBytes(long converterHandle);
31 
getMaxBytesPerChar(long converterHandle)32     public static native int getMaxBytesPerChar(long converterHandle);
getMinBytesPerChar(long converterHandle)33     public static native int getMinBytesPerChar(long converterHandle);
getAveBytesPerChar(long converterHandle)34     public static native float getAveBytesPerChar(long converterHandle);
getAveCharsPerByte(long converterHandle)35     public static native float getAveCharsPerByte(long converterHandle);
36 
contains(String converterName1, String converterName2)37     public static native boolean contains(String converterName1, String converterName2);
38 
getAvailableCharsetNames()39     public static native String[] getAvailableCharsetNames();
charsetForName(String charsetName)40     public static native Charset charsetForName(String charsetName);
41 
42     // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp".
translateCodingErrorAction(CodingErrorAction action)43     private static int translateCodingErrorAction(CodingErrorAction action) {
44         if (action == CodingErrorAction.REPORT) {
45             return 0;
46         } else if (action == CodingErrorAction.IGNORE) {
47             return 1;
48         } else if (action == CodingErrorAction.REPLACE) {
49             return 2;
50         } else {
51             throw new AssertionError(); // Someone changed the enum.
52         }
53     }
54 
setCallbackDecode(long converterHandle, CharsetDecoder decoder)55     public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) {
56         setCallbackDecode(converterHandle,
57                           translateCodingErrorAction(decoder.malformedInputAction()),
58                           translateCodingErrorAction(decoder.unmappableCharacterAction()),
59                           decoder.replacement());
60     }
setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars)61     private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars);
62 
setCallbackEncode(long converterHandle, CharsetEncoder encoder)63     public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
64         setCallbackEncode(converterHandle,
65                           translateCodingErrorAction(encoder.malformedInputAction()),
66                           translateCodingErrorAction(encoder.unmappableCharacterAction()),
67                           encoder.replacement());
68     }
setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes)69     private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
70 }
71