forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pipe.java
163 lines (147 loc) · 5.44 KB
/
Pipe.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
/*
* Copyright (c) 2000, 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.io.IOException;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;
/**
* A pair of channels that implements a unidirectional pipe.
*
* <p> A pipe consists of a pair of channels: A writable {@link
* Pipe.SinkChannel sink} channel and a readable {@link Pipe.SourceChannel source}
* channel. Once some bytes are written to the sink channel they can be read
* from the source channel in exactly the order in which they were written.
*
* <p> Whether or not a thread writing bytes to a pipe will block until another
* thread reads those bytes, or some previously-written bytes, from the pipe is
* system-dependent and therefore unspecified. Many pipe implementations will
* buffer up to a certain number of bytes between the sink and source channels,
* but such buffering should not be assumed. </p>
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*/
/*
* 单向同步管道,用在通道选择器中
*
* 该管道由写通道SinkChannel和读通道SourceChannel组成,
* 可以从写通道写入数据,从读通道读取数据,并且内部的读写通道是连通的
*/
public abstract class Pipe {
/**
* Initializes a new instance of this class.
*/
protected Pipe() {
}
/**
* Opens a pipe.
*
* <p> The new pipe is created by invoking the {@link
* java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the
* system-wide default {@link java.nio.channels.spi.SelectorProvider}
* object. </p>
*
* @return A new pipe
*
* @throws IOException If an I/O error occurs
*/
// 构造一个Pipe对象
public static Pipe open() throws IOException {
return SelectorProvider.provider().openPipe();
}
/**
* Returns this pipe's sink channel.
*
* @return This pipe's sink channel
*/
// 返回管道中的写通道,可以向这里写入数据
public abstract SinkChannel sink();
/**
* Returns this pipe's source channel.
*
* @return This pipe's source channel
*/
// 返回管道中的读通道,可以从这里读取数据
public abstract SourceChannel source();
/**
* A channel representing the writable end of a {@link Pipe}.
*
* @since 1.4
*/
// 管道中的写通道,向这里写入数据
public abstract static class SinkChannel extends AbstractSelectableChannel implements WritableByteChannel, GatheringByteChannel {
/**
* Initializes a new instance of this class.
*
* @param provider The selector provider
*/
protected SinkChannel(SelectorProvider provider) {
super(provider);
}
/**
* Returns an operation set identifying this channel's supported
* operations.
*
* <p> Pipe-sink channels only support writing, so this method returns
* {@link SelectionKey#OP_WRITE}. </p>
*
* @return The valid-operation set
*/
// 返回当前通道允许的有效操作参数集
public final int validOps() {
return SelectionKey.OP_WRITE;
}
}
/**
* A channel representing the readable end of a {@link Pipe}.
*
* @since 1.4
*/
// 管道中的读通道,从这里读取数据
public abstract static class SourceChannel extends AbstractSelectableChannel implements ReadableByteChannel, ScatteringByteChannel {
/**
* Constructs a new instance of this class.
*
* @param provider The selector provider
*/
protected SourceChannel(SelectorProvider provider) {
super(provider);
}
/**
* Returns an operation set identifying this channel's supported
* operations.
*
* <p> Pipe-source channels only support reading, so this method
* returns {@link SelectionKey#OP_READ}. </p>
*
* @return The valid-operation set
*/
// 返回当前通道允许的有效操作参数集
public final int validOps() {
return SelectionKey.OP_READ;
}
}
}