src/java.base/share/classes/java/nio/channels/SocketChannel.java
branchunixdomainchannels
changeset 58981 5c79956cc7d7
parent 58847 692de65ab293
child 59078 4e648a2d8480
equal deleted inserted replaced
58939:aca099f4b3ec 58981:5c79956cc7d7
    65  * thread while another thread is blocked in a write operation on the socket's
    65  * thread while another thread is blocked in a write operation on the socket's
    66  * channel, then the blocked thread will receive an {@link
    66  * channel, then the blocked thread will receive an {@link
    67  * AsynchronousCloseException}.
    67  * AsynchronousCloseException}.
    68  *
    68  *
    69  * <p>Two kinds of socket channel are supported: <i>IP</i> (Internet Protocol) and
    69  * <p>Two kinds of socket channel are supported: <i>IP</i> (Internet Protocol) and
    70  * <i>unix domain</i> depending on which open method is used to create them and which
    70  * <i>Unix Domain</i> depending on which open method is used to create them and which
    71  * subtype of {@link SocketAddress} that they support for local and remote addresses.
    71  * subtype of {@link SocketAddress} that they support for local and remote addresses.
    72  * <i>IP</i> channels are created using {@link #open()}. They use {@link
    72  * <i>IP</i> channels are created using {@link #open()}. They use {@link
    73  * InetSocketAddress} addresses and support both IPv4 and IPv6 TCP/IP.
    73  * InetSocketAddress} addresses and support both IPv4 and IPv6 TCP/IP.
    74  * <i>unix domain</i> channels are created using {@link #open(ProtocolFamily)}
    74  * <i>Unix Domain</i> channels are created using {@link #open(ProtocolFamily)}
    75  * with the family parameter set to {@link StandardProtocolFamily#UNIX}.
    75  * with the family parameter set to {@link StandardProtocolFamily#UNIX}.
    76  * They use {@link UnixDomainSocketAddress}es and also
    76  * They use {@link UnixDomainSocketAddress}es and also
    77  * do not support the {@link #socket()} method.
    77  * do not support the {@link #socket()} method. Note, it is also possible to
       
    78  * create channels that support either IPv4 or IPv6 only through the
       
    79  * {@link #open(ProtocolFamily)} method.
    78  *
    80  *
    79  * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
    81  * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
    80  * setOption} method. <i>IP</i> socket channels support the following options:
    82  * setOption} method. <i>IP</i> socket channels support the following options:
    81  * <blockquote>
    83  * <blockquote>
    82  * <table class="striped">
    84  * <table class="striped">
   124  * #connect connect} and {@link #finishConnect finishConnect} methods are
   126  * #connect connect} and {@link #finishConnect finishConnect} methods are
   125  * mutually synchronized against each other, and an attempt to initiate a read
   127  * mutually synchronized against each other, and an attempt to initiate a read
   126  * or write operation while an invocation of one of these methods is in
   128  * or write operation while an invocation of one of these methods is in
   127  * progress will block until that invocation is complete.  </p>
   129  * progress will block until that invocation is complete.  </p>
   128  *
   130  *
   129  * <p><i>Unix domain</i> channels support a subset of the options listed above.
   131  * <p><i>Unix Domain</i> channels support a subset of the options listed above.
   130  *
   132  *
   131  * @author Mark Reinhold
   133  * @author Mark Reinhold
   132  * @author JSR-51 Expert Group
   134  * @author JSR-51 Expert Group
   133  * @since 1.4
   135  * @since 1.4
   134  */
   136  */
   181     }
   183     }
   182 
   184 
   183     /**
   185     /**
   184      * Opens a socket channel and connects it to a remote address.
   186      * Opens a socket channel and connects it to a remote address.
   185      * Depending on the type of {@link SocketAddress} supplied, the returned object
   187      * Depending on the type of {@link SocketAddress} supplied, the returned object
   186      * is an <i>IP</i> or <i>unix domain</i> channel.
   188      * is an <i>IP</i> or <i>Unix Domain</i> channel.
   187      *
   189      *
   188      * <p> For {@link InetSocketAddress}es this convenience method works as if by invoking the {@link #open()}
   190      * <p> For {@link InetSocketAddress}es this convenience method works as if by invoking the {@link #open()}
   189      * method, invoking the {@link #connect(SocketAddress) connect} method upon
   191      * method, invoking the {@link #connect(SocketAddress) connect} method upon
   190      * the resulting socket channel, passing it {@code remote}, and then
   192      * the resulting socket channel, passing it {@code remote}, which must be an
   191      * returning that channel.  </p>
   193      * {@link InetSocketAddress} and then returning that channel.  </p>
   192      *
   194      *
   193      * <p> For {@link UnixDomainSocketAddress}es it works as if by invoking the {@link
   195      * <p> For {@link UnixDomainSocketAddress}es it works as if by invoking the {@link
   194      * #open(ProtocolFamily)} method with {@link StandardProtocolFamily#UNIX} as parameter,
   196      * #open(ProtocolFamily)} method with {@link StandardProtocolFamily#UNIX} as parameter,
   195      * invoking the {@link #connect(SocketAddress) connect} method upon
   197      * invoking the {@link #connect(SocketAddress) connect} method upon
   196      * the resulting socket channel, passing it {@code remote}, and then
   198      * the resulting socket channel, passing it {@code remote}, which must be a
   197      * returning that channel.  </p>
   199      * {@link UnixDomainSocketAddress} and then returning that channel.  </p>
   198      *
   200      *
   199      * @param  remote
   201      * @param  remote
   200      *         The remote address to which the new channel is to be connected
   202      *         The remote address to which the new channel is to be connected
   201      *
   203      *
   202      * @return  A new, and connected, socket channel
   204      * @return  A new, and connected, socket channel
   270     // -- Socket-specific operations --
   272     // -- Socket-specific operations --
   271 
   273 
   272     /**
   274     /**
   273      * {@inheritDoc}
   275      * {@inheritDoc}
   274      *
   276      *
   275      * <p> Note, for <i>Unix domain</i> channels, a file is created in the file-system
   277      * <p> Note, for <i>Unix Domain</i> channels, a file is created in the file-system
   276      * with the same name as this channel's bound address. This file persists after
   278      * with the same name as this channel's bound address. This file persists after
   277      * the channel is closed, and must be removed before another channel can bind
   279      * the channel is closed, and must be removed before another channel can bind
   278      * to the same name.
   280      * to the same name.
   279      *
   281      *
   280      * @throws  ConnectionPendingException
   282      * @throws  ConnectionPendingException
   285      * @throws  ClosedChannelException              {@inheritDoc}
   287      * @throws  ClosedChannelException              {@inheritDoc}
   286      * @throws  IOException                         {@inheritDoc}
   288      * @throws  IOException                         {@inheritDoc}
   287      * @throws  SecurityException
   289      * @throws  SecurityException
   288      *          If a security manager has been installed and its
   290      *          If a security manager has been installed and its
   289      *          {@link SecurityManager#checkListen checkListen} method denies
   291      *          {@link SecurityManager#checkListen checkListen} method denies
   290      *          the operation for <i>IP</i> channels or for <i>unix domain</i>
   292      *          the operation for <i>IP</i> channels; or for <i>Unix Domain</i>
   291      *          channels, if the security manager denies "read" or "write"
   293      *          channels, if the security manager denies "read" or "write"
   292      *          {@link java.io.FilePermission} for the local path.
   294      *          {@link java.io.FilePermission} for the local path.
   293      *
   295      *
   294      * @since 1.7
   296      * @since 1.7
   295      */
   297      */
   350      */
   352      */
   351     public abstract SocketChannel shutdownOutput() throws IOException;
   353     public abstract SocketChannel shutdownOutput() throws IOException;
   352 
   354 
   353     /**
   355     /**
   354      * Retrieves a socket associated with this channel if it is an <i>IP</i>
   356      * Retrieves a socket associated with this channel if it is an <i>IP</i>
   355      * channel. The operation is not supported for <i>unix domain</i> channels.
   357      * channel. The operation is not supported for <i>Unix Domain</i> channels.
   356      *
   358      *
   357      * <p> The returned object will not declare any public methods that are not
   359      * <p> The returned object will not declare any public methods that are not
   358      * declared in the {@link java.net.Socket} class.  </p>
   360      * declared in the {@link java.net.Socket} class.  </p>
   359      *
   361      *
   360      * @return  A socket associated with this channel
   362      * @return  A socket associated with this channel
   361      * @throws UnsupportedOperationException is this is a Unix domain channel
   363      *
       
   364      * @throws UnsupportedOperationException is this is a <i>Unix Domain</i> channel
   362      */
   365      */
   363     public abstract Socket socket();
   366     public abstract Socket socket();
   364 
   367 
   365     /**
   368     /**
   366      * Tells whether or not this channel's network socket is connected.
   369      * Tells whether or not this channel's network socket is connected.
   398      * as the {@link java.net.Socket} class.  That is, if a security manager has been
   401      * as the {@link java.net.Socket} class.  That is, if a security manager has been
   399      * installed then this method verifies that its {@link
   402      * installed then this method verifies that its {@link
   400      * java.lang.SecurityManager#checkConnect checkConnect} method permits
   403      * java.lang.SecurityManager#checkConnect checkConnect} method permits
   401      * connecting to the address and port number of the given remote endpoint.
   404      * connecting to the address and port number of the given remote endpoint.
   402      *
   405      *
   403      * <p> For <i>unix domain</i> channels, this method performs a security
   406      * <p> For <i>Unix Domain</i> channels, this method performs a security
   404      * manager {@link SecurityManager#checkPermission(Permission)} using
   407      * manager {@link SecurityManager#checkPermission(Permission)} using
   405      * a {@link java.io.FilePermission} constructed with the path from the
   408      * a {@link java.io.FilePermission} constructed with the path from the
   406      * destination address and "read,write" as the actions.
   409      * destination address and "read,write" as the actions.
   407      *
   410      *
   408      * <p> This method may be invoked at any time.  If a read or write
   411      * <p> This method may be invoked at any time.  If a read or write
   510      *
   513      *
   511      * <p> Where the channel is bound and connected to an Internet Protocol
   514      * <p> Where the channel is bound and connected to an Internet Protocol
   512      * socket address then the return value from this method is of type {@link
   515      * socket address then the return value from this method is of type {@link
   513      * java.net.InetSocketAddress}.
   516      * java.net.InetSocketAddress}.
   514      *
   517      *
   515      * <p> Where the channel is bound and connected to a <i>Unix domain</i>
   518      * <p> Where the channel is bound and connected to a <i>Unix Domain</i>
   516      * address, the returned address is a {@link UnixDomainSocketAddress}
   519      * address, the returned address is a {@link UnixDomainSocketAddress}
   517      *
   520      *
   518      * @return  The remote address; {@code null} if the channel's socket is not
   521      * @return  The remote address; {@code null} if the channel's socket is not
   519      *          connected
   522      *          connected
   520      *
   523      *
   580      * if the operation is allowed. If the operation is not allowed,
   583      * if the operation is allowed. If the operation is not allowed,
   581      * a {@code SocketAddress} representing the
   584      * a {@code SocketAddress} representing the
   582      * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
   585      * {@link java.net.InetAddress#getLoopbackAddress loopback} address and the
   583      * local port of the channel's socket is returned.
   586      * local port of the channel's socket is returned.
   584      * <p>
   587      * <p>
   585      * If there is a security manager set and this is an <i>unix domain</i> channel,
   588      * If there is a security manager set and this is a <i>Unix Domain</i> channel,
   586      * then this returns a {@link UnixDomainSocketAddress} corresponding to the
   589      * {@code checkPermission} is called with a {@link FilePermission} whose {@code
   587      * bound address.
   590      * path} is the path of the bound address and actions is {@code "read"}
       
   591      * and if the operation is allowed a {@link UnixDomainSocketAddress} 
       
   592      * corresponding to the bound address is returned. If not, then an address with
       
   593      * an empty path is returned.
   588      *
   594      *
   589      * @return  The {@code SocketAddress} that the socket is bound to, or the
   595      * @return  The {@code SocketAddress} that the socket is bound to, or the
   590      *          {@code SocketAddress} representing the loopback address if
   596      *          {@code SocketAddress} representing the loopback address or an
   591      *          denied by the security manager, or {@code null} if the
   597      *          empty pathname, if denied by the security manager, or {@code null} if the
   592      *          channel's socket is not bound
   598      *          channel's socket is not bound
   593      *
   599      *
   594      * @throws  ClosedChannelException     {@inheritDoc}
   600      * @throws  ClosedChannelException     {@inheritDoc}
   595      * @throws  IOException                {@inheritDoc}
   601      * @throws  IOException                {@inheritDoc}
   596      */
   602      */