Home | History | Annotate | Download | only in icu
      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 
     23     public static native int decode(long converterHandle, byte[] input, int inEnd,
     24             char[] output, int outEnd, int[] data, boolean flush);
     25 
     26     public static native int encode(long converterHandle, char[] input, int inEnd,
     27             byte[] output, int outEnd, int[] data, boolean flush);
     28 
     29     public static native long openConverter(String charsetName);
     30     public static native void closeConverter(long converterHandle);
     31 
     32     public static void registerConverter(Object referrent, long converterHandle) {
     33         registry.registerNativeAllocation(referrent, converterHandle);
     34     }
     35 
     36     public static native void resetByteToChar(long converterHandle);
     37     public static native void resetCharToByte(long converterHandle);
     38 
     39     public static native byte[] getSubstitutionBytes(long converterHandle);
     40 
     41     public static native int getMaxBytesPerChar(long converterHandle);
     42     public static native int getMinBytesPerChar(long converterHandle);
     43     public static native float getAveBytesPerChar(long converterHandle);
     44     public static native float getAveCharsPerByte(long converterHandle);
     45 
     46     public static native boolean contains(String converterName1, String converterName2);
     47 
     48     public static native String[] getAvailableCharsetNames();
     49     public static native Charset charsetForName(String charsetName);
     50 
     51     // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp".
     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 
     64     public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) {
     65         setCallbackDecode(converterHandle,
     66                           translateCodingErrorAction(decoder.malformedInputAction()),
     67                           translateCodingErrorAction(decoder.unmappableCharacterAction()),
     68                           decoder.replacement());
     69     }
     70     private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars);
     71 
     72     public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
     73         setCallbackEncode(converterHandle,
     74                           translateCodingErrorAction(encoder.malformedInputAction()),
     75                           translateCodingErrorAction(encoder.unmappableCharacterAction()),
     76                           encoder.replacement());
     77     }
     78     private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
     79 
     80     public static native long getNativeFinalizer();
     81     public static native long getNativeSize();
     82 }
     83