github.com/yandex/pandora@v0.5.32/lib/ioutil2/reader.go (about)

     1  package ioutil2
     2  
     3  import "io"
     4  
     5  type ReaderWrapper interface {
     6  	Unwrap() io.Reader
     7  }
     8  
     9  // TODO(skipor): test
    10  
    11  func NewMultiPassReader(r io.Reader, passes int) io.Reader {
    12  	if passes == 1 {
    13  		return r
    14  	}
    15  	rs, isSeakable := r.(io.ReadSeeker)
    16  	if !isSeakable {
    17  		return r
    18  	}
    19  	return &MultiPassReader{rs: rs, passesLimit: passes}
    20  }
    21  
    22  type MultiPassReader struct {
    23  	rs          io.ReadSeeker
    24  	passesCount int
    25  	passesLimit int
    26  }
    27  
    28  func (r *MultiPassReader) Read(p []byte) (n int, err error) {
    29  	n, err = r.rs.Read(p)
    30  	if err == io.EOF {
    31  		r.passesCount++
    32  		if r.passesLimit <= 0 || r.passesCount < r.passesLimit {
    33  			_, err = r.rs.Seek(0, io.SeekStart)
    34  		}
    35  	}
    36  	return
    37  }
    38  
    39  // func (r *MultiPassReader) PassesLeft() int {
    40  //	return r.PassesLeft()
    41  // }
    42  
    43  func (r *MultiPassReader) Unwrap() io.Reader {
    44  	return r.rs
    45  }