Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/ch/SinkChannelImpl.java
32288 views
/*1* Copyright (c) 2002, 2006, 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.nio.ByteBuffer;33import java.nio.channels.*;34import java.nio.channels.spi.*;353637/**38* Pipe.SinkChannel implementation based on socket connection.39*/4041class SinkChannelImpl42extends Pipe.SinkChannel43implements SelChImpl44{45// The SocketChannel assoicated with this pipe46SocketChannel sc;4748public FileDescriptor getFD() {49return ((SocketChannelImpl)sc).getFD();50}5152public int getFDVal() {53return ((SocketChannelImpl)sc).getFDVal();54}5556SinkChannelImpl(SelectorProvider sp, SocketChannel sc) {57super(sp);58this.sc = sc;59}6061protected void implCloseSelectableChannel() throws IOException {62if (!isRegistered())63kill();64}6566public void kill() throws IOException {67sc.close();68}6970protected void implConfigureBlocking(boolean block) throws IOException {71sc.configureBlocking(block);72}7374public boolean translateReadyOps(int ops, int initialOps,75SelectionKeyImpl sk) {76int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes77int oldOps = sk.nioReadyOps();78int newOps = initialOps;7980if ((ops & Net.POLLNVAL) != 0)81throw new Error("POLLNVAL detected");8283if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {84newOps = intOps;85sk.nioReadyOps(newOps);86return (newOps & ~oldOps) != 0;87}8889if (((ops & Net.POLLOUT) != 0) &&90((intOps & SelectionKey.OP_WRITE) != 0))91newOps |= SelectionKey.OP_WRITE;9293sk.nioReadyOps(newOps);94return (newOps & ~oldOps) != 0;95}9697public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {98return translateReadyOps(ops, sk.nioReadyOps(), sk);99}100101public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {102return translateReadyOps(ops, 0, sk);103}104105public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {106if ((ops & SelectionKey.OP_WRITE) != 0)107ops = Net.POLLOUT;108sk.selector.putEventOps(sk, ops);109}110111public int write(ByteBuffer src) throws IOException {112try {113return sc.write(src);114} catch (AsynchronousCloseException x) {115close();116throw x;117}118}119120public long write(ByteBuffer[] srcs) throws IOException {121try {122return sc.write(srcs);123} catch (AsynchronousCloseException x) {124close();125throw x;126}127}128129public long write(ByteBuffer[] srcs, int offset, int length)130throws IOException131{132if ((offset < 0) || (length < 0) || (offset > srcs.length - length))133throw new IndexOutOfBoundsException();134try {135return write(Util.subsequence(srcs, offset, length));136} catch (AsynchronousCloseException x) {137close();138throw x;139}140}141}142143144