|
1 /* |
|
2 * Copyright 2007-2009 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package java.nio.channels; |
|
27 |
|
28 import java.nio.ByteBuffer; |
|
29 import java.util.concurrent.Future; |
|
30 |
|
31 /** |
|
32 * An asynchronous channel that can read and write bytes. |
|
33 * |
|
34 * <p> Some channels may not allow more than one read or write to be outstanding |
|
35 * at any given time. If a thread invokes a read method before a previous read |
|
36 * operation has completed then a {@link ReadPendingException} will be thrown. |
|
37 * Similarly, if a write method is invoked before a previous write has completed |
|
38 * then {@link WritePendingException} is thrown. Whether or not other kinds of |
|
39 * I/O operations may proceed concurrently with a read operation depends upon |
|
40 * the type of the channel. |
|
41 * |
|
42 * <p> Note that {@link java.nio.ByteBuffer ByteBuffers} are not safe for use by |
|
43 * multiple concurrent threads. When a read or write operation is initiated then |
|
44 * care must be taken to ensure that the buffer is not accessed until the |
|
45 * operation completes. |
|
46 * |
|
47 * @see Channels#newInputStream(AsynchronousByteChannel) |
|
48 * @see Channels#newOutputStream(AsynchronousByteChannel) |
|
49 * |
|
50 * @since 1.7 |
|
51 */ |
|
52 |
|
53 public interface AsynchronousByteChannel |
|
54 extends AsynchronousChannel |
|
55 { |
|
56 /** |
|
57 * Reads a sequence of bytes from this channel into the given buffer. |
|
58 * |
|
59 * <p> This method initiates an operation to read a sequence of bytes from |
|
60 * this channel into the given buffer. The method returns a {@link Future} |
|
61 * representing the pending result of the operation. The result of the |
|
62 * operation, obtained by invoking the {@code Future} 's {@link |
|
63 * Future#get() get} method, is the number of bytes read or {@code -1} if |
|
64 * all bytes have been read and the channel has reached end-of-stream. |
|
65 * |
|
66 * <p> This method initiates a read operation to read up to <i>r</i> bytes |
|
67 * from the channel, where <i>r</i> is the number of bytes remaining in the |
|
68 * buffer, that is, {@code dst.remaining()} at the time that the read is |
|
69 * attempted. Where <i>r</i> is 0, the read operation completes immediately |
|
70 * with a result of {@code 0} without initiating an I/O operation. |
|
71 * |
|
72 * <p> Suppose that a byte sequence of length <i>n</i> is read, where |
|
73 * <tt>0</tt> <tt><</tt> <i>n</i> <tt><=</tt> <i>r</i>. |
|
74 * This byte sequence will be transferred into the buffer so that the first |
|
75 * byte in the sequence is at index <i>p</i> and the last byte is at index |
|
76 * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>, |
|
77 * where <i>p</i> is the buffer's position at the moment the read is |
|
78 * performed. Upon completion the buffer's position will be equal to |
|
79 * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. |
|
80 * |
|
81 * <p> Buffers are not safe for use by multiple concurrent threads so care |
|
82 * should be taken to not to access the buffer until the operaton has completed. |
|
83 * |
|
84 * <p> This method may be invoked at any time. Some channel types may not |
|
85 * allow more than one read to be outstanding at any given time. If a thread |
|
86 * initiates a read operation before a previous read operation has |
|
87 * completed then a {@link ReadPendingException} will be thrown. |
|
88 * |
|
89 * <p> The <tt>handler</tt> parameter is used to specify a {@link |
|
90 * CompletionHandler}. When the read operation completes the handler's |
|
91 * {@link CompletionHandler#completed completed} method is executed. |
|
92 * |
|
93 * |
|
94 * @param dst |
|
95 * The buffer into which bytes are to be transferred |
|
96 * @param attachment |
|
97 * The object to attach to the I/O operation; can be {@code null} |
|
98 * @param handler |
|
99 * The completion handler object; can be {@code null} |
|
100 * |
|
101 * @return A Future representing the result of the operation |
|
102 * |
|
103 * @throws IllegalArgumentException |
|
104 * If the buffer is read-only |
|
105 * @throws ReadPendingException |
|
106 * If the channel does not allow more than one read to be outstanding |
|
107 * and a previous read has not completed |
|
108 */ |
|
109 <A> Future<Integer> read(ByteBuffer dst, |
|
110 A attachment, |
|
111 CompletionHandler<Integer,? super A> handler); |
|
112 |
|
113 /** |
|
114 * Reads a sequence of bytes from this channel into the given buffer. |
|
115 * |
|
116 * <p> An invocation of this method of the form <tt>c.read(dst)</tt> |
|
117 * behaves in exactly the same manner as the invocation |
|
118 * <blockquote><pre> |
|
119 * c.read(dst, null, null);</pre></blockquote> |
|
120 * |
|
121 * @param dst |
|
122 * The buffer into which bytes are to be transferred |
|
123 * |
|
124 * @return A Future representing the result of the operation |
|
125 * |
|
126 * @throws IllegalArgumentException |
|
127 * If the buffer is read-only |
|
128 * @throws ReadPendingException |
|
129 * If the channel does not allow more than one read to be outstanding |
|
130 * and a previous read has not completed |
|
131 */ |
|
132 Future<Integer> read(ByteBuffer dst); |
|
133 |
|
134 /** |
|
135 * Writes a sequence of bytes to this channel from the given buffer. |
|
136 * |
|
137 * <p> This method initiates an operation to write a sequence of bytes to |
|
138 * this channel from the given buffer. This method returns a {@link |
|
139 * Future} representing the pending result of the operation. The result |
|
140 * of the operation, obtained by invoking the <tt>Future</tt>'s {@link |
|
141 * Future#get() get} method, is the number of bytes written, possibly zero. |
|
142 * |
|
143 * <p> This method initiates a write operation to write up to <i>r</i> bytes |
|
144 * to the channel, where <i>r</i> is the number of bytes remaining in the |
|
145 * buffer, that is, {@code src.remaining()} at the moment the write is |
|
146 * attempted. Where <i>r</i> is 0, the write operation completes immediately |
|
147 * with a result of {@code 0} without initiating an I/O operation. |
|
148 * |
|
149 * <p> Suppose that a byte sequence of length <i>n</i> is written, where |
|
150 * <tt>0</tt> <tt><</tt> <i>n</i> <tt><=</tt> <i>r</i>. |
|
151 * This byte sequence will be transferred from the buffer starting at index |
|
152 * <i>p</i>, where <i>p</i> is the buffer's position at the moment the |
|
153 * write is performed; the index of the last byte written will be |
|
154 * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>. |
|
155 * Upon completion the buffer's position will be equal to |
|
156 * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. |
|
157 * |
|
158 * <p> Buffers are not safe for use by multiple concurrent threads so care |
|
159 * should be taken to not to access the buffer until the operaton has completed. |
|
160 * |
|
161 * <p> This method may be invoked at any time. Some channel types may not |
|
162 * allow more than one write to be outstanding at any given time. If a thread |
|
163 * initiates a write operation before a previous write operation has |
|
164 * completed then a {@link WritePendingException} will be thrown. |
|
165 * |
|
166 * <p> The <tt>handler</tt> parameter is used to specify a {@link |
|
167 * CompletionHandler}. When the write operation completes the handler's |
|
168 * {@link CompletionHandler#completed completed} method is executed. |
|
169 * |
|
170 * @param src |
|
171 * The buffer from which bytes are to be retrieved |
|
172 * @param attachment |
|
173 * The object to attach to the I/O operation; can be {@code null} |
|
174 * @param handler |
|
175 * The completion handler object; can be {@code null} |
|
176 * |
|
177 * @return A Future representing the result of the operation |
|
178 * |
|
179 * @throws WritePendingException |
|
180 * If the channel does not allow more than one write to be outstanding |
|
181 * and a previous write has not completed |
|
182 */ |
|
183 <A> Future<Integer> write(ByteBuffer src, |
|
184 A attachment, |
|
185 CompletionHandler<Integer,? super A> handler); |
|
186 |
|
187 /** |
|
188 * Writes a sequence of bytes to this channel from the given buffer. |
|
189 * |
|
190 * <p> An invocation of this method of the form <tt>c.write(src)</tt> |
|
191 * behaves in exactly the same manner as the invocation |
|
192 * <blockquote><pre> |
|
193 * c.write(src, null, null);</pre></blockquote> |
|
194 * |
|
195 * @param src |
|
196 * The buffer from which bytes are to be retrieved |
|
197 * |
|
198 * @return A Future representing the result of the operation |
|
199 * |
|
200 * @throws WritePendingException |
|
201 * If the channel does not allow more than one write to be outstanding |
|
202 * and a previous write has not completed |
|
203 */ |
|
204 Future<Integer> write(ByteBuffer src); |
|
205 } |