Path: blob/master/src/java.base/share/classes/java/nio/channels/Pipe.java
41159 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.io.IOException;28import java.nio.channels.spi.*;293031/**32* A pair of channels that implements a unidirectional pipe.33*34* <p> A pipe consists of a pair of channels: A writable {@link35* Pipe.SinkChannel sink} channel and a readable {@link Pipe.SourceChannel source}36* channel. Once some bytes are written to the sink channel they can be read37* from the source channel in exactly the order in which they were written.38*39* <p> Whether or not a thread writing bytes to a pipe will block until another40* thread reads those bytes, or some previously-written bytes, from the pipe is41* system-dependent and therefore unspecified. Many pipe implementations will42* buffer up to a certain number of bytes between the sink and source channels,43* but such buffering should not be assumed. </p>44*45*46* @author Mark Reinhold47* @author JSR-51 Expert Group48* @since 1.449*/5051public abstract class Pipe {5253/**54* A channel representing the readable end of a {@link Pipe}.55*56* @since 1.457*/58public abstract static class SourceChannel59extends AbstractSelectableChannel60implements ReadableByteChannel, ScatteringByteChannel61{62/**63* Constructs a new instance of this class.64*65* @param provider66* The selector provider67*/68protected SourceChannel(SelectorProvider provider) {69super(provider);70}7172/**73* Returns an operation set identifying this channel's supported74* operations.75*76* <p> Pipe-source channels only support reading, so this method77* returns {@link SelectionKey#OP_READ}. </p>78*79* @return The valid-operation set80*/81public final int validOps() {82return SelectionKey.OP_READ;83}8485}8687/**88* A channel representing the writable end of a {@link Pipe}.89*90* @since 1.491*/92public abstract static class SinkChannel93extends AbstractSelectableChannel94implements WritableByteChannel, GatheringByteChannel95{96/**97* Initializes a new instance of this class.98*99* @param provider100* The selector provider101*/102protected SinkChannel(SelectorProvider provider) {103super(provider);104}105106/**107* Returns an operation set identifying this channel's supported108* operations.109*110* <p> Pipe-sink channels only support writing, so this method returns111* {@link SelectionKey#OP_WRITE}. </p>112*113* @return The valid-operation set114*/115public final int validOps() {116return SelectionKey.OP_WRITE;117}118119}120121/**122* Initializes a new instance of this class.123*/124protected Pipe() { }125126/**127* Returns this pipe's source channel.128*129* @return This pipe's source channel130*/131public abstract SourceChannel source();132133/**134* Returns this pipe's sink channel.135*136* @return This pipe's sink channel137*/138public abstract SinkChannel sink();139140/**141* Opens a pipe.142*143* <p> The new pipe is created by invoking the {@link144* java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the145* system-wide default {@link java.nio.channels.spi.SelectorProvider}146* object. </p>147*148* @return A new pipe149*150* @throws IOException151* If an I/O error occurs152*/153public static Pipe open() throws IOException {154return SelectorProvider.provider().openPipe();155}156157}158159160