1 /*
2  * Copyright (c) 2007, 2013, 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 package java.nio.channels;
27 
28 import java.io.IOException;
29 import java.util.concurrent.Future;  // javadoc
30 
31 /**
32  * A channel that supports asynchronous I/O operations. Asynchronous I/O
33  * operations will usually take one of two forms:
34  *
35  * <ol>
36  * <li><pre>{@link Future}&lt;V&gt; <em>operation</em>(<em>...</em>)</pre></li>
37  * <li><pre>void <em>operation</em>(<em>...</em> A attachment, {@link
38  *   CompletionHandler}&lt;V,? super A&gt; handler)</pre></li>
39  * </ol>
40  *
41  * where <i>operation</i> is the name of the I/O operation (read or write for
42  * example), <i>V</i> is the result type of the I/O operation, and <i>A</i> is
43  * the type of an object attached to the I/O operation to provide context when
44  * consuming the result. The attachment is important for cases where a
45  * <em>state-less</em> {@code CompletionHandler} is used to consume the result
46  * of many I/O operations.
47  *
48  * <p> In the first form, the methods defined by the {@link Future Future}
49  * interface may be used to check if the operation has completed, wait for its
50  * completion, and to retrieve the result. In the second form, a {@link
51  * CompletionHandler} is invoked to consume the result of the I/O operation when
52  * it completes or fails.
53  *
54  * <p> A channel that implements this interface is <em>asynchronously
55  * closeable</em>: If an I/O operation is outstanding on the channel and the
56  * channel's {@link #close close} method is invoked, then the I/O operation
57  * fails with the exception {@link AsynchronousCloseException}.
58  *
59  * <p> Asynchronous channels are safe for use by multiple concurrent threads.
60  * Some channel implementations may support concurrent reading and writing, but
61  * may not allow more than one read and one write operation to be outstanding at
62  * any given time.
63  *
64  * <h2>Cancellation</h2>
65  *
66  * <p> The {@code Future} interface defines the {@link Future#cancel cancel}
67  * method to cancel execution. This causes all threads waiting on the result of
68  * the I/O operation to throw {@link java.util.concurrent.CancellationException}.
69  * Whether the underlying I/O operation can be cancelled is highly implementation
70  * specific and therefore not specified. Where cancellation leaves the channel,
71  * or the entity to which it is connected, in an inconsistent state, then the
72  * channel is put into an implementation specific <em>error state</em> that
73  * prevents further attempts to initiate I/O operations that are <i>similar</i>
74  * to the operation that was cancelled. For example, if a read operation is
75  * cancelled but the implementation cannot guarantee that bytes have not been
76  * read from the channel then it puts the channel into an error state; further
77  * attempts to initiate a {@code read} operation cause an unspecified runtime
78  * exception to be thrown. Similarly, if a write operation is cancelled but the
79  * implementation cannot guarantee that bytes have not been written to the
80  * channel then subsequent attempts to initiate a {@code write} will fail with
81  * an unspecified runtime exception.
82  *
83  * <p> Where the {@link Future#cancel cancel} method is invoked with the {@code
84  * mayInterruptIfRunning} parameter set to {@code true} then the I/O operation
85  * may be interrupted by closing the channel. In that case all threads waiting
86  * on the result of the I/O operation throw {@code CancellationException} and
87  * any other I/O operations outstanding on the channel complete with the
88  * exception {@link AsynchronousCloseException}.
89  *
90  * <p> Where the {@code cancel} method is invoked to cancel read or write
91  * operations then it is recommended that all buffers used in the I/O operations
92  * be discarded or care taken to ensure that the buffers are not accessed while
93  * the channel remains open.
94  *
95  *  @since 1.7
96  */
97 
98 public interface AsynchronousChannel
99     extends Channel
100 {
101     /**
102      * Closes this channel.
103      *
104      * <p> Any outstanding asynchronous operations upon this channel will
105      * complete with the exception {@link AsynchronousCloseException}. After a
106      * channel is closed, further attempts to initiate asynchronous I/O
107      * operations complete immediately with cause {@link ClosedChannelException}.
108      *
109      * <p>  This method otherwise behaves exactly as specified by the {@link
110      * Channel} interface.
111      *
112      * @throws  IOException
113      *          If an I/O error occurs
114      */
115     @Override
close()116     void close() throws IOException;
117 }
118