Search in sources :

Example 1 with ICUUncheckedIOException

use of android.icu.util.ICUUncheckedIOException in project j2objc by google.

the class CaseMapImpl method toUpper.

public static <A extends Appendable> A toUpper(int caseLocale, int options, CharSequence src, A dest, Edits edits) {
    try {
        if (edits != null) {
            edits.reset();
        }
        if (caseLocale == UCaseProps.LOC_GREEK) {
            return GreekUpper.toUpper(options, src, dest, edits);
        }
        StringContextIterator iter = new StringContextIterator(src);
        int c;
        while ((c = iter.nextCaseMapCP()) >= 0) {
            c = UCaseProps.INSTANCE.toFullUpper(c, iter, dest, caseLocale);
            appendResult(c, dest, iter.getCPLength(), options, edits);
        }
        return dest;
    } catch (IOException e) {
        throw new ICUUncheckedIOException(e);
    }
}
Also used : ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException)

Example 2 with ICUUncheckedIOException

use of android.icu.util.ICUUncheckedIOException in project j2objc by google.

the class ICUBinary method getData.

/**
 * Loads an ICU binary data file and returns it as a ByteBuffer.
 * The buffer contents is normally read-only, but its position etc. can be modified.
 *
 * @param loader Used for loader.getResourceAsStream() unless the data is found elsewhere.
 * @param resourceName Resource name for use with the loader.
 * @param itemPath Relative ICU data item path, for example "root.res" or "coll/ucadata.icu".
 * @return The data as a read-only ByteBuffer.
 * @throws MissingResourceException if required==true and the resource could not be found
 */
// public static ByteBuffer getRequiredData(ClassLoader loader, String resourceName,
// String itemPath) {
// return getData(loader, resourceName, itemPath, true);
// }
/**
 * Loads an ICU binary data file and returns it as a ByteBuffer.
 * The buffer contents is normally read-only, but its position etc. can be modified.
 *
 * @param loader Used for loader.getResourceAsStream() unless the data is found elsewhere.
 * @param resourceName Resource name for use with the loader.
 * @param itemPath Relative ICU data item path, for example "root.res" or "coll/ucadata.icu".
 * @param required If the resource cannot be found,
 *        this method returns null (!required) or throws an exception (required).
 * @return The data as a read-only ByteBuffer,
 *         or null if required==false and the resource could not be found.
 * @throws MissingResourceException if required==true and the resource could not be found
 */
private static ByteBuffer getData(ClassLoader loader, String resourceName, String itemPath, boolean required) {
    ByteBuffer bytes = getDataFromFile(itemPath);
    if (bytes != null) {
        return bytes;
    }
    if (loader == null) {
        loader = ClassLoaderUtil.getClassLoader(ICUData.class);
    }
    if (resourceName == null) {
        resourceName = ICUData.ICU_BASE_NAME + '/' + itemPath;
    }
    ByteBuffer buffer = null;
    try {
        // Closed by getByteBufferFromInputStreamAndCloseStream().
        @SuppressWarnings("resource") InputStream is = ICUData.getStream(loader, resourceName, required);
        if (is == null) {
            return null;
        }
        buffer = getByteBufferFromInputStreamAndCloseStream(is);
    } catch (IOException e) {
        throw new ICUUncheckedIOException(e);
    }
    return buffer;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ICUUncheckedIOException

use of android.icu.util.ICUUncheckedIOException in project j2objc by google.

the class Normalizer2 method getInstance.

/**
 * Returns a Normalizer2 instance which uses the specified data file
 * (an ICU data file if data=null, or else custom binary data)
 * and which composes or decomposes text according to the specified mode.
 * Returns an unmodifiable singleton instance.
 * <ul>
 * <li>Use data=null for data files that are part of ICU's own data.
 * <li>Use name="nfc" and COMPOSE/DECOMPOSE for Unicode standard NFC/NFD.
 * <li>Use name="nfkc" and COMPOSE/DECOMPOSE for Unicode standard NFKC/NFKD.
 * <li>Use name="nfkc_cf" and COMPOSE for Unicode standard NFKC_CF=NFKC_Casefold.
 * </ul>
 * If data!=null, then the binary data is read once and cached using the provided
 * name as the key.
 * If you know or expect the data to be cached already, you can use data!=null
 * for non-ICU data as well.
 * <p>Any {@link java.io.IOException} is wrapped into a {@link android.icu.util.ICUUncheckedIOException}.
 * @param data the binary, big-endian normalization (.nrm file) data, or null for ICU data
 * @param name "nfc" or "nfkc" or "nfkc_cf" or name of custom data file
 * @param mode normalization mode (compose or decompose etc.)
 * @return the requested Normalizer2, if successful
 */
public static Normalizer2 getInstance(InputStream data, String name, Mode mode) {
    // TODO: If callers really use this API, then we should add an overload that takes a ByteBuffer.
    ByteBuffer bytes = null;
    if (data != null) {
        try {
            bytes = ICUBinary.getByteBufferFromInputStreamAndCloseStream(data);
        } catch (IOException e) {
            throw new ICUUncheckedIOException(e);
        }
    }
    Norm2AllModes all2Modes = Norm2AllModes.getInstance(bytes, name);
    switch(mode) {
        case COMPOSE:
            return all2Modes.comp;
        case DECOMPOSE:
            return all2Modes.decomp;
        case FCD:
            return all2Modes.fcd;
        case COMPOSE_CONTIGUOUS:
            return all2Modes.fcc;
        // will not occur
        default:
            return null;
    }
}
Also used : Norm2AllModes(android.icu.impl.Norm2AllModes) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ICUUncheckedIOException

use of android.icu.util.ICUUncheckedIOException in project j2objc by google.

the class FilteredNormalizer2 method normalize.

// Internal: No argument checking, and appends to dest.
// Pass as input spanCondition the one that is likely to yield a non-zero
// span length at the start of src.
// For set=[:age=3.2:], since almost all common characters were in Unicode 3.2,
// UnicodeSet.SpanCondition.SIMPLE should be passed in for the start of src
// and UnicodeSet.SpanCondition.NOT_CONTAINED should be passed in if we continue after
// an in-filter prefix.
private Appendable normalize(CharSequence src, Appendable dest, UnicodeSet.SpanCondition spanCondition) {
    // Don't throw away destination buffer between iterations.
    StringBuilder tempDest = new StringBuilder();
    try {
        for (int prevSpanLimit = 0; prevSpanLimit < src.length(); ) {
            int spanLimit = set.span(src, prevSpanLimit, spanCondition);
            int spanLength = spanLimit - prevSpanLimit;
            if (spanCondition == UnicodeSet.SpanCondition.NOT_CONTAINED) {
                if (spanLength != 0) {
                    dest.append(src, prevSpanLimit, spanLimit);
                }
                spanCondition = UnicodeSet.SpanCondition.SIMPLE;
            } else {
                if (spanLength != 0) {
                    // Not norm2.normalizeSecondAndAppend() because we do not want
                    // to modify the non-filter part of dest.
                    dest.append(norm2.normalize(src.subSequence(prevSpanLimit, spanLimit), tempDest));
                }
                spanCondition = UnicodeSet.SpanCondition.NOT_CONTAINED;
            }
            prevSpanLimit = spanLimit;
        }
    } catch (IOException e) {
        throw new ICUUncheckedIOException(e);
    }
    return dest;
}
Also used : ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException)

Example 5 with ICUUncheckedIOException

use of android.icu.util.ICUUncheckedIOException in project j2objc by google.

the class UCaseProps method toFullLower.

/**
 * Get the full lowercase mapping for c.
 *
 * @param c Character to be mapped.
 * @param iter Character iterator, used for context-sensitive mappings.
 *             See ContextIterator for details.
 *             If iter==null then a context-independent result is returned.
 * @param out If the mapping result is a string, then it is appended to out.
 * @param caseLocale Case locale value from ucase_getCaseLocale().
 * @return Output code point or string length, see MAX_STRING_LENGTH.
 *
 * @see ContextIterator
 * @see #MAX_STRING_LENGTH
 * @hide draft / provisional / internal are hidden on Android
 */
public final int toFullLower(int c, ContextIterator iter, Appendable out, int caseLocale) {
    int result, props;
    result = c;
    props = trie.get(c);
    if (!propsHasException(props)) {
        if (getTypeFromProps(props) >= UPPER) {
            result = c + getDelta(props);
        }
    } else {
        int excOffset = getExceptionsOffset(props), excOffset2;
        int excWord = exceptions.charAt(excOffset++);
        int full;
        excOffset2 = excOffset;
        if ((excWord & EXC_CONDITIONAL_SPECIAL) != 0) {
            /*
                 * Test for conditional mappings first
                 *   (otherwise the unconditional default mappings are always taken),
                 * then test for characters that have unconditional mappings in SpecialCasing.txt,
                 * then get the UnicodeData.txt mappings.
                 */
            if (caseLocale == LOC_LITHUANIAN && /* base characters, find accents above */
            (((c == 0x49 || c == 0x4a || c == 0x12e) && isFollowedByMoreAbove(iter)) || /* precomposed with accent above, no need to find one */
            (c == 0xcc || c == 0xcd || c == 0x128))) {
                /*
                        # Lithuanian

                        # Lithuanian retains the dot in a lowercase i when followed by accents.

                        # Introduce an explicit dot above when lowercasing capital I's and J's
                        # whenever there are more accents above.
                        # (of the accents used in Lithuanian: grave, acute, tilde above, and ogonek)

                        0049; 0069 0307; 0049; 0049; lt More_Above; # LATIN CAPITAL LETTER I
                        004A; 006A 0307; 004A; 004A; lt More_Above; # LATIN CAPITAL LETTER J
                        012E; 012F 0307; 012E; 012E; lt More_Above; # LATIN CAPITAL LETTER I WITH OGONEK
                        00CC; 0069 0307 0300; 00CC; 00CC; lt; # LATIN CAPITAL LETTER I WITH GRAVE
                        00CD; 0069 0307 0301; 00CD; 00CD; lt; # LATIN CAPITAL LETTER I WITH ACUTE
                        0128; 0069 0307 0303; 0128; 0128; lt; # LATIN CAPITAL LETTER I WITH TILDE
                     */
                try {
                    switch(c) {
                        case 0x49:
                            /* LATIN CAPITAL LETTER I */
                            out.append(iDot);
                            return 2;
                        case 0x4a:
                            /* LATIN CAPITAL LETTER J */
                            out.append(jDot);
                            return 2;
                        case 0x12e:
                            /* LATIN CAPITAL LETTER I WITH OGONEK */
                            out.append(iOgonekDot);
                            return 2;
                        case 0xcc:
                            /* LATIN CAPITAL LETTER I WITH GRAVE */
                            out.append(iDotGrave);
                            return 3;
                        case 0xcd:
                            /* LATIN CAPITAL LETTER I WITH ACUTE */
                            out.append(iDotAcute);
                            return 3;
                        case 0x128:
                            /* LATIN CAPITAL LETTER I WITH TILDE */
                            out.append(iDotTilde);
                            return 3;
                        default:
                            return 0;
                    }
                } catch (IOException e) {
                    throw new ICUUncheckedIOException(e);
                }
            /* # Turkish and Azeri */
            } else if (caseLocale == LOC_TURKISH && c == 0x130) {
                /*
                        # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
                        # The following rules handle those cases.

                        0130; 0069; 0130; 0130; tr # LATIN CAPITAL LETTER I WITH DOT ABOVE
                        0130; 0069; 0130; 0130; az # LATIN CAPITAL LETTER I WITH DOT ABOVE
                     */
                return 0x69;
            } else if (caseLocale == LOC_TURKISH && c == 0x307 && isPrecededBy_I(iter)) {
                /*
                        # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
                        # This matches the behavior of the canonically equivalent I-dot_above

                        0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
                        0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
                     */
                return 0;
            /* remove the dot (continue without output) */
            } else if (caseLocale == LOC_TURKISH && c == 0x49 && !isFollowedByDotAbove(iter)) {
                /*
                        # When lowercasing, unless an I is before a dot_above, it turns into a dotless i.

                        0049; 0131; 0049; 0049; tr Not_Before_Dot; # LATIN CAPITAL LETTER I
                        0049; 0131; 0049; 0049; az Not_Before_Dot; # LATIN CAPITAL LETTER I
                     */
                return 0x131;
            } else if (c == 0x130) {
                /*
                        # Preserve canonical equivalence for I with dot. Turkic is handled below.

                        0130; 0069 0307; 0130; 0130; # LATIN CAPITAL LETTER I WITH DOT ABOVE
                     */
                try {
                    out.append(iDot);
                    return 2;
                } catch (IOException e) {
                    throw new ICUUncheckedIOException(e);
                }
            } else if (c == 0x3a3 && !isFollowedByCasedLetter(iter, 1) && isFollowedByCasedLetter(iter, -1)) /* -1=preceded */
            {
                /*
                        # Special case for final form of sigma

                        03A3; 03C2; 03A3; 03A3; Final_Sigma; # GREEK CAPITAL LETTER SIGMA
                     */
                return 0x3c2;
            /* greek small final sigma */
            } else {
            /* no known conditional special case mapping, use a normal mapping */
            }
        } else if (hasSlot(excWord, EXC_FULL_MAPPINGS)) {
            long value = getSlotValueAndOffset(excWord, EXC_FULL_MAPPINGS, excOffset);
            full = (int) value & FULL_LOWER;
            if (full != 0) {
                /* start of full case mapping strings */
                excOffset = (int) (value >> 32) + 1;
                try {
                    // append the lowercase mapping
                    out.append(exceptions, excOffset, excOffset + full);
                    /* return the string length */
                    return full;
                } catch (IOException e) {
                    throw new ICUUncheckedIOException(e);
                }
            }
        }
        if (hasSlot(excWord, EXC_LOWER)) {
            result = getSlotValue(excWord, EXC_LOWER, excOffset2);
        }
    }
    return (result == c) ? ~result : result;
}
Also used : ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException)

Aggregations

ICUUncheckedIOException (android.icu.util.ICUUncheckedIOException)13 IOException (java.io.IOException)13 ByteBuffer (java.nio.ByteBuffer)3 MissingResourceException (java.util.MissingResourceException)2 ICUResourceBundle (android.icu.impl.ICUResourceBundle)1 Norm2AllModes (android.icu.impl.Norm2AllModes)1 ULocale (android.icu.util.ULocale)1 UResourceBundle (android.icu.util.UResourceBundle)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1