001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package java.nio.channels;
018:
019: import java.io.IOException;
020: import java.nio.channels.spi.AbstractInterruptibleChannel;
021: import java.nio.channels.spi.SelectorProvider;
022:
023: /**
024: * A channel that can be detected by a selector. The channel can be registered
025: * with some selectors, and when invoke select method of the selectors, the
026: * channel can be checked if it is readable, writable, connectable or acceptable
027: * according to its interesting operation.
028: */
029: public abstract class SelectableChannel extends
030: AbstractInterruptibleChannel implements Channel {
031:
032: /**
033: * Default constructor, can be overridden.
034: */
035: protected SelectableChannel() {
036: super ();
037: }
038:
039: /**
040: * Gets the blocking lock which synchronizes the configureBlocking and
041: * register methods.
042: *
043: * @return the blocking object as lock
044: */
045: public abstract Object blockingLock();
046:
047: /**
048: * Sets blocking mode of the channel.
049: *
050: * @param block
051: * true as blocking, false as non-blocking
052: * @return this channel
053: * @throws ClosedChannelException
054: * If this channel has been closed
055: * @throws IllegalBlockingModeException
056: * If the channel has been registered
057: * @throws IOException
058: * if I/O error occurs
059: */
060: public abstract SelectableChannel configureBlocking(boolean block)
061: throws IOException;
062:
063: /**
064: * Returns if channel is in blocking mode.
065: *
066: * @return true if channel is blocking
067: */
068: public abstract boolean isBlocking();
069:
070: /**
071: * Returns if channel is registered.
072: *
073: * @return true if channel is registered
074: */
075: public abstract boolean isRegistered();
076:
077: /**
078: * Gets the selection key for the channel with the given selector.
079: *
080: * @param sel
081: * the selector with which this channel may register
082: * @return the selection key for the channel according to the given selector
083: */
084: public abstract SelectionKey keyFor(Selector sel);
085:
086: /**
087: * Gets the provider of the channel.
088: *
089: * @return the provider of the channel
090: */
091: public abstract SelectorProvider provider();
092:
093: /**
094: * Registers with the given selector with a certain interesting operation.
095: *
096: * @param selector
097: * the selector with which this channel shall be registered
098: * @param operations
099: * the interesting operation
100: * @return the selection key indicates the channel
101: * @throws ClosedChannelException
102: * if the channel is closed
103: * @throws IllegalBlockingModeException
104: * If the channel is in blocking mode
105: * @throws IllegalSelectorException
106: * If this channel does not have the same provider as the given
107: * selector
108: * @throws CancelledKeyException
109: * If this channel is registered but its key has been cancelled
110: * @throws IllegalArgumentException
111: * If the operation given is unsupported by this channel
112: */
113: public final SelectionKey register(Selector selector, int operations)
114: throws ClosedChannelException {
115: return register(selector, operations, null);
116: }
117:
118: /**
119: * Registers with the given selector with a certain interesting operation
120: * and an attached object.
121: *
122: * @param sel
123: * the selector with which this channel shall be registered
124: * @param ops
125: * the interesting operation
126: * @param att
127: * The attached object, which can be null
128: * @return the selection key indicates the channel
129: * @throws ClosedChannelException
130: * if the channel is closed
131: * @throws IllegalBlockingModeException
132: * If the channel is in blocking mode
133: * @throws IllegalSelectorException
134: * If this channel does not have the same provider with the
135: * given selector
136: * @throws CancelledKeyException
137: * If this channel is registered but its key has been cancelled
138: * @throws IllegalArgumentException
139: * If the operation given is unsupported by this channel
140: */
141: public abstract SelectionKey register(Selector sel, int ops,
142: Object att) throws ClosedChannelException;
143:
144: /**
145: * Gets the possible interesting operation of the channel.
146: *
147: * @return the possible interesting operation of the channel
148: */
149: public abstract int validOps();
150: }
|