1 /*
   2  * Copyright (c) 2002, 2018, 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
  23  * questions.
  24  */
  25 
  26 /*
  27  */
  28 
  29 package sun.nio.ch;
  30 
  31 import java.io.IOException;
  32 import java.io.FileDescriptor;
  33 import java.net.StandardSocketOptions;
  34 import java.nio.ByteBuffer;
  35 import java.nio.channels.*;
  36 import java.nio.channels.spi.*;
  37 
  38 
  39 /**
  40  * Pipe.SinkChannel implementation based on socket connection.
  41  */
  42 
  43 class SinkChannelImpl
  44     extends Pipe.SinkChannel
  45     implements SelChImpl
  46 {
  47     // The SocketChannel assoicated with this pipe
  48     final SocketChannel sc;
  49 
  50     public FileDescriptor getFD() {
  51         return ((SocketChannelImpl)sc).getFD();
  52     }
  53 
  54     public int getFDVal() {
  55         return ((SocketChannelImpl)sc).getFDVal();
  56     }
  57 
  58     void setNoDelay() throws IOException {
  59         if (sc instanceof InetSocketChannelImpl) {
  60             InetSocketChannelImpl isc = (InetSocketChannelImpl)sc;
  61             isc.setOption(StandardSocketOptions.TCP_NODELAY, true);
  62         }
  63     }
  64 
  65     SinkChannelImpl(SelectorProvider sp, SocketChannel sc) {
  66         super(sp);
  67         this.sc = sc;
  68     }
  69 
  70     protected void implCloseSelectableChannel() throws IOException {
  71         if (!isRegistered())
  72             kill();
  73     }
  74 
  75     public void kill() throws IOException {
  76         sc.close();
  77     }
  78 
  79     protected void implConfigureBlocking(boolean block) throws IOException {
  80         sc.configureBlocking(block);
  81     }
  82 
  83     public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {
  84         int intOps = ski.nioInterestOps();
  85         int oldOps = ski.nioReadyOps();
  86         int newOps = initialOps;
  87 
  88         if ((ops & Net.POLLNVAL) != 0)
  89             throw new Error("POLLNVAL detected");
  90 
  91         if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
  92             newOps = intOps;
  93             ski.nioReadyOps(newOps);
  94             return (newOps & ~oldOps) != 0;
  95         }
  96 
  97         if (((ops & Net.POLLOUT) != 0) &&
  98             ((intOps & SelectionKey.OP_WRITE) != 0))
  99             newOps |= SelectionKey.OP_WRITE;
 100 
 101         ski.nioReadyOps(newOps);
 102         return (newOps & ~oldOps) != 0;
 103     }
 104 
 105     public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
 106         return translateReadyOps(ops, ski.nioReadyOps(), ski);
 107     }
 108 
 109     public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {
 110         return translateReadyOps(ops, 0, ski);
 111     }
 112 
 113     public int translateInterestOps(int ops) {
 114         int newOps = 0;
 115         if ((ops & SelectionKey.OP_WRITE) != 0)
 116             newOps |= Net.POLLOUT;
 117         return newOps;
 118     }
 119 
 120     public int write(ByteBuffer src) throws IOException {
 121         try {
 122             return sc.write(src);
 123         } catch (AsynchronousCloseException x) {
 124             close();
 125             throw x;
 126         }
 127     }
 128 
 129     public long write(ByteBuffer[] srcs) throws IOException {
 130         try {
 131             return sc.write(srcs);
 132         } catch (AsynchronousCloseException x) {
 133             close();
 134             throw x;
 135         }
 136     }
 137 
 138     public long write(ByteBuffer[] srcs, int offset, int length)
 139         throws IOException
 140     {
 141         if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
 142            throw new IndexOutOfBoundsException();
 143         try {
 144             return write(Util.subsequence(srcs, offset, length));
 145         } catch (AsynchronousCloseException x) {
 146             close();
 147             throw x;
 148         }
 149     }
 150 }