github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/reader_utils.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package utils
     4  
     5  import "io"
     6  
     7  func CopyWithFilter(writer io.Writer, reader io.Reader, buf []byte, filter func(p []byte) []byte) (written int64, err error) {
     8  	for {
     9  		n, err := reader.Read(buf)
    10  		if n > 0 {
    11  			n2, err := writer.Write(filter(buf[:n]))
    12  			written += int64(n2)
    13  			if err != nil {
    14  				return written, err
    15  			}
    16  		}
    17  		if err != nil {
    18  			if err == io.EOF {
    19  				break
    20  			}
    21  			return written, err
    22  		}
    23  	}
    24  	return written, nil
    25  }