1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java.nio.channels;
18
19import java.io.IOException;
20import java.nio.channels.spi.AbstractSelectableChannel;
21import java.nio.channels.spi.SelectorProvider;
22
23/**
24 * A pipe contains two channels. One is the writable sink channel and the other
25 * is the readable source channel. When bytes are written into the writable
26 * channel they can be read from the readable channel. The order of these bytes
27 * remains unchanged.
28 */
29public abstract class Pipe {
30
31    /**
32     * Writable sink channel used to write to a pipe.
33     */
34    public static abstract class SinkChannel extends AbstractSelectableChannel
35            implements WritableByteChannel, GatheringByteChannel {
36
37        /**
38         * Constructs a new {@code SinkChannel}.
39         *
40         * @param provider
41         *            the provider of the channel.
42         */
43        protected SinkChannel(SelectorProvider provider) {
44            super(provider);
45        }
46
47        /**
48         * Indicates that this channel only supports writing.
49         *
50         * @return a static value of OP_WRITE.
51         */
52        @Override
53        public final int validOps() {
54            return SelectionKey.OP_WRITE;
55        }
56    }
57
58    /**
59     * Readable source channel used to read from a pipe.
60     */
61    public static abstract class SourceChannel extends
62            AbstractSelectableChannel implements ReadableByteChannel,
63            ScatteringByteChannel {
64
65        /**
66         * Constructs a new {@code SourceChannel}.
67         *
68         * @param provider
69         *            the provider of the channel.
70         */
71        protected SourceChannel(SelectorProvider provider) {
72            super(provider);
73        }
74
75        /**
76         * Indicates that this channel only supports reading.
77         *
78         * @return a static value of OP_READ.
79         */
80        @Override
81        public final int validOps() {
82            return SelectionKey.OP_READ;
83        }
84
85    }
86
87    /**
88     * Initializes a pipe.
89     *
90     * @return a new instance of pipe.
91     *
92     * @throws IOException
93     *             if an I/O error occurs.
94     */
95    public static Pipe open() throws IOException {
96        return SelectorProvider.provider().openPipe();
97    }
98
99    /**
100     * The protected default constructor.
101     */
102    protected Pipe() {
103        super();
104    }
105
106    /**
107     * Returns the sink channel of the pipe.
108     *
109     * @return a writable sink channel of the pipe.
110     */
111    public abstract SinkChannel sink();
112
113    /**
114     * Returns the source channel of the pipe.
115     *
116     * @return a readable source channel of the pipe.
117     */
118    public abstract SourceChannel source();
119
120}
121