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 { 18 public static native int decode(long converterHandle, byte[] input, int inEnd, 19 char[] output, int outEnd, int[] data, boolean flush); 20 21 public static native int encode(long converterHandle, char[] input, int inEnd, 22 byte[] output, int outEnd, int[] data, boolean flush); 23 24 public static native long openConverter(String charsetName); 25 public static native void closeConverter(long converterHandle); 26 27 public static native void resetByteToChar(long converterHandle); 28 public static native void resetCharToByte(long converterHandle); 29 30 public static native byte[] getSubstitutionBytes(long converterHandle); 31 32 public static native int getMaxBytesPerChar(long converterHandle); 33 public static native int getMinBytesPerChar(long converterHandle); 34 public static native float getAveBytesPerChar(long converterHandle); 35 public static native float getAveCharsPerByte(long converterHandle); 36 37 public static native boolean contains(String converterName1, String converterName2); 38 39 public static native String[] getAvailableCharsetNames(); 40 public static native Charset charsetForName(String charsetName); 41 42 // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp". 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 55 public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) { 56 setCallbackDecode(converterHandle, 57 translateCodingErrorAction(decoder.malformedInputAction()), 58 translateCodingErrorAction(decoder.unmappableCharacterAction()), 59 decoder.replacement()); 60 } 61 private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars); 62 63 public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) { 64 setCallbackEncode(converterHandle, 65 translateCodingErrorAction(encoder.malformedInputAction()), 66 translateCodingErrorAction(encoder.unmappableCharacterAction()), 67 encoder.replacement()); 68 } 69 private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes); 70 } 71