src/share/classes/java/net/SocketImpl.java

Print this page
rev 9687 : * * *


  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
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.io.FileDescriptor;



  32 
  33 /**
  34  * The abstract class {@code SocketImpl} is a common superclass
  35  * of all classes that actually implement sockets. It is used to
  36  * create both client and server sockets.
  37  * <p>
  38  * A "plain" socket implements these methods exactly as
  39  * described, without attempting to go through a firewall or proxy.
  40  *
  41  * @author  unascribed
  42  * @since   JDK1.0
  43  */
  44 public abstract class SocketImpl implements SocketOptions {
  45     /**
  46      * The actual Socket object.
  47      */
  48     Socket socket = null;
  49     ServerSocket serverSocket = null;
  50 
  51     /**


 338      * @param  connectionTime
 339      *         An {@code int} expressing the relative importance of a short
 340      *         connection time
 341      *
 342      * @param  latency
 343      *         An {@code int} expressing the relative importance of low
 344      *         latency
 345      *
 346      * @param  bandwidth
 347      *         An {@code int} expressing the relative importance of high
 348      *         bandwidth
 349      *
 350      * @since 1.5
 351      */
 352     protected void setPerformancePreferences(int connectionTime,
 353                                           int latency,
 354                                           int bandwidth)
 355     {
 356         /* Not implemented yet */
 357     }

































































































 358 }


  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
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import java.io.FileDescriptor;
  32 import java.util.Set;
  33 import java.util.HashSet;
  34 import java.util.Collections;
  35 
  36 /**
  37  * The abstract class {@code SocketImpl} is a common superclass
  38  * of all classes that actually implement sockets. It is used to
  39  * create both client and server sockets.
  40  * <p>
  41  * A "plain" socket implements these methods exactly as
  42  * described, without attempting to go through a firewall or proxy.
  43  *
  44  * @author  unascribed
  45  * @since   JDK1.0
  46  */
  47 public abstract class SocketImpl implements SocketOptions {
  48     /**
  49      * The actual Socket object.
  50      */
  51     Socket socket = null;
  52     ServerSocket serverSocket = null;
  53 
  54     /**


 341      * @param  connectionTime
 342      *         An {@code int} expressing the relative importance of a short
 343      *         connection time
 344      *
 345      * @param  latency
 346      *         An {@code int} expressing the relative importance of low
 347      *         latency
 348      *
 349      * @param  bandwidth
 350      *         An {@code int} expressing the relative importance of high
 351      *         bandwidth
 352      *
 353      * @since 1.5
 354      */
 355     protected void setPerformancePreferences(int connectionTime,
 356                                           int latency,
 357                                           int bandwidth)
 358     {
 359         /* Not implemented yet */
 360     }
 361 
 362     /**
 363      * Called to set a socket option. 
 364      *
 365      * @param name The socket option
 366      *
 367      * @param value The value of the socket option. A value of {@code null}
 368      *              may be valid for some options.
 369      *
 370      * @throws UnsupportedOperationException if the SocketImpl does not 
 371      *         support the option
 372      */
 373     protected <T> void setOption(SocketOption<T> name, T value)
 374         throws IOException
 375     {
 376         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 377             setOption(SocketOptions.SO_KEEPALIVE, value);
 378         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 379             setOption(SocketOptions.SO_SNDBUF, value);
 380         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 381             setOption(SocketOptions.SO_RCVBUF, value);
 382         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 383             setOption(SocketOptions.SO_REUSEADDR, value);
 384         } else if (name == StandardSocketOptions.SO_LINGER) {
 385             setOption(SocketOptions.SO_LINGER, value);
 386         } else if (name == StandardSocketOptions.IP_TOS) {
 387             setOption(SocketOptions.IP_TOS, value);
 388         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 389             setOption(SocketOptions.TCP_NODELAY, value);
 390         } else {
 391             throw new UnsupportedOperationException("unsupported option");
 392         }
 393     }
 394 
 395     /**
 396      * Called to get a socket option. 
 397      *
 398      * @param name The socket option
 399      *
 400      * @return the value of the named option
 401      *
 402      * @throws UnsupportedOperationException if the SocketImpl does not 
 403      *         support the option.
 404      */
 405     protected <T> T getOption(SocketOption<T> name) throws IOException
 406     {
 407         if (name == StandardSocketOptions.SO_KEEPALIVE) {
 408             return (T)getOption(SocketOptions.SO_KEEPALIVE);
 409         } else if (name == StandardSocketOptions.SO_SNDBUF) {
 410             return (T)getOption(SocketOptions.SO_SNDBUF);
 411         } else if (name == StandardSocketOptions.SO_RCVBUF) {
 412             return (T)getOption(SocketOptions.SO_RCVBUF);
 413         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
 414             return (T)getOption(SocketOptions.SO_REUSEADDR);
 415         } else if (name == StandardSocketOptions.SO_LINGER) {
 416             return (T)getOption(SocketOptions.SO_LINGER);
 417         } else if (name == StandardSocketOptions.IP_TOS) {
 418             return (T)getOption(SocketOptions.IP_TOS);
 419         } else if (name == StandardSocketOptions.TCP_NODELAY) {
 420             return (T)getOption(SocketOptions.TCP_NODELAY);
 421         } else {
 422             throw new UnsupportedOperationException("unsupported option");
 423         }
 424     }
 425 
 426     private static final  Set<SocketOption<?>> socketOptions = 
 427         new HashSet<>();
 428 
 429     private static final  Set<SocketOption<?>> serverSocketOptions = 
 430         new HashSet<>();
 431 
 432     static {
 433         socketOptions.add(StandardSocketOptions.SO_KEEPALIVE);
 434         socketOptions.add(StandardSocketOptions.SO_SNDBUF);
 435         socketOptions.add(StandardSocketOptions.SO_RCVBUF);
 436         socketOptions.add(StandardSocketOptions.SO_REUSEADDR);
 437         socketOptions.add(StandardSocketOptions.SO_LINGER);
 438         socketOptions.add(StandardSocketOptions.IP_TOS);
 439         socketOptions.add(StandardSocketOptions.TCP_NODELAY);
 440 
 441         serverSocketOptions.add(StandardSocketOptions.SO_RCVBUF);
 442         serverSocketOptions.add(StandardSocketOptions.SO_REUSEADDR);
 443     };
 444 
 445     /**
 446      * Returns a set of SocketOptions supported by this impl
 447      * and by this impl's socket (Socket or ServerSocket)
 448      *
 449      * @return a Set of SocketOptions 
 450      */
 451     protected Set<SocketOption<?>> supportedOptions() {
 452         if (getSocket() != null) {
 453             return socketOptions;
 454         } else {
 455             return serverSocketOptions;
 456         } 
 457     }
 458 }