2
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
*/
|
|
28 |
|
|
29 |
package sun.nio.ch;
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.FileDescriptor;
|
|
33 |
import java.nio.ByteBuffer;
|
|
34 |
import java.nio.channels.*;
|
|
35 |
import java.nio.channels.spi.*;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Pipe.SourceChannel implementation based on socket connection.
|
|
39 |
*/
|
|
40 |
|
|
41 |
class 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 & PollArrayWrapper.POLLNVAL) != 0)
|
|
81 |
throw new Error("POLLNVAL detected");
|
|
82 |
|
|
83 |
if ((ops & (PollArrayWrapper.POLLERR
|
|
84 |
| PollArrayWrapper.POLLHUP)) != 0) {
|
|
85 |
newOps = intOps;
|
|
86 |
sk.nioReadyOps(newOps);
|
|
87 |
return (newOps & ~oldOps) != 0;
|
|
88 |
}
|
|
89 |
|
|
90 |
if (((ops & PollArrayWrapper.POLLIN) != 0) &&
|
|
91 |
((intOps & SelectionKey.OP_READ) != 0))
|
|
92 |
newOps |= SelectionKey.OP_READ;
|
|
93 |
|
|
94 |
sk.nioReadyOps(newOps);
|
|
95 |
return (newOps & ~oldOps) != 0;
|
|
96 |
}
|
|
97 |
|
|
98 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
|
|
99 |
return translateReadyOps(ops, sk.nioReadyOps(), sk);
|
|
100 |
}
|
|
101 |
|
|
102 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
|
|
103 |
return translateReadyOps(ops, 0, sk);
|
|
104 |
}
|
|
105 |
|
|
106 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
|
|
107 |
if ((ops & SelectionKey.OP_READ) != 0)
|
|
108 |
ops = PollArrayWrapper.POLLIN;
|
|
109 |
sk.selector.putEventOps(sk, ops);
|
|
110 |
}
|
|
111 |
|
|
112 |
public int read(ByteBuffer dst) throws IOException {
|
|
113 |
try {
|
|
114 |
return sc.read(dst);
|
|
115 |
} catch (AsynchronousCloseException x) {
|
|
116 |
close();
|
|
117 |
throw x;
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
public long read(ByteBuffer[] dsts, int offset, int length)
|
|
122 |
throws IOException
|
|
123 |
{
|
|
124 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
|
|
125 |
throw new IndexOutOfBoundsException();
|
|
126 |
try {
|
|
127 |
return read(Util.subsequence(dsts, offset, length));
|
|
128 |
} catch (AsynchronousCloseException x) {
|
|
129 |
close();
|
|
130 |
throw x;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
public long read(ByteBuffer[] dsts) throws IOException {
|
|
135 |
try {
|
|
136 |
return sc.read(dsts);
|
|
137 |
} catch (AsynchronousCloseException x) {
|
|
138 |
close();
|
|
139 |
throw x;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
}
|