summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/libcore/icu/NativeConverter.java
blob: 17be4587bdf10516c129639a906537d5246643a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
*******************************************************************************
* Copyright (C) 1996-2006, International Business Machines Corporation and    *
* others. All Rights Reserved.                                                *
*******************************************************************************
*
*******************************************************************************
*/

package libcore.icu;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;

public final class NativeConverter {
    public static native int decode(long converterHandle, byte[] input, int inEnd,
            char[] output, int outEnd, int[] data, boolean flush);

    public static native int encode(long converterHandle, char[] input, int inEnd,
            byte[] output, int outEnd, int[] data, boolean flush);

    public static native long openConverter(String charsetName);
    public static native void closeConverter(long converterHandle);

    public static native void resetByteToChar(long converterHandle);
    public static native void resetCharToByte(long converterHandle);

    public static native byte[] getSubstitutionBytes(long converterHandle);

    public static native int getMaxBytesPerChar(long converterHandle);
    public static native int getMinBytesPerChar(long converterHandle);
    public static native float getAveBytesPerChar(long converterHandle);
    public static native float getAveCharsPerByte(long converterHandle);

    public static native boolean contains(String converterName1, String converterName2);

    public static native String[] getAvailableCharsetNames();
    public static native Charset charsetForName(String charsetName);

    // Translates from Java's enum to the magic numbers #defined in "NativeConverter.cpp".
    private static int translateCodingErrorAction(CodingErrorAction action) {
        if (action == CodingErrorAction.REPORT) {
            return 0;
        } else if (action == CodingErrorAction.IGNORE) {
            return 1;
        } else if (action == CodingErrorAction.REPLACE) {
            return 2;
        } else {
            throw new AssertionError(); // Someone changed the enum.
        }
    }

    public static void setCallbackDecode(long converterHandle, CharsetDecoder decoder) {
        setCallbackDecode(converterHandle,
                          translateCodingErrorAction(decoder.malformedInputAction()),
                          translateCodingErrorAction(decoder.unmappableCharacterAction()),
                          decoder.replacement());
    }
    private static native void setCallbackDecode(long converterHandle, int onMalformedInput, int onUnmappableInput, String subChars);

    public static void setCallbackEncode(long converterHandle, CharsetEncoder encoder) {
        setCallbackEncode(converterHandle,
                          translateCodingErrorAction(encoder.malformedInputAction()),
                          translateCodingErrorAction(encoder.unmappableCharacterAction()),
                          encoder.replacement());
    }
    private static native void setCallbackEncode(long converterHandle, int onMalformedInput, int onUnmappableInput, byte[] subBytes);
}