github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/util/chanpair.go (about)

     1  // Copyright (c) 2014, Google, Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  // A ChanReadWriteCloser implements io.ReadWriteCloser over chan []byte.
    18  type ChanReadWriteCloser struct {
    19  	R <-chan []byte
    20  	W chan []byte
    21  }
    22  
    23  // Read implements io.Reader for ChanReadWriteCloser.
    24  func (crw ChanReadWriteCloser) Read(p []byte) (int, error) {
    25  	return copy(p, <-crw.R), nil
    26  }
    27  
    28  // Write implements io.Writer for ChanReadWriteCloser.
    29  func (crw ChanReadWriteCloser) Write(p []byte) (int, error) {
    30  	crw.W <- p
    31  	return len(p), nil
    32  }
    33  
    34  // Close implements io.Closer for ChanReadWriteCloser.
    35  func (crw ChanReadWriteCloser) Close() error {
    36  	close(crw.W)
    37  	return nil
    38  }