kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/delimited/copy.go (about) 1 /* 2 * Copyright 2016 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 delimited 18 19 import ( 20 "fmt" 21 "io" 22 ) 23 24 // A Source represents a sequence of records. 25 type Source interface { 26 // Next returns the next record in the sequence, or io.EOF when no further 27 // records are available. The slice returned by Next is only required to be 28 // valid until a subsequent call to Next. 29 Next() ([]byte, error) 30 } 31 32 // A Sink represents a receiver of records. 33 type Sink interface { 34 // Put delivers a record to the sink. 35 Put([]byte) error 36 } 37 38 // Copy sequentially copies each record read from src to sink until src.Next() 39 // returns io.EOF or another error occurs. 40 func Copy(sink Sink, src Source) error { 41 for { 42 record, err := src.Next() 43 if err == io.EOF { 44 return nil 45 } else if err != nil { 46 return fmt.Errorf("copy: read error: %v", err) 47 } 48 if err := sink.Put(record); err != nil { 49 return fmt.Errorf("copy: write error: %v", err) 50 } 51 } 52 }