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 encoding); 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 boolean canEncode(long converterHandle, int codeUnit); 40 41 public static native String[] getAvailableCharsetNames(); 42 public static native Charset charsetForName(String charsetName); 43 44 // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp". 45 private static int translateCodingErrorAction(CodingErrorAction action) { 46 if (action == CodingErrorAction.REPORT) { 47 return 0; 48 } else if (action == CodingErrorAction.IGNORE) { 49 return 1; 50 } else if (action == CodingErrorAction.REPLACE) { 51 return 2; 52 } else { 53 throw new AssertionError(); // Someone changed the enum. 54 } 55 } 56 57 public static int setCallbackDecode(long converterHandle, CharsetDecoder decoder) { 58 return setCallbackDecode(converterHandle, 59 translateCodingErrorAction(decoder.malformedInputAction()), 60 translateCodingErrorAction(decoder.unmappableCharacterAction()), 61 decoder.replacement()); 62 } 63 private static native int setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars); 64 65 public static int setCallbackEncode(long converterHandle, CharsetEncoder encoder) { 66 return setCallbackEncode(converterHandle, 67 translateCodingErrorAction(encoder.malformedInputAction()), 68 translateCodingErrorAction(encoder.unmappableCharacterAction()), 69 encoder.replacement()); 70 } 71 private static native int setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes); 72 } 73