src/share/classes/sun/nio/ch/SocketAdaptor.java

Print this page
rev 5501 : imported patch io-trace


  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 sun.nio.ch;
  27 
  28 import java.io.*;
  29 import java.lang.ref.*;
  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedExceptionAction;
  35 import java.util.*;
  36 


  37 
  38 // Make a socket channel look like a socket.
  39 //
  40 // The only aspects of java.net.Socket-hood that we don't attempt to emulate
  41 // here are the interrupted-I/O exceptions (which our Solaris implementations
  42 // attempt to support) and the sending of urgent data.  Otherwise an adapted
  43 // socket should look enough like a real java.net.Socket to fool most of the
  44 // developers most of the time, right down to the exception message strings.
  45 //
  46 // The methods in this class are defined in exactly the same order as in
  47 // java.net.Socket so as to simplify tracking future changes to that class.
  48 //
  49 
  50 public class SocketAdaptor
  51     extends Socket
  52 {
  53 
  54     // The channel being adapted
  55     private final SocketChannelImpl sc;
  56 


 187     private class SocketInputStream
 188         extends ChannelInputStream
 189     {
 190         private SocketInputStream() {
 191             super(sc);
 192         }
 193 
 194         protected int read(ByteBuffer bb)
 195             throws IOException
 196         {
 197             synchronized (sc.blockingLock()) {
 198                 if (!sc.isBlocking())
 199                     throw new IllegalBlockingModeException();
 200                 if (timeout == 0)
 201                     return sc.read(bb);
 202 
 203                 // Implement timeout with a selector
 204                 SelectionKey sk = null;
 205                 Selector sel = null;
 206                 sc.configureBlocking(false);


 207                 try {
 208                     int n;
 209                     if ((n = sc.read(bb)) != 0)
 210                         return n;
 211                     sel = Util.getTemporarySelector(sc);
 212                     sk = sc.register(sel, SelectionKey.OP_READ);
 213                     long to = timeout;
 214                     for (;;) {
 215                         if (!sc.isOpen())
 216                             throw new ClosedChannelException();
 217                         long st = System.currentTimeMillis();
 218                         int ns = sel.select(to);
 219                         if (ns > 0 && sk.isReadable()) {
 220                             if ((n = sc.read(bb)) != 0)
 221                                 return n;
 222                         }
 223                         sel.selectedKeys().remove(sk);
 224                         to -= System.currentTimeMillis() - st;
 225                         if (to <= 0)
 226                             throw new SocketTimeoutException();
 227                     }
 228                 } finally {

 229                     if (sk != null)
 230                         sk.cancel();
 231                     if (sc.isOpen())
 232                         sc.configureBlocking(true);
 233                     if (sel != null)
 234                         Util.releaseTemporarySelector(sel);
 235                 }
 236 
 237             }
 238         }
 239     }
 240 
 241     private InputStream socketInputStream = null;
 242 
 243     public InputStream getInputStream() throws IOException {
 244         if (!sc.isOpen())
 245             throw new SocketException("Socket is closed");
 246         if (!sc.isConnected())
 247             throw new SocketException("Socket is not connected");
 248         if (!sc.isInputOpen())




  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 sun.nio.ch;
  27 
  28 import java.io.*;
  29 import java.lang.ref.*;
  30 import java.net.*;
  31 import java.nio.*;
  32 import java.nio.channels.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedExceptionAction;
  35 import java.util.*;
  36 
  37 import sun.misc.IoTrace;
  38 
  39 
  40 // Make a socket channel look like a socket.
  41 //
  42 // The only aspects of java.net.Socket-hood that we don't attempt to emulate
  43 // here are the interrupted-I/O exceptions (which our Solaris implementations
  44 // attempt to support) and the sending of urgent data.  Otherwise an adapted
  45 // socket should look enough like a real java.net.Socket to fool most of the
  46 // developers most of the time, right down to the exception message strings.
  47 //
  48 // The methods in this class are defined in exactly the same order as in
  49 // java.net.Socket so as to simplify tracking future changes to that class.
  50 //
  51 
  52 public class SocketAdaptor
  53     extends Socket
  54 {
  55 
  56     // The channel being adapted
  57     private final SocketChannelImpl sc;
  58 


 189     private class SocketInputStream
 190         extends ChannelInputStream
 191     {
 192         private SocketInputStream() {
 193             super(sc);
 194         }
 195 
 196         protected int read(ByteBuffer bb)
 197             throws IOException
 198         {
 199             synchronized (sc.blockingLock()) {
 200                 if (!sc.isBlocking())
 201                     throw new IllegalBlockingModeException();
 202                 if (timeout == 0)
 203                     return sc.read(bb);
 204 
 205                 // Implement timeout with a selector
 206                 SelectionKey sk = null;
 207                 Selector sel = null;
 208                 sc.configureBlocking(false);
 209                 int n = 0;
 210                 Object traceContext = IoTrace.socketReadBegin(getInetAddress(), getPort(), timeout);
 211                 try {

 212                     if ((n = sc.read(bb)) != 0)
 213                         return n;
 214                     sel = Util.getTemporarySelector(sc);
 215                     sk = sc.register(sel, SelectionKey.OP_READ);
 216                     long to = timeout;
 217                     for (;;) {
 218                         if (!sc.isOpen())
 219                             throw new ClosedChannelException();
 220                         long st = System.currentTimeMillis();
 221                         int ns = sel.select(to);
 222                         if (ns > 0 && sk.isReadable()) {
 223                             if ((n = sc.read(bb)) != 0)
 224                                 return n;
 225                         }
 226                         sel.selectedKeys().remove(sk);
 227                         to -= System.currentTimeMillis() - st;
 228                         if (to <= 0)
 229                             throw new SocketTimeoutException();
 230                     }
 231                 } finally {
 232                     IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
 233                     if (sk != null)
 234                         sk.cancel();
 235                     if (sc.isOpen())
 236                         sc.configureBlocking(true);
 237                     if (sel != null)
 238                         Util.releaseTemporarySelector(sel);
 239                 }
 240 
 241             }
 242         }
 243     }
 244 
 245     private InputStream socketInputStream = null;
 246 
 247     public InputStream getInputStream() throws IOException {
 248         if (!sc.isOpen())
 249             throw new SocketException("Socket is closed");
 250         if (!sc.isConnected())
 251             throw new SocketException("Socket is not connected");
 252         if (!sc.isInputOpen())