github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/requester/uploader/readed.go (about)

     1  package uploader
     2  
     3  import (
     4  	"github.com/qjfoidnh/BaiduPCS-Go/requester/rio"
     5  	"sync/atomic"
     6  )
     7  
     8  type (
     9  	// Readed64 增加获取已读取数据量, 用于统计速度
    10  	Readed64 interface {
    11  		rio.ReaderLen64
    12  		Readed() int64
    13  	}
    14  
    15  	readed64 struct {
    16  		readed int64
    17  		rio.ReaderLen64
    18  	}
    19  )
    20  
    21  // NewReaded64 实现Readed64接口
    22  func NewReaded64(rl rio.ReaderLen64) Readed64 {
    23  	return &readed64{
    24  		readed:      0,
    25  		ReaderLen64: rl,
    26  	}
    27  }
    28  
    29  func (r64 *readed64) Read(p []byte) (n int, err error) {
    30  	n, err = r64.ReaderLen64.Read(p)
    31  	atomic.AddInt64(&r64.readed, int64(n))
    32  	return n, err
    33  }
    34  
    35  func (r64 *readed64) Readed() int64 {
    36  	return atomic.LoadInt64(&r64.readed)
    37  }