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