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.nio.ByteBuffer;
893N/Aimport java.util.concurrent.Future;
893N/A
893N/A/**
893N/A * An asynchronous channel that can read and write bytes.
893N/A *
893N/A * <p> Some channels may not allow more than one read or write to be outstanding
893N/A * at any given time. If a thread invokes a read method before a previous read
893N/A * operation has completed then a {@link ReadPendingException} will be thrown.
893N/A * Similarly, if a write method is invoked before a previous write has completed
893N/A * then {@link WritePendingException} is thrown. Whether or not other kinds of
893N/A * I/O operations may proceed concurrently with a read operation depends upon
893N/A * the type of the channel.
893N/A *
893N/A * <p> Note that {@link java.nio.ByteBuffer ByteBuffers} are not safe for use by
893N/A * multiple concurrent threads. When a read or write operation is initiated then
893N/A * care must be taken to ensure that the buffer is not accessed until the
893N/A * operation completes.
893N/A *
893N/A * @see Channels#newInputStream(AsynchronousByteChannel)
893N/A * @see Channels#newOutputStream(AsynchronousByteChannel)
893N/A *
893N/A * @since 1.7
893N/A */
893N/A
893N/Apublic interface AsynchronousByteChannel
893N/A extends AsynchronousChannel
893N/A{
893N/A /**
893N/A * Reads a sequence of bytes from this channel into the given buffer.
893N/A *
1580N/A * <p> This method initiates an asynchronous read operation to read a
1580N/A * sequence of bytes from this channel into the given buffer. The {@code
1580N/A * handler} parameter is a completion handler that is invoked when the read
1580N/A * operation completes (or fails). The result passed to the completion
1580N/A * handler is the number of bytes read or {@code -1} if no bytes could be
1580N/A * read because the channel has reached end-of-stream.
893N/A *
1580N/A * <p> The read operation may read up to <i>r</i> bytes from the channel,
1580N/A * where <i>r</i> is the number of bytes remaining in the buffer, that is,
1580N/A * {@code dst.remaining()} at the time that the read is attempted. Where
1580N/A * <i>r</i> is 0, the read operation completes immediately with a result of
1580N/A * {@code 0} without initiating an I/O operation.
893N/A *
893N/A * <p> Suppose that a byte sequence of length <i>n</i> is read, where
893N/A * <tt>0</tt>&nbsp;<tt>&lt;</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;<i>r</i>.
893N/A * This byte sequence will be transferred into the buffer so that the first
893N/A * byte in the sequence is at index <i>p</i> and the last byte is at index
893N/A * <i>p</i>&nbsp;<tt>+</tt>&nbsp;<i>n</i>&nbsp;<tt>-</tt>&nbsp;<tt>1</tt>,
893N/A * where <i>p</i> is the buffer's position at the moment the read is
893N/A * performed. Upon completion the buffer's position will be equal to
893N/A * <i>p</i>&nbsp;<tt>+</tt>&nbsp;<i>n</i>; its limit will not have changed.
893N/A *
893N/A * <p> Buffers are not safe for use by multiple concurrent threads so care
1580N/A * should be taken to not access the buffer until the operation has
1580N/A * completed.
893N/A *
893N/A * <p> This method may be invoked at any time. Some channel types may not
893N/A * allow more than one read to be outstanding at any given time. If a thread
893N/A * initiates a read operation before a previous read operation has
893N/A * completed then a {@link ReadPendingException} will be thrown.
893N/A *
893N/A * @param dst
893N/A * The buffer into which bytes are to be transferred
893N/A * @param attachment
893N/A * The object to attach to the I/O operation; can be {@code null}
893N/A * @param handler
1580N/A * The completion handler
893N/A *
893N/A * @throws IllegalArgumentException
893N/A * If the buffer is read-only
893N/A * @throws ReadPendingException
893N/A * If the channel does not allow more than one read to be outstanding
893N/A * and a previous read has not completed
1580N/A * @throws ShutdownChannelGroupException
1580N/A * If the channel is associated with a {@link AsynchronousChannelGroup
1580N/A * group} that has terminated
893N/A */
1580N/A <A> void read(ByteBuffer dst,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler);
893N/A
893N/A /**
893N/A * Reads a sequence of bytes from this channel into the given buffer.
893N/A *
1580N/A * <p> This method initiates an asynchronous read operation to read a
1580N/A * sequence of bytes from this channel into the given buffer. The method
1580N/A * behaves in exactly the same manner as the {@link
1580N/A * #read(ByteBuffer,Object,CompletionHandler)
1580N/A * read(ByteBuffer,Object,CompletionHandler)} method except that instead
1580N/A * of specifying a completion handler, this method returns a {@code Future}
1580N/A * representing the pending result. The {@code Future}'s {@link Future#get()
1580N/A * get} method returns the number of bytes read or {@code -1} if no bytes
1580N/A * could be read because the channel has reached end-of-stream.
893N/A *
893N/A * @param dst
893N/A * The buffer into which bytes are to be transferred
893N/A *
893N/A * @return A Future representing the result of the operation
893N/A *
893N/A * @throws IllegalArgumentException
893N/A * If the buffer is read-only
893N/A * @throws ReadPendingException
893N/A * If the channel does not allow more than one read to be outstanding
893N/A * and a previous read has not completed
893N/A */
893N/A Future<Integer> read(ByteBuffer dst);
893N/A
893N/A /**
893N/A * Writes a sequence of bytes to this channel from the given buffer.
893N/A *
1580N/A * <p> This method initiates an asynchronous write operation to write a
1580N/A * sequence of bytes to this channel from the given buffer. The {@code
1580N/A * handler} parameter is a completion handler that is invoked when the write
1580N/A * operation completes (or fails). The result passed to the completion
1580N/A * handler is the number of bytes written.
893N/A *
1580N/A * <p> The write operation may write up to <i>r</i> bytes to the channel,
1580N/A * where <i>r</i> is the number of bytes remaining in the buffer, that is,
1580N/A * {@code src.remaining()} at the time that the write is attempted. Where
1580N/A * <i>r</i> is 0, the write operation completes immediately with a result of
1580N/A * {@code 0} without initiating an I/O operation.
893N/A *
893N/A * <p> Suppose that a byte sequence of length <i>n</i> is written, where
893N/A * <tt>0</tt>&nbsp;<tt>&lt;</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;<i>r</i>.
893N/A * This byte sequence will be transferred from the buffer starting at index
893N/A * <i>p</i>, where <i>p</i> is the buffer's position at the moment the
893N/A * write is performed; the index of the last byte written will be
893N/A * <i>p</i>&nbsp;<tt>+</tt>&nbsp;<i>n</i>&nbsp;<tt>-</tt>&nbsp;<tt>1</tt>.
893N/A * Upon completion the buffer's position will be equal to
893N/A * <i>p</i>&nbsp;<tt>+</tt>&nbsp;<i>n</i>; its limit will not have changed.
893N/A *
893N/A * <p> Buffers are not safe for use by multiple concurrent threads so care
1580N/A * should be taken to not access the buffer until the operation has
1580N/A * completed.
893N/A *
893N/A * <p> This method may be invoked at any time. Some channel types may not
893N/A * allow more than one write to be outstanding at any given time. If a thread
893N/A * initiates a write operation before a previous write operation has
893N/A * completed then a {@link WritePendingException} will be thrown.
893N/A *
893N/A * @param src
893N/A * The buffer from which bytes are to be retrieved
893N/A * @param attachment
893N/A * The object to attach to the I/O operation; can be {@code null}
893N/A * @param handler
1580N/A * The completion handler object
893N/A *
893N/A * @throws WritePendingException
893N/A * If the channel does not allow more than one write to be outstanding
893N/A * and a previous write has not completed
1580N/A * @throws ShutdownChannelGroupException
1580N/A * If the channel is associated with a {@link AsynchronousChannelGroup
1580N/A * group} that has terminated
893N/A */
1580N/A <A> void write(ByteBuffer src,
1580N/A A attachment,
1580N/A CompletionHandler<Integer,? super A> handler);
893N/A
893N/A /**
893N/A * Writes a sequence of bytes to this channel from the given buffer.
893N/A *
1580N/A * <p> This method initiates an asynchronous write operation to write a
1580N/A * sequence of bytes to this channel from the given buffer. The method
1580N/A * behaves in exactly the same manner as the {@link
1580N/A * #write(ByteBuffer,Object,CompletionHandler)
1580N/A * write(ByteBuffer,Object,CompletionHandler)} method except that instead
1580N/A * of specifying a completion handler, this method returns a {@code Future}
1580N/A * representing the pending result. The {@code Future}'s {@link Future#get()
1580N/A * get} method returns the number of bytes written.
893N/A *
893N/A * @param src
893N/A * The buffer from which bytes are to be retrieved
893N/A *
893N/A * @return A Future representing the result of the operation
893N/A *
893N/A * @throws WritePendingException
893N/A * If the channel does not allow more than one write to be outstanding
893N/A * and a previous write has not completed
893N/A */
893N/A Future<Integer> write(ByteBuffer src);
893N/A}