jdk/src/share/classes/sun/nio/ch/OptionAdaptor.java
changeset 1203 3e5496df0d2b
parent 1202 5a725d2f0daa
parent 1201 e87f9c042699
child 1211 b659a7cee935
equal deleted inserted replaced
1202:5a725d2f0daa 1203:3e5496df0d2b
     1 /*
       
     2  * Copyright 2001 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.io.*;
       
    29 import java.net.*;
       
    30 import java.nio.*;
       
    31 import java.nio.channels.*;
       
    32 
       
    33 
       
    34 // Adaptor class for java.net-style options
       
    35 //
       
    36 // The option get/set methods in the socket, server-socket, and datagram-socket
       
    37 // adaptors delegate to an instance of this class.
       
    38 //
       
    39 
       
    40 class OptionAdaptor {                                   // package-private
       
    41 
       
    42     private final SocketOpts.IP opts;
       
    43 
       
    44     OptionAdaptor(SocketChannelImpl sc) {
       
    45         opts = (SocketOpts.IP)sc.options();
       
    46     }
       
    47 
       
    48     OptionAdaptor(ServerSocketChannelImpl ssc) {
       
    49         opts = (SocketOpts.IP)ssc.options();
       
    50     }
       
    51 
       
    52     OptionAdaptor(DatagramChannelImpl dc) {
       
    53         opts = (SocketOpts.IP)dc.options();
       
    54     }
       
    55 
       
    56     private SocketOpts.IP opts() {
       
    57         return opts;
       
    58     }
       
    59 
       
    60     private SocketOpts.IP.TCP tcpOpts() {
       
    61         return (SocketOpts.IP.TCP)opts;
       
    62     }
       
    63 
       
    64     public void setTcpNoDelay(boolean on) throws SocketException {
       
    65         try {
       
    66             tcpOpts().noDelay(on);
       
    67         } catch (Exception x) {
       
    68             Net.translateToSocketException(x);
       
    69         }
       
    70     }
       
    71 
       
    72     public boolean getTcpNoDelay() throws SocketException {
       
    73         try {
       
    74             return tcpOpts().noDelay();
       
    75         } catch (Exception x) {
       
    76             Net.translateToSocketException(x);
       
    77             return false;               // Never happens
       
    78         }
       
    79     }
       
    80 
       
    81     public void setSoLinger(boolean on, int linger) throws SocketException {
       
    82         try {
       
    83             if (linger > 65535)
       
    84                 linger = 65535;
       
    85             opts().linger(on ? linger : -1);
       
    86         } catch (Exception x) {
       
    87             Net.translateToSocketException(x);
       
    88         }
       
    89     }
       
    90 
       
    91     public int getSoLinger() throws SocketException {
       
    92         try {
       
    93             return opts().linger();
       
    94         } catch (Exception x) {
       
    95             Net.translateToSocketException(x);
       
    96             return 0;                   // Never happens
       
    97         }
       
    98     }
       
    99 
       
   100     public void setOOBInline(boolean on) throws SocketException {
       
   101         try {
       
   102             opts().outOfBandInline(on);
       
   103         } catch (Exception x) {
       
   104             Net.translateToSocketException(x);
       
   105         }
       
   106     }
       
   107 
       
   108     public boolean getOOBInline() throws SocketException {
       
   109         try {
       
   110             return opts().outOfBandInline();
       
   111         } catch (Exception x) {
       
   112             Net.translateToSocketException(x);
       
   113             return false;               // Never happens
       
   114         }
       
   115     }
       
   116 
       
   117     public void setSendBufferSize(int size)
       
   118         throws SocketException
       
   119     {
       
   120         try {
       
   121             opts().sendBufferSize(size);
       
   122         } catch (Exception x) {
       
   123             Net.translateToSocketException(x);
       
   124         }
       
   125     }
       
   126 
       
   127     public int getSendBufferSize() throws SocketException {
       
   128         try {
       
   129             return opts().sendBufferSize();
       
   130         } catch (Exception x) {
       
   131             Net.translateToSocketException(x);
       
   132             return 0;                   // Never happens
       
   133         }
       
   134     }
       
   135 
       
   136     public void setReceiveBufferSize(int size)
       
   137         throws SocketException
       
   138     {
       
   139         try {
       
   140             opts().receiveBufferSize(size);
       
   141         } catch (Exception x) {
       
   142             Net.translateToSocketException(x);
       
   143         }
       
   144     }
       
   145 
       
   146     public int getReceiveBufferSize() throws SocketException {
       
   147         try {
       
   148             return opts().receiveBufferSize();
       
   149         } catch (Exception x) {
       
   150             Net.translateToSocketException(x);
       
   151             return 0;                   // Never happens
       
   152         }
       
   153     }
       
   154 
       
   155     public void setKeepAlive(boolean on) throws SocketException {
       
   156         try {
       
   157             opts().keepAlive(on);
       
   158         } catch (Exception x) {
       
   159             Net.translateToSocketException(x);
       
   160         }
       
   161     }
       
   162 
       
   163     public boolean getKeepAlive() throws SocketException {
       
   164         try {
       
   165             return opts().keepAlive();
       
   166         } catch (Exception x) {
       
   167             Net.translateToSocketException(x);
       
   168             return false;               // Never happens
       
   169         }
       
   170     }
       
   171 
       
   172     public void setTrafficClass(int tc) throws SocketException {
       
   173         if (tc < 0 || tc > 255)
       
   174             throw new IllegalArgumentException("tc is not in range 0 -- 255");
       
   175         try {
       
   176             opts().typeOfService(tc);
       
   177         } catch (Exception x) {
       
   178             Net.translateToSocketException(x);
       
   179         }
       
   180     }
       
   181 
       
   182     public int getTrafficClass() throws SocketException {
       
   183         try {
       
   184             return opts().typeOfService();
       
   185         } catch (Exception x) {
       
   186             Net.translateToSocketException(x);
       
   187             return 0;                   // Never happens
       
   188         }
       
   189     }
       
   190 
       
   191     public void setReuseAddress(boolean on)
       
   192         throws SocketException
       
   193     {
       
   194         try {
       
   195             opts().reuseAddress(on);
       
   196         } catch (Exception x) {
       
   197             Net.translateToSocketException(x);
       
   198         }
       
   199     }
       
   200 
       
   201     public boolean getReuseAddress() throws SocketException {
       
   202         try {
       
   203             return opts().reuseAddress();
       
   204         } catch (Exception x) {
       
   205             Net.translateToSocketException(x);
       
   206             return false;               // Never happens
       
   207         }
       
   208     }
       
   209 
       
   210     public void setBroadcast(boolean on)
       
   211         throws SocketException
       
   212     {
       
   213         try {
       
   214             opts().broadcast(on);
       
   215         } catch (Exception x) {
       
   216             Net.translateToSocketException(x);
       
   217         }
       
   218     }
       
   219 
       
   220     public boolean getBroadcast() throws SocketException {
       
   221         try {
       
   222             return opts().broadcast();
       
   223         } catch (Exception x) {
       
   224             Net.translateToSocketException(x);
       
   225             return false;               // Never happens
       
   226         }
       
   227     }
       
   228 
       
   229 }