github.com/puellanivis/breton@v0.2.16/lib/files/fuzzy_limited_reader.go (about)

     1  package files
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // fuzzyLimitedReader reads at least N-bytes from the underlying reader.
     8  // It does not ensure that it reads only or at-most N-bytes.
     9  // Each call to Read updates N to reflect the new amount remaining.
    10  // Read returns EOF when N <= 0 or when the underlying R returns EOF.
    11  type fuzzyLimitedReader struct {
    12  	R io.Reader // underlying reader
    13  	N int64     // stop reading after at least this much
    14  }
    15  
    16  func (r *fuzzyLimitedReader) Read(b []byte) (n int, err error) {
    17  	if r.N <= 0 {
    18  		return 0, io.EOF
    19  	}
    20  
    21  	n, err = r.R.Read(b)
    22  	r.N -= int64(n)
    23  
    24  	return n, err
    25  }