< prev index next >

src/java.base/share/classes/java/net/Socket.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.InputStream;
  29 import java.io.OutputStream;
  30 import java.io.IOException;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.invoke.VarHandle;
  33 import java.nio.channels.SocketChannel;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;

  36 import java.util.Set;
  37 import java.util.Collections;
  38 
  39 /**
  40  * This class implements client sockets (also called just
  41  * "sockets"). A socket is an endpoint for communication
  42  * between two machines.
  43  * <p>
  44  * The actual work of the socket is performed by an instance of the
  45  * {@code SocketImpl} class. An application, by changing
  46  * the socket factory that creates the socket implementation,
  47  * can configure itself to create sockets appropriate to the local
  48  * firewall.
  49  *
  50  * @author  unascribed
  51  * @see     java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  52  * @see     java.net.SocketImpl
  53  * @see     java.nio.channels.SocketChannel
  54  * @since   1.0
  55  */


1769      *
1770      * @throws UnsupportedOperationException if the socket does not support
1771      *         the option.
1772      *
1773      * @throws IllegalArgumentException if the value is not valid for
1774      *         the option.
1775      *
1776      * @throws IOException if an I/O error occurs, or if the socket is closed.
1777      *
1778      * @throws NullPointerException if name is {@code null}
1779      *
1780      * @throws SecurityException if a security manager is set and if the socket
1781      *         option requires a security permission and if the caller does
1782      *         not have the required permission.
1783      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1784      *         do not require any security permission.
1785      *
1786      * @since 9
1787      */
1788     public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {



1789         getImpl().setOption(name, value);
1790         return this;
1791     }
1792 
1793     /**
1794      * Returns the value of a socket option.
1795      *
1796      * @param <T> The type of the socket option value
1797      * @param name The socket option
1798      *
1799      * @return The value of the socket option.
1800      *
1801      * @throws UnsupportedOperationException if the socket does not support
1802      *         the option.
1803      *
1804      * @throws IOException if an I/O error occurs, or if the socket is closed.
1805      *
1806      * @throws NullPointerException if name is {@code null}
1807      *
1808      * @throws SecurityException if a security manager is set and if the socket
1809      *         option requires a security permission and if the caller does
1810      *         not have the required permission.
1811      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1812      *         do not require any security permission.
1813      *
1814      * @since 9
1815      */
1816     @SuppressWarnings("unchecked")
1817     public <T> T getOption(SocketOption<T> name) throws IOException {



1818         return getImpl().getOption(name);
1819     }
1820 
1821     // cache of unmodifiable impl options. Possibly set racy, in impl we trust
1822     private volatile Set<SocketOption<?>> options;
1823 
1824     /**
1825      * Returns a set of the socket options supported by this socket.
1826      *
1827      * This method will continue to return the set of options even after
1828      * the socket has been closed.
1829      *
1830      * @return A set of the socket options supported by this socket. This set
1831      *         may be empty if the socket's SocketImpl cannot be created.
1832      *
1833      * @since 9
1834      */
1835     public Set<SocketOption<?>> supportedOptions() {
1836         Set<SocketOption<?>> so = options;
1837         if (so != null)


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.InputStream;
  29 import java.io.OutputStream;
  30 import java.io.IOException;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.invoke.VarHandle;
  33 import java.nio.channels.SocketChannel;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;
  36 import java.util.Objects;
  37 import java.util.Set;
  38 import java.util.Collections;
  39 
  40 /**
  41  * This class implements client sockets (also called just
  42  * "sockets"). A socket is an endpoint for communication
  43  * between two machines.
  44  * <p>
  45  * The actual work of the socket is performed by an instance of the
  46  * {@code SocketImpl} class. An application, by changing
  47  * the socket factory that creates the socket implementation,
  48  * can configure itself to create sockets appropriate to the local
  49  * firewall.
  50  *
  51  * @author  unascribed
  52  * @see     java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
  53  * @see     java.net.SocketImpl
  54  * @see     java.nio.channels.SocketChannel
  55  * @since   1.0
  56  */


1770      *
1771      * @throws UnsupportedOperationException if the socket does not support
1772      *         the option.
1773      *
1774      * @throws IllegalArgumentException if the value is not valid for
1775      *         the option.
1776      *
1777      * @throws IOException if an I/O error occurs, or if the socket is closed.
1778      *
1779      * @throws NullPointerException if name is {@code null}
1780      *
1781      * @throws SecurityException if a security manager is set and if the socket
1782      *         option requires a security permission and if the caller does
1783      *         not have the required permission.
1784      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1785      *         do not require any security permission.
1786      *
1787      * @since 9
1788      */
1789     public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
1790         Objects.requireNonNull(name);
1791         if (isClosed())
1792             throw new SocketException("Socket is closed");
1793         getImpl().setOption(name, value);
1794         return this;
1795     }
1796 
1797     /**
1798      * Returns the value of a socket option.
1799      *
1800      * @param <T> The type of the socket option value
1801      * @param name The socket option
1802      *
1803      * @return The value of the socket option.
1804      *
1805      * @throws UnsupportedOperationException if the socket does not support
1806      *         the option.
1807      *
1808      * @throws IOException if an I/O error occurs, or if the socket is closed.
1809      *
1810      * @throws NullPointerException if name is {@code null}
1811      *
1812      * @throws SecurityException if a security manager is set and if the socket
1813      *         option requires a security permission and if the caller does
1814      *         not have the required permission.
1815      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1816      *         do not require any security permission.
1817      *
1818      * @since 9
1819      */
1820     @SuppressWarnings("unchecked")
1821     public <T> T getOption(SocketOption<T> name) throws IOException {
1822         Objects.requireNonNull(name);
1823         if (isClosed())
1824             throw new SocketException("Socket is closed");
1825         return getImpl().getOption(name);
1826     }
1827 
1828     // cache of unmodifiable impl options. Possibly set racy, in impl we trust
1829     private volatile Set<SocketOption<?>> options;
1830 
1831     /**
1832      * Returns a set of the socket options supported by this socket.
1833      *
1834      * This method will continue to return the set of options even after
1835      * the socket has been closed.
1836      *
1837      * @return A set of the socket options supported by this socket. This set
1838      *         may be empty if the socket's SocketImpl cannot be created.
1839      *
1840      * @since 9
1841      */
1842     public Set<SocketOption<?>> supportedOptions() {
1843         Set<SocketOption<?>> so = options;
1844         if (so != null)
< prev index next >