forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsynchronousByteChannel.java
214 lines (206 loc) · 11.5 KB
/
AsynchronousByteChannel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio.channels;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
/**
* An asynchronous channel that can read and write bytes.
*
* <p> Some channels may not allow more than one read or write to be outstanding
* at any given time. If a thread invokes a read method before a previous read
* operation has completed then a {@link ReadPendingException} will be thrown.
* Similarly, if a write method is invoked before a previous write has completed
* then {@link WritePendingException} is thrown. Whether or not other kinds of
* I/O operations may proceed concurrently with a read operation depends upon
* the type of the channel.
*
* <p> Note that {@link java.nio.ByteBuffer ByteBuffers} are not safe for use by
* multiple concurrent threads. When a read or write operation is initiated then
* care must be taken to ensure that the buffer is not accessed until the
* operation completes.
*
* @see Channels#newInputStream(AsynchronousByteChannel)
* @see Channels#newOutputStream(AsynchronousByteChannel)
* @since 1.7
*/
// 异步IO通道,支持基础的读写操作
public interface AsynchronousByteChannel extends AsynchronousChannel {
/**
* Reads a sequence of bytes from this channel into the given buffer.
*
* <p> This method initiates an asynchronous read operation to read a
* sequence of bytes from this channel into the given buffer. The method
* behaves in exactly the same manner as the {@link
* #read(ByteBuffer, Object, CompletionHandler)
* read(ByteBuffer,Object,CompletionHandler)} method except that instead
* of specifying a completion handler, this method returns a {@code Future}
* representing the pending result. The {@code Future}'s {@link Future#get()
* get} method returns the number of bytes read or {@code -1} if no bytes
* could be read because the channel has reached end-of-stream.
*
* @param dst The buffer into which bytes are to be transferred
*
* @return A Future representing the result of the operation
*
* @throws IllegalArgumentException If the buffer is read-only
* @throws ReadPendingException If the channel does not allow more than one read to be outstanding
* and a previous read has not completed
*/
/*
* 从当前通道读取数据并填充到缓冲区dst中(读取的字节数量最多填满缓冲区的剩余空间)
* 返回值是一个包含IO操作结果的Future,主线程轮询此Future以判断是否读取完成,以及获取实际读取到的字节数
*
* 注:此IO操作的结果是读取到的字节数。如果IO操作没成效,则执行结果可以是EOF或异常。
*/
Future<Integer> read(ByteBuffer dst);
/**
* Reads a sequence of bytes from this channel into the given buffer.
*
* <p> This method initiates an asynchronous read operation to read a
* sequence of bytes from this channel into the given buffer. The {@code
* handler} parameter is a completion handler that is invoked when the read
* operation completes (or fails). The result passed to the completion
* handler is the number of bytes read or {@code -1} if no bytes could be
* read because the channel has reached end-of-stream.
*
* <p> The read operation may read up to <i>r</i> bytes from the channel,
* where <i>r</i> is the number of bytes remaining in the buffer, that is,
* {@code dst.remaining()} at the time that the read is attempted. Where
* <i>r</i> is 0, the read operation completes immediately with a result of
* {@code 0} without initiating an I/O operation.
*
* <p> Suppose that a byte sequence of length <i>n</i> is read, where
* {@code 0} {@code <} <i>n</i> {@code <=} <i>r</i>.
* This byte sequence will be transferred into the buffer so that the first
* byte in the sequence is at index <i>p</i> and the last byte is at index
* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1},
* where <i>p</i> is the buffer's position at the moment the read is
* performed. Upon completion the buffer's position will be equal to
* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.
*
* <p> Buffers are not safe for use by multiple concurrent threads so care
* should be taken to not access the buffer until the operation has
* completed.
*
* <p> This method may be invoked at any time. Some channel types may not
* allow more than one read to be outstanding at any given time. If a thread
* initiates a read operation before a previous read operation has
* completed then a {@link ReadPendingException} will be thrown.
*
* @param <A> The type of the attachment
* @param dst The buffer into which bytes are to be transferred
* @param attachment The object to attach to the I/O operation; can be {@code null}
* @param handler The completion handler
*
* @throws IllegalArgumentException If the buffer is read-only
* @throws ReadPendingException If the channel does not allow more than one read to be outstanding
* and a previous read has not completed
* @throws ShutdownChannelGroupException If the channel is associated with a {@link AsynchronousChannelGroup
* group} that has terminated
*/
/*
* 从当前通道读取数据并填充到缓冲区dst中
* 最后一个参数是异步IO回调句柄,由工作线程执行完任务之后通过handler中的回调方法通知主线程,以便主线程获取实际读到的字节数
*
* 注:此IO操作的结果是读取到的字节数。如果IO操作没成效,则执行结果可以是EOF或异常。
*/
<A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler);
/**
* Writes a sequence of bytes to this channel from the given buffer.
*
* <p> This method initiates an asynchronous write operation to write a
* sequence of bytes to this channel from the given buffer. The method
* behaves in exactly the same manner as the {@link
* #write(ByteBuffer, Object, CompletionHandler)
* write(ByteBuffer,Object,CompletionHandler)} method except that instead
* of specifying a completion handler, this method returns a {@code Future}
* representing the pending result. The {@code Future}'s {@link Future#get()
* get} method returns the number of bytes written.
*
* @param src The buffer from which bytes are to be retrieved
*
* @return A Future representing the result of the operation
*
* @throws WritePendingException If the channel does not allow more than one write to be outstanding
* and a previous write has not completed
*/
/*
* 从源缓冲区src中读取数据,并将读到的内容写入到当前通道中
* 返回值是一个包含IO操作结果的Future,主线程轮询此Future以判断是否写入完成,以及获取实际写入的字节数
*
* 注:此IO操作的结果是写入的字节数。如果IO操作没成效,则执行结果可以是异常。
*/
Future<Integer> write(ByteBuffer src);
/**
* Writes a sequence of bytes to this channel from the given buffer.
*
* <p> This method initiates an asynchronous write operation to write a
* sequence of bytes to this channel from the given buffer. The {@code
* handler} parameter is a completion handler that is invoked when the write
* operation completes (or fails). The result passed to the completion
* handler is the number of bytes written.
*
* <p> The write operation may write up to <i>r</i> bytes to the channel,
* where <i>r</i> is the number of bytes remaining in the buffer, that is,
* {@code src.remaining()} at the time that the write is attempted. Where
* <i>r</i> is 0, the write operation completes immediately with a result of
* {@code 0} without initiating an I/O operation.
*
* <p> Suppose that a byte sequence of length <i>n</i> is written, where
* {@code 0} {@code <} <i>n</i> {@code <=} <i>r</i>.
* This byte sequence will be transferred from the buffer starting at index
* <i>p</i>, where <i>p</i> is the buffer's position at the moment the
* write is performed; the index of the last byte written will be
* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1}.
* Upon completion the buffer's position will be equal to
* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.
*
* <p> Buffers are not safe for use by multiple concurrent threads so care
* should be taken to not access the buffer until the operation has
* completed.
*
* <p> This method may be invoked at any time. Some channel types may not
* allow more than one write to be outstanding at any given time. If a thread
* initiates a write operation before a previous write operation has
* completed then a {@link WritePendingException} will be thrown.
*
* @param <A> The type of the attachment
* @param src The buffer from which bytes are to be retrieved
* @param attachment The object to attach to the I/O operation; can be {@code null}
* @param handler The completion handler object
*
* @throws WritePendingException If the channel does not allow more than one write to be outstanding
* and a previous write has not completed
* @throws ShutdownChannelGroupException If the channel is associated with a {@link AsynchronousChannelGroup
* group} that has terminated
*/
/*
* 从源缓冲区src中读取数据,并将读到的内容写入到当前通道中
* 最后一个参数是异步IO回调句柄,由工作线程执行完任务之后通过handler中的回调方法通知主线程,以便主线程获取实际写入的字节数
*
* 注:此IO操作的结果是写入的字节数。如果IO操作没成效,则执行结果可以是异常。
*/
<A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler);
}