Search in sources :

Example 1 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project XobotOS by xamarin.

the class Charset method forName.

/**
     * Returns a {@code Charset} instance for the named charset.
     *
     * @param charsetName a charset name (either canonical or an alias)
     * @throws IllegalCharsetNameException
     *             if the specified charset name is illegal.
     * @throws UnsupportedCharsetException
     *             if the desired charset is not supported by this runtime.
     */
public static Charset forName(String charsetName) {
    // Is this charset in our cache?
    Charset cs;
    synchronized (CACHED_CHARSETS) {
        cs = CACHED_CHARSETS.get(charsetName);
        if (cs != null) {
            return cs;
        }
    }
    if (charsetName == null) {
        throw new IllegalCharsetNameException(null);
    }
    // Is this a built-in charset supported by ICU?
    checkCharsetName(charsetName);
    cs = NativeConverter.charsetForName(charsetName);
    if (cs != null) {
        return cacheCharset(charsetName, cs);
    }
    // Does a configured CharsetProvider have this charset?
    for (CharsetProvider charsetProvider : ServiceLoader.load(CharsetProvider.class)) {
        cs = charsetProvider.charsetForName(charsetName);
        if (cs != null) {
            return cacheCharset(charsetName, cs);
        }
    }
    throw new UnsupportedCharsetException(charsetName);
}
Also used : CharsetProvider(java.nio.charset.spi.CharsetProvider)

Example 2 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project robovm by robovm.

the class Charset method availableCharsets.

/**
     * Returns an immutable case-insensitive map from canonical names to {@code Charset} instances.
     * If multiple charsets have the same canonical name, it is unspecified which is returned in
     * the map. This method may be slow. If you know which charset you're looking for, use
     * {@link #forName}.
     */
public static SortedMap<String, Charset> availableCharsets() {
    // Start with a copy of the built-in charsets...
    TreeMap<String, Charset> charsets = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
    for (String charsetName : NativeConverter.getAvailableCharsetNames()) {
        // RoboVM note: Added try-catch to ignore charsets with bad names (e.g. "x-UTF-16,version=1")
        try {
            Charset charset = NativeConverter.charsetForName(charsetName);
            charsets.put(charset.name(), charset);
        } catch (IllegalCharsetNameException e) {
        }
    }
    // Add all charsets provided by all charset providers...
    for (CharsetProvider charsetProvider : ServiceLoader.load(CharsetProvider.class)) {
        Iterator<Charset> it = charsetProvider.charsets();
        while (it.hasNext()) {
            Charset cs = it.next();
            // A CharsetProvider can't override a built-in Charset.
            if (!charsets.containsKey(cs.name())) {
                charsets.put(cs.name(), cs);
            }
        }
    }
    return Collections.unmodifiableSortedMap(charsets);
}
Also used : CharsetProvider(java.nio.charset.spi.CharsetProvider) TreeMap(java.util.TreeMap)

Example 3 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project XobotOS by xamarin.

the class Charset method availableCharsets.

/**
     * Returns an immutable case-insensitive map from canonical names to {@code Charset} instances.
     * If multiple charsets have the same canonical name, it is unspecified which is returned in
     * the map. This method may be slow. If you know which charset you're looking for, use
     * {@link #forName}.
     * @return an immutable case-insensitive map from canonical names to {@code Charset} instances
     */
public static SortedMap<String, Charset> availableCharsets() {
    // Start with a copy of the built-in charsets...
    TreeMap<String, Charset> charsets = new TreeMap<String, Charset>(String.CASE_INSENSITIVE_ORDER);
    for (String charsetName : NativeConverter.getAvailableCharsetNames()) {
        Charset charset = NativeConverter.charsetForName(charsetName);
        charsets.put(charset.name(), charset);
    }
    // Add all charsets provided by all charset providers...
    for (CharsetProvider charsetProvider : ServiceLoader.load(CharsetProvider.class)) {
        Iterator<Charset> it = charsetProvider.charsets();
        while (it.hasNext()) {
            Charset cs = it.next();
            // A CharsetProvider can't override a built-in Charset.
            if (!charsets.containsKey(cs.name())) {
                charsets.put(cs.name(), cs);
            }
        }
    }
    return Collections.unmodifiableSortedMap(charsets);
}
Also used : CharsetProvider(java.nio.charset.spi.CharsetProvider) TreeMap(java.util.TreeMap)

Example 4 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project robovm by robovm.

the class Charset method forName.

/**
     * Returns a {@code Charset} instance for the named charset.
     *
     * @param charsetName a charset name (either canonical or an alias)
     * @throws IllegalCharsetNameException
     *             if the specified charset name is illegal.
     * @throws UnsupportedCharsetException
     *             if the desired charset is not supported by this runtime.
     */
public static Charset forName(String charsetName) {
    // Is this charset in our cache?
    Charset cs;
    synchronized (CACHED_CHARSETS) {
        cs = CACHED_CHARSETS.get(charsetName);
        if (cs != null) {
            return cs;
        }
    }
    if (charsetName == null) {
        throw new IllegalCharsetNameException(null);
    }
    // Is this a built-in charset supported by ICU?
    checkCharsetName(charsetName);
    cs = NativeConverter.charsetForName(charsetName);
    if (cs != null) {
        return cacheCharset(charsetName, cs);
    }
    // Does a configured CharsetProvider have this charset?
    for (CharsetProvider charsetProvider : ServiceLoader.load(CharsetProvider.class)) {
        cs = charsetProvider.charsetForName(charsetName);
        if (cs != null) {
            return cacheCharset(charsetName, cs);
        }
    }
    throw new UnsupportedCharsetException(charsetName);
}
Also used : CharsetProvider(java.nio.charset.spi.CharsetProvider)

Example 5 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project jdk8u_jdk by JetBrains.

the class Charset method providers.

// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown.  Should be invoked with full privileges.
//
private static Iterator<CharsetProvider> providers() {
    return new Iterator<CharsetProvider>() {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        ServiceLoader<CharsetProvider> sl = ServiceLoader.load(CharsetProvider.class, cl);

        Iterator<CharsetProvider> i = sl.iterator();

        CharsetProvider next = null;

        private boolean getNext() {
            while (next == null) {
                try {
                    if (!i.hasNext())
                        return false;
                    next = i.next();
                } catch (ServiceConfigurationError sce) {
                    if (sce.getCause() instanceof SecurityException) {
                        // Ignore security exceptions
                        continue;
                    }
                    throw sce;
                }
            }
            return true;
        }

        public boolean hasNext() {
            return getNext();
        }

        public CharsetProvider next() {
            if (!getNext())
                throw new NoSuchElementException();
            CharsetProvider n = next;
            next = null;
            return n;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ServiceLoader(java.util.ServiceLoader) CharsetProvider(java.nio.charset.spi.CharsetProvider) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

CharsetProvider (java.nio.charset.spi.CharsetProvider)7 Iterator (java.util.Iterator)2 NoSuchElementException (java.util.NoSuchElementException)2 ServiceConfigurationError (java.util.ServiceConfigurationError)2 ServiceLoader (java.util.ServiceLoader)2 TreeMap (java.util.TreeMap)2 Before (org.junit.Before)1