893N/A/*
2362N/A * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage java.nio.channels;
893N/A
893N/Aimport java.io.IOException;
893N/Aimport java.util.concurrent.Future; // javadoc
893N/A
893N/A/**
893N/A * A channel that supports asynchronous I/O operations. Asynchronous I/O
893N/A * operations will usually take one of two forms:
893N/A *
893N/A * <ol>
893N/A * <li><pre>{@link Future}&lt;V&gt; <em>operation</em>(<em>...</em>)</pre></li>
1580N/A * <li><pre>void <em>operation</em>(<em>...</em> A attachment, {@link
1580N/A * CompletionHandler}&lt;V,? super A&gt; handler)</pre></li>
893N/A * </ol>
893N/A *
893N/A * where <i>operation</i> is the name of the I/O operation (read or write for
893N/A * example), <i>V</i> is the result type of the I/O operation, and <i>A</i> is
893N/A * the type of an object attached to the I/O operation to provide context when
893N/A * consuming the result. The attachment is important for cases where a
893N/A * <em>state-less</em> {@code CompletionHandler} is used to consume the result
893N/A * of many I/O operations.
893N/A *
893N/A * <p> In the first form, the methods defined by the {@link Future Future}
893N/A * interface may be used to check if the operation has completed, wait for its
893N/A * completion, and to retrieve the result. In the second form, a {@link
893N/A * CompletionHandler} is invoked to consume the result of the I/O operation when
1580N/A * it completes or fails.
893N/A *
893N/A * <p> A channel that implements this interface is <em>asynchronously
893N/A * closeable</em>: If an I/O operation is outstanding on the channel and the
893N/A * channel's {@link #close close} method is invoked, then the I/O operation
893N/A * fails with the exception {@link AsynchronousCloseException}.
893N/A *
893N/A * <p> Asynchronous channels are safe for use by multiple concurrent threads.
893N/A * Some channel implementations may support concurrent reading and writing, but
893N/A * may not allow more than one read and one write operation to be outstanding at
893N/A * any given time.
893N/A *
893N/A * <h4>Cancellation</h4>
893N/A *
893N/A * <p> The {@code Future} interface defines the {@link Future#cancel cancel}
1580N/A * method to cancel execution. This causes all threads waiting on the result of
1580N/A * the I/O operation to throw {@link java.util.concurrent.CancellationException}.
1580N/A * Whether the underlying I/O operation can be cancelled is highly implementation
1580N/A * specific and therefore not specified. Where cancellation leaves the channel,
1580N/A * or the entity to which it is connected, in an inconsistent state, then the
1580N/A * channel is put into an implementation specific <em>error state</em> that
1580N/A * prevents further attempts to initiate I/O operations that are <i>similar</i>
1580N/A * to the operation that was cancelled. For example, if a read operation is
1580N/A * cancelled but the implementation cannot guarantee that bytes have not been
1580N/A * read from the channel then it puts the channel into an error state; further
1580N/A * attempts to initiate a {@code read} operation cause an unspecified runtime
1580N/A * exception to be thrown. Similarly, if a write operation is cancelled but the
1580N/A * implementation cannot guarantee that bytes have not been written to the
1580N/A * channel then subsequent attempts to initiate a {@code write} will fail with
1580N/A * an unspecified runtime exception.
893N/A *
1580N/A * <p> Where the {@link Future#cancel cancel} method is invoked with the {@code
1580N/A * mayInterruptIfRunning} parameter set to {@code true} then the I/O operation
1580N/A * may be interrupted by closing the channel. In that case all threads waiting
1580N/A * on the result of the I/O operation throw {@code CancellationException} and
1580N/A * any other I/O operations outstanding on the channel complete with the
1580N/A * exception {@link AsynchronousCloseException}.
893N/A *
893N/A * <p> Where the {@code cancel} method is invoked to cancel read or write
1580N/A * operations then it is recommended that all buffers used in the I/O operations
1580N/A * be discarded or care taken to ensure that the buffers are not accessed while
1580N/A * the channel remains open.
893N/A *
893N/A * @since 1.7
893N/A */
893N/A
893N/Apublic interface AsynchronousChannel
893N/A extends Channel
893N/A{
893N/A /**
893N/A * Closes this channel.
893N/A *
893N/A * <p> Any outstanding asynchronous operations upon this channel will
893N/A * complete with the exception {@link AsynchronousCloseException}. After a
1580N/A * channel is closed, further attempts to initiate asynchronous I/O
893N/A * operations complete immediately with cause {@link ClosedChannelException}.
893N/A *
893N/A * <p> This method otherwise behaves exactly as specified by the {@link
893N/A * Channel} interface.
893N/A *
893N/A * @throws IOException
893N/A * If an I/O error occurs
893N/A */
893N/A @Override
893N/A void close() throws IOException;
893N/A}