< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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


 250     /**
 251      * Gets the local port.
 252      * @return an {@code int} representing the local port value
 253      */
 254     protected int getLocalPort() {
 255         return localPort;
 256     }
 257 
 258     /**
 259      * Gets the datagram socket file descriptor.
 260      * @return a {@code FileDescriptor} object representing the datagram socket
 261      * file descriptor
 262      */
 263     protected FileDescriptor getFileDescriptor() {
 264         return fd;
 265     }
 266 
 267     /**
 268      * Called to set a socket option.
 269      *

 270      * @param name The socket option
 271      *
 272      * @param value The value of the socket option. A value of {@code null}
 273      *              may be valid for some options.
 274      *
 275      * @throws UnsupportedOperationException if the DatagramSocketImpl does not
 276      *         support the option
 277      *
 278      * @throws NullPointerException if name is {@code null}
 279      *
 280      * @since 1.9
 281      */
 282     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
 283         if (name == StandardSocketOptions.SO_SNDBUF) {
 284             setOption(SocketOptions.SO_SNDBUF, value);
 285         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 286             setOption(SocketOptions.SO_RCVBUF, value);
 287         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 288             setOption(SocketOptions.SO_REUSEADDR, value);
 289         } else if (name == StandardSocketOptions.IP_TOS) {
 290             setOption(SocketOptions.IP_TOS, value);
 291         } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
 292             (getDatagramSocket() instanceof MulticastSocket)) {
 293             setOption(SocketOptions.IP_MULTICAST_IF2, value);
 294         } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
 295             (getDatagramSocket() instanceof MulticastSocket)) {
 296             if (! (value instanceof Integer)) {
 297                 throw new IllegalArgumentException("not an integer");
 298             }
 299             setTimeToLive((Integer)value);
 300         } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
 301             (getDatagramSocket() instanceof MulticastSocket)) {
 302             setOption(SocketOptions.IP_MULTICAST_LOOP, value);
 303         } else {
 304             throw new UnsupportedOperationException("unsupported option");
 305         }
 306     }
 307 
 308     /**
 309      * Called to get a socket option.
 310      *


 311      * @param name The socket option
 312      *
 313      * @throws UnsupportedOperationException if the DatagramSocketImpl does not
 314      *         support the option
 315      *
 316      * @throws NullPointerException if name is {@code null}

 317      *
 318      * @since 1.9
 319      */
 320     @SuppressWarnings("unchecked")
 321     protected <T> T getOption(SocketOption<T> name) throws IOException {
 322         if (name == StandardSocketOptions.SO_SNDBUF) {
 323             return (T) getOption(SocketOptions.SO_SNDBUF);
 324         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 325             return (T) getOption(SocketOptions.SO_RCVBUF);
 326         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 327             return (T) getOption(SocketOptions.SO_REUSEADDR);
 328         } else if (name == StandardSocketOptions.IP_TOS) {
 329             return (T) getOption(SocketOptions.IP_TOS);
 330         } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
 331             (getDatagramSocket() instanceof MulticastSocket)) {
 332             return (T) getOption(SocketOptions.IP_MULTICAST_IF2);
 333         } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
 334             (getDatagramSocket() instanceof MulticastSocket)) {
 335             Integer ttl = getTimeToLive();
 336             return (T)ttl;


   1 /*
   2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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


 250     /**
 251      * Gets the local port.
 252      * @return an {@code int} representing the local port value
 253      */
 254     protected int getLocalPort() {
 255         return localPort;
 256     }
 257 
 258     /**
 259      * Gets the datagram socket file descriptor.
 260      * @return a {@code FileDescriptor} object representing the datagram socket
 261      * file descriptor
 262      */
 263     protected FileDescriptor getFileDescriptor() {
 264         return fd;
 265     }
 266 
 267     /**
 268      * Called to set a socket option.
 269      *
 270      * @param <T> The type of the socket option value
 271      * @param name The socket option
 272      *
 273      * @param value The value of the socket option. A value of {@code null}
 274      *              may be valid for some options.
 275      *
 276      * @throws UnsupportedOperationException if the DatagramSocketImpl does not
 277      *         support the option
 278      *
 279      * @throws NullPointerException if name is {@code null}
 280      * @throws IOException if an I/O problem occurs while attempting to set the option
 281      * @since 1.9
 282      */
 283     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
 284         if (name == StandardSocketOptions.SO_SNDBUF) {
 285             setOption(SocketOptions.SO_SNDBUF, value);
 286         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 287             setOption(SocketOptions.SO_RCVBUF, value);
 288         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 289             setOption(SocketOptions.SO_REUSEADDR, value);
 290         } else if (name == StandardSocketOptions.IP_TOS) {
 291             setOption(SocketOptions.IP_TOS, value);
 292         } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
 293             (getDatagramSocket() instanceof MulticastSocket)) {
 294             setOption(SocketOptions.IP_MULTICAST_IF2, value);
 295         } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
 296             (getDatagramSocket() instanceof MulticastSocket)) {
 297             if (! (value instanceof Integer)) {
 298                 throw new IllegalArgumentException("not an integer");
 299             }
 300             setTimeToLive((Integer)value);
 301         } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
 302             (getDatagramSocket() instanceof MulticastSocket)) {
 303             setOption(SocketOptions.IP_MULTICAST_LOOP, value);
 304         } else {
 305             throw new UnsupportedOperationException("unsupported option");
 306         }
 307     }
 308 
 309     /**
 310      * Called to get a socket option.
 311      *
 312      * @return the socket option
 313      * @param <T> The type of the socket option value
 314      * @param name The socket option
 315      *
 316      * @throws UnsupportedOperationException if the DatagramSocketImpl does not
 317      *         support the option
 318      *
 319      * @throws NullPointerException if name is {@code null}
 320      * @throws IOException if an I/O problem occurs while attempting to set the option
 321      *
 322      * @since 1.9
 323      */
 324     @SuppressWarnings("unchecked")
 325     protected <T> T getOption(SocketOption<T> name) throws IOException {
 326         if (name == StandardSocketOptions.SO_SNDBUF) {
 327             return (T) getOption(SocketOptions.SO_SNDBUF);
 328         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 329             return (T) getOption(SocketOptions.SO_RCVBUF);
 330         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 331             return (T) getOption(SocketOptions.SO_REUSEADDR);
 332         } else if (name == StandardSocketOptions.IP_TOS) {
 333             return (T) getOption(SocketOptions.IP_TOS);
 334         } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
 335             (getDatagramSocket() instanceof MulticastSocket)) {
 336             return (T) getOption(SocketOptions.IP_MULTICAST_IF2);
 337         } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
 338             (getDatagramSocket() instanceof MulticastSocket)) {
 339             Integer ttl = getTimeToLive();
 340             return (T)ttl;


< prev index next >