Home | History | Annotate | Download | only in channels
      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 
     17 package java.nio.channels;
     18 
     19 import java.io.IOException;
     20 import java.nio.channels.spi.AbstractSelectableChannel;
     21 import java.nio.channels.spi.SelectorProvider;
     22 
     23 /**
     24  * A pipe contains two channels, forming a unidirectional pipe. One is the writable sink channel,
     25  * and the other is the readable source channel. When bytes are written into the writable
     26  * channel they can be read from the readable channel. Bytes are read in the order in which they
     27  * were written.
     28  */
     29 public abstract class Pipe {
     30     /**
     31      * Writable sink channel used to write to a pipe.
     32      */
     33     public static abstract class SinkChannel extends AbstractSelectableChannel
     34             implements WritableByteChannel, GatheringByteChannel {
     35         /**
     36          * Constructs a new {@code SinkChannel}.
     37          *
     38          * @param provider
     39          *            the provider of the channel.
     40          */
     41         protected SinkChannel(SelectorProvider provider) {
     42             super(provider);
     43         }
     44 
     45         /**
     46          * Indicates that this channel only supports writing.
     47          *
     48          * @return a static value of OP_WRITE.
     49          */
     50         @Override
     51         public final int validOps() {
     52             return SelectionKey.OP_WRITE;
     53         }
     54     }
     55 
     56     /**
     57      * Readable source channel used to read from a pipe.
     58      */
     59     public static abstract class SourceChannel extends
     60             AbstractSelectableChannel implements ReadableByteChannel, ScatteringByteChannel {
     61         /**
     62          * Constructs a new {@code SourceChannel}.
     63          *
     64          * @param provider
     65          *            the provider of the channel.
     66          */
     67         protected SourceChannel(SelectorProvider provider) {
     68             super(provider);
     69         }
     70 
     71         /**
     72          * Indicates that this channel only supports reading.
     73          *
     74          * @return a static value of OP_READ.
     75          */
     76         @Override
     77         public final int validOps() {
     78             return SelectionKey.OP_READ;
     79         }
     80     }
     81 
     82     /**
     83      * Returns a new pipe from the default {@see java.nio.channels.spi.SelectorProvider}.
     84      *
     85      * @throws IOException
     86      *             if an I/O error occurs.
     87      */
     88     public static Pipe open() throws IOException {
     89         return SelectorProvider.provider().openPipe();
     90     }
     91 
     92     /**
     93      * The protected default constructor.
     94      */
     95     protected Pipe() {
     96         super();
     97     }
     98 
     99     /**
    100      * Returns the sink channel of the pipe.
    101      *
    102      * @return a writable sink channel of the pipe.
    103      */
    104     public abstract SinkChannel sink();
    105 
    106     /**
    107      * Returns the source channel of the pipe.
    108      *
    109      * @return a readable source channel of the pipe.
    110      */
    111     public abstract SourceChannel source();
    112 }
    113