Path: blob/master/src/java.base/windows/classes/sun/nio/ch/SinkChannelImpl.java
41139 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728package sun.nio.ch;2930import java.io.IOException;31import java.io.FileDescriptor;32import java.net.SocketOption;33import java.nio.ByteBuffer;34import java.nio.channels.*;35import java.nio.channels.spi.*;363738/**39* Pipe.SinkChannel implementation based on socket connection.40*/4142class SinkChannelImpl43extends Pipe.SinkChannel44implements SelChImpl45{46// The SocketChannel assoicated with this pipe47private final SocketChannelImpl sc;4849public FileDescriptor getFD() {50return sc.getFD();51}5253public int getFDVal() {54return sc.getFDVal();55}5657SinkChannelImpl(SelectorProvider sp, SocketChannel sc) {58super(sp);59this.sc = (SocketChannelImpl) sc;60}6162boolean isNetSocket() {63return sc.isNetSocket();64}6566<T> void setOption(SocketOption<T> name, T value) throws IOException {67sc.setOption(name, value);68}6970protected void implCloseSelectableChannel() throws IOException {71if (!isRegistered())72kill();73}7475public void kill() throws IOException {76sc.close();77}7879protected void implConfigureBlocking(boolean block) throws IOException {80sc.configureBlocking(block);81}8283public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) {84int intOps = ski.nioInterestOps();85int oldOps = ski.nioReadyOps();86int newOps = initialOps;8788if ((ops & Net.POLLNVAL) != 0)89throw new Error("POLLNVAL detected");9091if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {92newOps = intOps;93ski.nioReadyOps(newOps);94return (newOps & ~oldOps) != 0;95}9697if (((ops & Net.POLLOUT) != 0) &&98((intOps & SelectionKey.OP_WRITE) != 0))99newOps |= SelectionKey.OP_WRITE;100101ski.nioReadyOps(newOps);102return (newOps & ~oldOps) != 0;103}104105public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {106return translateReadyOps(ops, ski.nioReadyOps(), ski);107}108109public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {110return translateReadyOps(ops, 0, ski);111}112113public int translateInterestOps(int ops) {114int newOps = 0;115if ((ops & SelectionKey.OP_WRITE) != 0)116newOps |= Net.POLLOUT;117return newOps;118}119120public int write(ByteBuffer src) throws IOException {121try {122return sc.write(src);123} catch (AsynchronousCloseException x) {124close();125throw x;126}127}128129public long write(ByteBuffer[] srcs) throws IOException {130try {131return sc.write(srcs);132} catch (AsynchronousCloseException x) {133close();134throw x;135}136}137138public long write(ByteBuffer[] srcs, int offset, int length)139throws IOException140{141if ((offset < 0) || (length < 0) || (offset > srcs.length - length))142throw new IndexOutOfBoundsException();143try {144return write(Util.subsequence(srcs, offset, length));145} catch (AsynchronousCloseException x) {146close();147throw x;148}149}150}151152153