author | alanb |
Sat, 23 Jun 2018 09:34:09 +0100 | |
changeset 50739 | 4bedc9338137 |
permissions | -rw-r--r-- |
50739
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
1 |
/* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
4 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
10 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
16 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
20 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
23 |
* questions. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
24 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
25 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
26 |
package jdk.nio; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
27 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
28 |
import java.io.FileDescriptor; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
29 |
import java.io.IOException; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
30 |
import java.nio.channels.SelectableChannel; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
31 |
import java.nio.channels.SelectionKey; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
32 |
import java.nio.channels.spi.AbstractSelectableChannel; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
33 |
import java.nio.channels.spi.SelectorProvider; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
34 |
import java.util.Objects; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
35 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
36 |
import sun.nio.ch.IOUtil; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
37 |
import sun.nio.ch.Net; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
38 |
import sun.nio.ch.SelChImpl; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
39 |
import sun.nio.ch.SelectionKeyImpl; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
40 |
import sun.nio.ch.SelectorProviderImpl; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
41 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
42 |
/** |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
43 |
* Defines static methods to create {@link java.nio.channels.Channel channels}. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
44 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
45 |
* <p> Unless otherwise specified, passing a {@code null} argument to any of the |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
46 |
* methods defined here will cause a {@code NullPointerException} to be thrown. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
47 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
48 |
* @since 11 |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
49 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
50 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
51 |
public final class Channels { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
52 |
private Channels() { } |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
53 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
54 |
/** |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
55 |
* An object used to coordinate the closing of a selectable channel created |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
56 |
* by {@link Channels#readWriteSelectableChannel readWriteSelectableChannel}. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
57 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
58 |
* @since 11 |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
59 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
60 |
public interface SelectableChannelCloser { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
61 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
62 |
/** |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
63 |
* Closes a selectable channel. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
64 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
65 |
* <p> This method is invoked by the channel's close method in order to |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
66 |
* perform the actual work of closing the channel. This method is only |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
67 |
* invoked if the channel has not yet been closed, and it is never |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
68 |
* invoked more than once by the channel's close implementation. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
69 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
70 |
* <p> An implementation of this method must arrange for any other |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
71 |
* thread that is blocked in an I/O operation upon the channel to return |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
72 |
* immediately, either by throwing an exception or by returning normally. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
73 |
* If the channel is {@link SelectableChannel#isRegistered registered} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
74 |
* with one or more {@link java.nio.channels.Selector Selector}s then |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
75 |
* the file descriptor should not be released until the {@link |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
76 |
* #implReleaseChannel implReleaseChannel} method is invoked. </p> |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
77 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
78 |
* @param sc |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
79 |
* The selectable channel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
80 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
81 |
* @throws IOException |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
82 |
* If an I/O error occurs while closing the file descriptor |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
83 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
84 |
* @see java.nio.channels.spi.AbstractInterruptibleChannel#implCloseChannel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
85 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
86 |
void implCloseChannel(SelectableChannel sc) throws IOException; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
87 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
88 |
/** |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
89 |
* Release the file descriptor and any resources for a selectable |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
90 |
* channel that closed while registered with one or more {@link |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
91 |
* java.nio.channels.Selector Selector}s. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
92 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
93 |
* <p> This method is for cases where a channel is closed when |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
94 |
* {@link java.nio.channels.SelectableChannel#isRegistered registered} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
95 |
* with one or more {@code Selector}s. A channel may remain registered |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
96 |
* for some time after it is closed. This method is invoked when the |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
97 |
* channel is eventually deregistered from the last {@code Selector} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
98 |
* that it was registered with. It is invoked at most once. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
99 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
100 |
* @apiNote This method is invoked while synchronized on the selector |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
101 |
* and its selected-key set. Great care must be taken to avoid deadlocks |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
102 |
* with other threads that also synchronize on these objects. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
103 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
104 |
* @param sc |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
105 |
* The closed selectable channel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
106 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
107 |
* @throws IOException |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
108 |
* If an I/O error occurs |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
109 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
110 |
* @see java.nio.channels.spi.AbstractSelector#deregister |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
111 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
112 |
void implReleaseChannel(SelectableChannel sc) throws IOException; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
113 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
114 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
115 |
/** |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
116 |
* Creates a selectable channel to a file descriptor that supports an |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
117 |
* {@link SelectableChannel#validOps() operation-set} of |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
118 |
* {@link SelectionKey#OP_READ OP_READ} and |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
119 |
* {@link SelectionKey#OP_WRITE OP_WRITE}. The selectable channel will be |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
120 |
* created by the default {@link SelectorProvider}. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
121 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
122 |
* <p> The given file descriptor is a socket or resource that can be |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
123 |
* multiplexed by a {@link java.nio.channels.Selector} for read and write |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
124 |
* readiness. Great care is required to coordinate direct use of the file |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
125 |
* descriptor with the use of the selectable channel. In particular, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
126 |
* changing the blocking mode or closing the file descriptor without careful |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
127 |
* coordination will result in unspecified and unsafe side effects. The |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
128 |
* given {@link SelectableChannelCloser SelectableChannelCloser} is invoked to |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
129 |
* close the file descriptor and to coordinate the closing when the channel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
130 |
* is registered with a {@code Selector}. </p> |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
131 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
132 |
* <p> If there is a security manager set then its |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
133 |
* {@link SecurityManager#checkRead(FileDescriptor) checkRead} and |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
134 |
* {@link SecurityManager#checkWrite(FileDescriptor) checkWrite} methods |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
135 |
* are invoked to check that the caller has permission to both read from and |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
136 |
* write to the file descriptor. </p> |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
137 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
138 |
* @implNote This method throws {@code UnsupportedOperationException} if |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
139 |
* the default {@code SelectorProvider} is not the JDK built-in implementation. |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
140 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
141 |
* @param fd |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
142 |
* The file descriptor |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
143 |
* @param closer |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
144 |
* The object to close the channel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
145 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
146 |
* @return The selectable channel |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
147 |
* |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
148 |
* @throws IllegalArgumentException |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
149 |
* If the file descriptor is not {@link FileDescriptor#valid() valid} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
150 |
* @throws SecurityException |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
151 |
* If denied by the security manager |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
152 |
*/ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
153 |
public static SelectableChannel readWriteSelectableChannel(FileDescriptor fd, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
154 |
SelectableChannelCloser closer) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
155 |
Objects.requireNonNull(closer); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
156 |
if (!fd.valid()) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
157 |
throw new IllegalArgumentException("file descriptor is not valid"); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
158 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
159 |
SecurityManager sm = System.getSecurityManager(); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
160 |
if (sm != null) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
161 |
sm.checkRead(fd); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
162 |
sm.checkWrite(fd); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
163 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
164 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
165 |
SelectorProvider provider = SelectorProvider.provider(); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
166 |
if (!(provider instanceof SelectorProviderImpl)) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
167 |
throw new UnsupportedOperationException("custom SelectorProvider"); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
168 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
169 |
return new ReadWriteChannelImpl((SelectorProviderImpl)provider, fd, closer); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
170 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
171 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
172 |
private static final class ReadWriteChannelImpl |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
173 |
extends AbstractSelectableChannel implements SelChImpl |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
174 |
{ |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
175 |
private final FileDescriptor fd; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
176 |
private final int fdVal; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
177 |
private final SelectableChannelCloser closer; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
178 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
179 |
ReadWriteChannelImpl(SelectorProviderImpl provider, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
180 |
FileDescriptor fd, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
181 |
SelectableChannelCloser closer) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
182 |
super(provider); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
183 |
this.fd = fd; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
184 |
this.fdVal = IOUtil.fdVal(fd); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
185 |
this.closer = closer; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
186 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
187 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
188 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
189 |
public FileDescriptor getFD() { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
190 |
return fd; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
191 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
192 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
193 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
194 |
public int getFDVal() { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
195 |
return fdVal; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
196 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
197 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
198 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
199 |
public int validOps() { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
200 |
return (SelectionKey.OP_READ | SelectionKey.OP_WRITE); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
201 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
202 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
203 |
private boolean translateReadyOps(int ops, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
204 |
int initialOps, |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
205 |
SelectionKeyImpl ski) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
206 |
int intOps = ski.nioInterestOps(); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
207 |
int oldOps = ski.nioReadyOps(); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
208 |
int newOps = initialOps; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
209 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
210 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
211 |
newOps = intOps; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
212 |
ski.nioReadyOps(newOps); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
213 |
return (newOps & ~oldOps) != 0; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
214 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
215 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
216 |
if (((ops & Net.POLLIN) != 0) && |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
217 |
((intOps & SelectionKey.OP_READ) != 0)) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
218 |
newOps |= SelectionKey.OP_READ; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
219 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
220 |
if (((ops & Net.POLLOUT) != 0) && |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
221 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
222 |
newOps |= SelectionKey.OP_WRITE; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
223 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
224 |
ski.nioReadyOps(newOps); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
225 |
return (newOps & ~oldOps) != 0; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
226 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
227 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
228 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
229 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
230 |
return translateReadyOps(ops, ski.nioReadyOps(), ski); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
231 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
232 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
233 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
234 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
235 |
return translateReadyOps(ops, 0, ski); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
236 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
237 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
238 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
239 |
public int translateInterestOps(int ops) { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
240 |
int newOps = 0; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
241 |
if ((ops & SelectionKey.OP_READ) != 0) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
242 |
newOps |= Net.POLLIN; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
243 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
244 |
newOps |= Net.POLLOUT; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
245 |
return newOps; |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
246 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
247 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
248 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
249 |
protected void implConfigureBlocking(boolean block) throws IOException { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
250 |
IOUtil.configureBlocking(fd, block); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
251 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
252 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
253 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
254 |
protected void implCloseSelectableChannel() throws IOException { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
255 |
closer.implCloseChannel(this); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
256 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
257 |
|
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
258 |
@Override |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
259 |
public void kill() throws IOException { |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
260 |
closer.implReleaseChannel(this); |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
261 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
262 |
} |
4bedc9338137
8198372: API to create a SelectableChannel to a FileDescriptor
alanb
parents:
diff
changeset
|
263 |
} |