Search in sources :

Example 6 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project Bytecoder by mirkosertic.

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<>() {

        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)

Example 7 with CharsetProvider

use of java.nio.charset.spi.CharsetProvider in project opensmpp by OpenSmpp.

the class Gsm7BitCharsetTest method setup.

@Before
public void setup() {
    CharsetProvider provider = new Gsm7BitCharsetProvider();
    charset = provider.charsetForName("X-Gsm7Bit");
}
Also used : CharsetProvider(java.nio.charset.spi.CharsetProvider) Before(org.junit.Before)

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