Path: blob/master/src/java.base/windows/classes/sun/nio/ch/SourceChannelImpl.java
41139 views
/*1* Copyright (c) 2002, 2018, 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 pipe45private final SocketChannel 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, SelectionKeyImpl ski) {74int intOps = ski.nioInterestOps();75int oldOps = ski.nioReadyOps();76int newOps = initialOps;7778if ((ops & Net.POLLNVAL) != 0)79throw new Error("POLLNVAL detected");8081if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {82newOps = intOps;83ski.nioReadyOps(newOps);84return (newOps & ~oldOps) != 0;85}8687if (((ops & Net.POLLIN) != 0) &&88((intOps & SelectionKey.OP_READ) != 0))89newOps |= SelectionKey.OP_READ;9091ski.nioReadyOps(newOps);92return (newOps & ~oldOps) != 0;93}9495public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {96return translateReadyOps(ops, ski.nioReadyOps(), ski);97}9899public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) {100return translateReadyOps(ops, 0, ski);101}102103public int translateInterestOps(int ops) {104int newOps = 0;105if ((ops & SelectionKey.OP_READ) != 0)106newOps |= Net.POLLIN;107return newOps;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