001: /*
002: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * @(#)SourceChannelImpl.java 1.18 07/05/05
028: */
029:
030: package sun.nio.ch;
031:
032: import java.io.IOException;
033: import java.io.FileDescriptor;
034: import java.nio.ByteBuffer;
035: import java.nio.channels.*;
036: import java.nio.channels.spi.*;
037:
038: /**
039: * Pipe.SourceChannel implementation based on socket connection.
040: */
041:
042: class SourceChannelImpl extends Pipe.SourceChannel implements SelChImpl {
043: // The SocketChannel assoicated with this pipe
044: SocketChannel sc;
045:
046: public FileDescriptor getFD() {
047: return ((SocketChannelImpl) sc).getFD();
048: }
049:
050: public int getFDVal() {
051: return ((SocketChannelImpl) sc).getFDVal();
052: }
053:
054: SourceChannelImpl(SelectorProvider sp, SocketChannel sc) {
055: super (sp);
056: this .sc = sc;
057: }
058:
059: protected void implCloseSelectableChannel() throws IOException {
060: if (!isRegistered())
061: kill();
062: }
063:
064: public void kill() throws IOException {
065: sc.close();
066: }
067:
068: protected void implConfigureBlocking(boolean block)
069: throws IOException {
070: sc.configureBlocking(block);
071: }
072:
073: public boolean translateReadyOps(int ops, int initialOps,
074: SelectionKeyImpl sk) {
075: int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes
076: int oldOps = sk.nioReadyOps();
077: int newOps = initialOps;
078:
079: if ((ops & PollArrayWrapper.POLLNVAL) != 0)
080: throw new Error("POLLNVAL detected");
081:
082: if ((ops & (PollArrayWrapper.POLLERR | PollArrayWrapper.POLLHUP)) != 0) {
083: newOps = intOps;
084: sk.nioReadyOps(newOps);
085: return (newOps & ~oldOps) != 0;
086: }
087:
088: if (((ops & PollArrayWrapper.POLLIN) != 0)
089: && ((intOps & SelectionKey.OP_READ) != 0))
090: newOps |= SelectionKey.OP_READ;
091:
092: sk.nioReadyOps(newOps);
093: return (newOps & ~oldOps) != 0;
094: }
095:
096: public boolean translateAndUpdateReadyOps(int ops,
097: SelectionKeyImpl sk) {
098: return translateReadyOps(ops, sk.nioReadyOps(), sk);
099: }
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 = PollArrayWrapper.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: if ((offset < 0) || (length < 0)
123: || (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: }
|