2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package java.nio.channels;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.channels.spi.*;
|
|
30 |
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A pair of channels that implements a unidirectional pipe.
|
|
34 |
*
|
|
35 |
* <p> A pipe consists of a pair of channels: A writable {@link
|
|
36 |
* Pipe.SinkChannel </code>sink<code>} channel and a readable {@link
|
|
37 |
* Pipe.SourceChannel </code>source<code>} channel. Once some bytes are
|
|
38 |
* written to the sink channel they can be read from source channel in exactly
|
|
39 |
* the order in which they were written.
|
|
40 |
*
|
|
41 |
* <p> Whether or not a thread writing bytes to a pipe will block until another
|
|
42 |
* thread reads those bytes, or some previously-written bytes, from the pipe is
|
|
43 |
* system-dependent and therefore unspecified. Many pipe implementations will
|
|
44 |
* buffer up to a certain number of bytes between the sink and source channels,
|
|
45 |
* but such buffering should not be assumed. </p>
|
|
46 |
*
|
|
47 |
*
|
|
48 |
* @author Mark Reinhold
|
|
49 |
* @author JSR-51 Expert Group
|
|
50 |
* @since 1.4
|
|
51 |
*/
|
|
52 |
|
|
53 |
public abstract class Pipe {
|
|
54 |
|
|
55 |
/**
|
|
56 |
* A channel representing the readable end of a {@link Pipe}. </p>
|
|
57 |
*
|
|
58 |
* @since 1.4
|
|
59 |
*/
|
|
60 |
public static abstract class SourceChannel
|
|
61 |
extends AbstractSelectableChannel
|
|
62 |
implements ReadableByteChannel, ScatteringByteChannel
|
|
63 |
{
|
|
64 |
/**
|
|
65 |
* Constructs a new instance of this class.
|
|
66 |
*/
|
|
67 |
protected SourceChannel(SelectorProvider provider) {
|
|
68 |
super(provider);
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Returns an operation set identifying this channel's supported
|
|
73 |
* operations.
|
|
74 |
*
|
|
75 |
* <p> Pipe-source channels only support reading, so this method
|
|
76 |
* returns {@link SelectionKey#OP_READ}. </p>
|
|
77 |
*
|
|
78 |
* @return The valid-operation set
|
|
79 |
*/
|
|
80 |
public final int validOps() {
|
|
81 |
return SelectionKey.OP_READ;
|
|
82 |
}
|
|
83 |
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* A channel representing the writable end of a {@link Pipe}. </p>
|
|
88 |
*
|
|
89 |
* @since 1.4
|
|
90 |
*/
|
|
91 |
public static abstract class SinkChannel
|
|
92 |
extends AbstractSelectableChannel
|
|
93 |
implements WritableByteChannel, GatheringByteChannel
|
|
94 |
{
|
|
95 |
/**
|
|
96 |
* Initializes a new instance of this class.
|
|
97 |
*/
|
|
98 |
protected SinkChannel(SelectorProvider provider) {
|
|
99 |
super(provider);
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Returns an operation set identifying this channel's supported
|
|
104 |
* operations.
|
|
105 |
*
|
|
106 |
* <p> Pipe-sink channels only support writing, so this method returns
|
|
107 |
* {@link SelectionKey#OP_WRITE}. </p>
|
|
108 |
*
|
|
109 |
* @return The valid-operation set
|
|
110 |
*/
|
|
111 |
public final int validOps() {
|
|
112 |
return SelectionKey.OP_WRITE;
|
|
113 |
}
|
|
114 |
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Initializes a new instance of this class.
|
|
119 |
*/
|
|
120 |
protected Pipe() { }
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Returns this pipe's source channel. </p>
|
|
124 |
*
|
|
125 |
* @return This pipe's source channel
|
|
126 |
*/
|
|
127 |
public abstract SourceChannel source();
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Returns this pipe's sink channel. </p>
|
|
131 |
*
|
|
132 |
* @return This pipe's sink channel
|
|
133 |
*/
|
|
134 |
public abstract SinkChannel sink();
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Opens a pipe.
|
|
138 |
*
|
|
139 |
* <p> The new pipe is created by invoking the {@link
|
|
140 |
* java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the
|
|
141 |
* system-wide default {@link java.nio.channels.spi.SelectorProvider}
|
|
142 |
* object. </p>
|
|
143 |
*
|
|
144 |
* @return A new pipe
|
|
145 |
*
|
|
146 |
* @throws IOException
|
|
147 |
* If an I/O error occurs
|
|
148 |
*/
|
|
149 |
public static Pipe open() throws IOException {
|
|
150 |
return SelectorProvider.provider().openPipe();
|
|
151 |
}
|
|
152 |
|
|
153 |
}
|