Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/ch/SourceChannelImpl.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.*;3536/**37* Pipe.SourceChannel implementation based on socket connection.38*/3940class SourceChannelImpl41extends Pipe.SourceChannel42implements SelChImpl43{44// The SocketChannel assoicated with this pipe45SocketChannel sc;4647public FileDescriptor getFD() {48return ((SocketChannelImpl) sc).getFD();49}5051public int getFDVal() {52return ((SocketChannelImpl) sc).getFDVal();53}5455SourceChannelImpl(SelectorProvider sp, SocketChannel sc) {56super(sp);57this.sc = sc;58}5960protected void implCloseSelectableChannel() throws IOException {61if (!isRegistered())62kill();63}6465public void kill() throws IOException {66sc.close();67}6869protected void implConfigureBlocking(boolean block) throws IOException {70sc.configureBlocking(block);71}7273public boolean translateReadyOps(int ops, int initialOps,74SelectionKeyImpl sk) {75int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes76int oldOps = sk.nioReadyOps();77int newOps = initialOps;7879if ((ops & Net.POLLNVAL) != 0)80throw new Error("POLLNVAL detected");8182if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {83newOps = intOps;84sk.nioReadyOps(newOps);85return (newOps & ~oldOps) != 0;86}8788if (((ops & Net.POLLIN) != 0) &&89((intOps & SelectionKey.OP_READ) != 0))90newOps |= SelectionKey.OP_READ;9192sk.nioReadyOps(newOps);93return (newOps & ~oldOps) != 0;94}9596public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {97return translateReadyOps(ops, sk.nioReadyOps(), sk);98}99100public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {101return translateReadyOps(ops, 0, sk);102}103104public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {105if ((ops & SelectionKey.OP_READ) != 0)106ops = Net.POLLIN;107sk.selector.putEventOps(sk, ops);108}109110public int read(ByteBuffer dst) throws IOException {111try {112return sc.read(dst);113} catch (AsynchronousCloseException x) {114close();115throw x;116}117}118119public long read(ByteBuffer[] dsts, int offset, int length)120throws IOException121{122if ((offset < 0) || (length < 0) || (offset > dsts.length - length))123throw new IndexOutOfBoundsException();124try {125return read(Util.subsequence(dsts, offset, length));126} catch (AsynchronousCloseException x) {127close();128throw x;129}130}131132public long read(ByteBuffer[] dsts) throws IOException {133try {134return sc.read(dsts);135} catch (AsynchronousCloseException x) {136close();137throw x;138}139}140141}142143144