FileDocCategorySizeDatePackage
SelectorProvider.javaAPI DocAndroid 1.5 API9905Wed May 06 22:41:04 BST 2009java.nio.channels.spi

SelectorProvider

public abstract class SelectorProvider extends Object
{@code SelectorProvider} is an abstract base class that declares methods for providing instances of {@link DatagramChannel}, {@link Pipe}, {@link java.nio.channels.Selector} , {@link ServerSocketChannel}, and {@link SocketChannel}. All the methods of this class are thread-safe.

A provider instance can be retrieved through a system property or the configuration file in a jar file; if no provide is available that way then the system default provider is returned.

since
Android 1.0

Fields Summary
private static final String
SYMBOL_COMMENT
private static final String
PROVIDER_IN_SYSTEM_PROPERTY
private static final String
PROVIDER_IN_JAR_RESOURCE
private static SelectorProvider
provider
private static Channel
inheritedChannel
Constructors Summary
protected SelectorProvider()
Constructs a new {@code SelectorProvider}.

throws
SecurityException if there is a security manager installed that does not permit the runtime permission labeled "selectorProvider".
since
Android 1.0

 

                                                            
      
        super();
        if (null != System.getSecurityManager()) {
            System.getSecurityManager().checkPermission(
                    new RuntimePermission("selectorProvider")); //$NON-NLS-1$
        }
    
Methods Summary
public java.nio.channels.ChannelinheritedChannel()
Returns the channel inherited from the instance that created this virtual machine.

return
the channel.
throws
IOException if an I/O error occurs.
throws
SecurityException if there is a security manager installed that does not permit the runtime permission labeled "selectorProvider".
since
Android 1.0

        // BEGIN android-added
        SecurityManager smngr = System.getSecurityManager();
        if (smngr != null) {
            smngr.checkPermission(
                    new RuntimePermission("inheritedChannel")); //$NON-NLS-1$
        }
        // END android-added
        if (null == inheritedChannel) {
            inheritedChannel = Platform.getNetworkSystem().inheritedChannel();
        }
        return inheritedChannel;
    
static java.nio.channels.spi.SelectorProviderloadProviderByJar()

        Enumeration<URL> enumeration = null;

        ClassLoader classLoader = AccessController.doPrivileged(
                new PrivilegedAction<ClassLoader>() {
                    public ClassLoader run() {
                        return ClassLoader.getSystemClassLoader();
                    }
                });
        try {
            enumeration = classLoader.getResources(PROVIDER_IN_JAR_RESOURCE);
        } catch (IOException e) {
            throw new Error(e);
        }
        if (null == enumeration) {
            return null;
        }
        // for every jar, read until we find the provider name.
        while (enumeration.hasMoreElements()) {
            BufferedReader br = null;
            String className = null;
            try {
                // BEGIN android-modified
                br = new BufferedReader(
                        new InputStreamReader(
                                (enumeration.nextElement()).openStream()),
                        8192);
                // END android-modified
            } catch (Exception e) {
                continue;
            }
            try {
                // only the first class is loaded ,as spec says, not the same as
                // we do before.
                while ((className = br.readLine()) != null) {
                    className = className.trim();
                    int siteComment = className.indexOf(SYMBOL_COMMENT);
                    className = (-1 == siteComment) ? className : className
                            .substring(0, siteComment);
                    if (0 < className.length()) {
                        return (SelectorProvider) classLoader.loadClass(
                                className).newInstance();                  
                    }
                }
            } catch (Exception e) {
                throw new Error(e);
            // BEGIN android-added
            // copied from a newer version of harmony
            } finally {
                try {
                    br.close();
                } catch (IOException ioe) {
                    // Ignore
                }
            // END android-added
            }
        }
        return null;
    
static java.nio.channels.spi.SelectorProviderloadProviderByProperty()

        return AccessController.doPrivileged(
                new PrivilegedAction<SelectorProvider>() {
                    public SelectorProvider run() {
                        try {
                            final String className =
                                System.getProperty(PROVIDER_IN_SYSTEM_PROPERTY);
                            if (null != className) {
                                Class<?> spClass = ClassLoader
                                        .getSystemClassLoader().loadClass(
                                                className);
                                return (SelectorProvider)spClass.newInstance();
                            }
                            return null;
                        } catch (Exception e) {
                            throw new Error(e);
                        }
                    }
                });
    
public abstract java.nio.channels.DatagramChannelopenDatagramChannel()
Creates a new open {@code DatagramChannel}.

return
the new channel.
throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract java.nio.channels.PipeopenPipe()
Creates a new {@code Pipe}.

return
the new pipe.
throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract java.nio.channels.spi.AbstractSelectoropenSelector()
Creates a new selector.

return
the new selector.
throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract java.nio.channels.ServerSocketChannelopenServerSocketChannel()
Creates a new open {@code ServerSocketChannel}.

return
the new channel.
throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract java.nio.channels.SocketChannelopenSocketChannel()
Create a new open {@code SocketChannel}.

return
the new channel.
throws
IOException if an I/O error occurs.
since
Android 1.0

public static synchronized java.nio.channels.spi.SelectorProviderprovider()
Gets a provider instance by executing the following steps when called for the first time:
  • if the system property "java.nio.channels.spi.SelectorProvider" is set, the value of this property is the class name of the provider returned;
  • if there is a provider-configuration file named "java.nio.channels.spi.SelectorProvider" in META-INF/services of a jar file valid in the system class loader, the first class name is the provider's class name;
  • otherwise, a system default provider will be returned.

return
the provider.
since
Android 1.0

        if (null == provider) {
            provider = loadProviderByProperty();
            if (null == provider) {
                provider = loadProviderByJar();
            }
            if (null == provider) {
                provider = AccessController
                        .doPrivileged(new PrivilegedAction<SelectorProvider>() {
                            public SelectorProvider run() {
                                return new SelectorProviderImpl();
                            }
                        });
            }
        }
        return provider;