KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > channels > Pipe


1 /*
2  * @(#)Pipe.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio.channels;
9
10 import java.io.IOException JavaDoc;
11 import java.nio.channels.spi.*;
12
13
14 /**
15  * A pair of channels that implements a unidirectional pipe.
16  *
17  * <p> A pipe consists of a pair of channels: A writable {@link
18  * Pipe.SinkChannel </code>sink<code>} channel and a readable {@link
19  * Pipe.SourceChannel </code>source<code>} channel. Once some bytes are
20  * written to the sink channel they can be read from source channel in exactly
21  * the order in which they were written.
22  *
23  * <p> Whether or not a thread writing bytes to a pipe will block until another
24  * thread reads those bytes, or some previously-written bytes, from the pipe is
25  * system-dependent and therefore unspecified. Many pipe implementations will
26  * buffer up to a certain number of bytes between the sink and source channels,
27  * but such buffering should not be assumed. </p>
28  *
29  *
30  * @author Mark Reinhold
31  * @author JSR-51 Expert Group
32  * @version 1.20, 03/12/19
33  * @since 1.4
34  */

35
36 public abstract class Pipe {
37
38     /**
39      * A channel representing the readable end of a {@link Pipe}. </p>
40      *
41      * @since 1.4
42      */

43     public static abstract class SourceChannel
44     extends AbstractSelectableChannel
45     implements ReadableByteChannel JavaDoc, ScatteringByteChannel JavaDoc
46     {
47     /**
48      * Constructs a new instance of this class.
49      */

50     protected SourceChannel(SelectorProvider provider) {
51         super(provider);
52     }
53
54     /**
55      * Returns an operation set identifying this channel's supported
56      * operations.
57      *
58      * <p> Pipe-source channels only support reading, so this method
59      * returns {@link SelectionKey#OP_READ}. </p>
60      *
61      * @return The valid-operation set
62      */

63     public final int validOps() {
64         return SelectionKey.OP_READ;
65     }
66
67     }
68
69     /**
70      * A channel representing the writable end of a {@link Pipe}. </p>
71      *
72      * @since 1.4
73      */

74     public static abstract class SinkChannel
75     extends AbstractSelectableChannel
76     implements WritableByteChannel JavaDoc, GatheringByteChannel JavaDoc
77     {
78     /**
79      * Initializes a new instance of this class.
80      */

81     protected SinkChannel(SelectorProvider provider) {
82         super(provider);
83     }
84
85     /**
86      * Returns an operation set identifying this channel's supported
87      * operations.
88      *
89      * <p> Pipe-sink channels only support writing, so this method returns
90      * {@link SelectionKey#OP_WRITE}. </p>
91      *
92      * @return The valid-operation set
93      */

94     public final int validOps() {
95         return SelectionKey.OP_WRITE;
96     }
97
98     }
99
100     /**
101      * Initializes a new instance of this class.
102      */

103     protected Pipe() { }
104
105     /**
106      * Returns this pipe's source channel. </p>
107      *
108      * @return This pipe's source channel
109      */

110     public abstract SourceChannel source();
111
112     /**
113      * Returns this pipe's sink channel. </p>
114      *
115      * @return This pipe's sink channel
116      */

117     public abstract SinkChannel sink();
118
119     /**
120      * Opens a pipe.
121      *
122      * <p> The new pipe is created by invoking the {@link
123      * java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the
124      * system-wide default {@link java.nio.channels.spi.SelectorProvider}
125      * object. </p>
126      *
127      * @return A new pipe
128      *
129      * @throws IOException
130      * If an I/O error occurs
131      */

132     public static Pipe JavaDoc open() throws IOException JavaDoc {
133     return SelectorProvider.provider().openPipe();
134     }
135
136 }
137
Popular Tags