blob: e7eb9bf6d00ca7ea82fb31cb327971153a0b875b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 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
26package java.nio.channels;
27
28import java.io.IOException;
29import 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
53public 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}