1 Star 0 Fork 0

J-star/BaiduPCS-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
block.go 2.30 KB
一键复制 编辑 原始数据 按行查看 历史
konica 提交于 2018-08-04 22:27 . 更新错误处理, 增加搜索文件
package uploader
import (
"fmt"
"io"
"os"
"sync"
)
type (
// SplitUnit 将 io.ReaderAt 分割单元
SplitUnit interface {
Readed64
io.Seeker
Range() ReadRange
Left() int64
}
// ReadRange 读取io.ReaderAt范围
ReadRange struct {
Begin int64 `json:"begin"`
End int64 `json:"end"`
}
fileBlock struct {
readRange ReadRange
readed int64
readerAt io.ReaderAt
mu sync.Mutex
}
)
// SplitBlock 文件分块
func SplitBlock(fileSize, blockSize int64) (blockList []*BlockState) {
blocksNum := int(fileSize / blockSize)
if fileSize%blockSize != 0 {
blocksNum += 1
}
blockList = make([]*BlockState, 0, blocksNum)
var (
id int
begin, end int64
)
for i := 0; i < blocksNum-1; i++ {
end += blockSize
blockList = append(blockList, &BlockState{
ID: id,
Range: ReadRange{
Begin: begin,
End: end,
},
})
id++
begin = end
}
blockList = append(blockList, &BlockState{
ID: id,
Range: ReadRange{
Begin: begin,
End: fileSize,
},
})
return
}
// NewSplitUnit io.ReaderAt实现SplitUnit接口
func NewSplitUnit(readerAt io.ReaderAt, readRange ReadRange) SplitUnit {
return &fileBlock{
readerAt: readerAt,
readRange: readRange,
}
}
func (fb *fileBlock) Read(b []byte) (n int, err error) {
fb.mu.Lock()
defer fb.mu.Unlock()
if fb.readed+fb.readRange.Begin >= fb.readRange.End {
return 0, io.EOF
}
left := int(fb.Left())
if len(b) > left {
n, err = fb.readerAt.ReadAt(b[:left], fb.readed+fb.readRange.Begin)
} else {
n, err = fb.readerAt.ReadAt(b, fb.readed+fb.readRange.Begin)
}
fb.readed += int64(n)
return
}
func (fb *fileBlock) Seek(offset int64, whence int) (int64, error) {
fb.mu.Lock()
defer fb.mu.Unlock()
switch whence {
case os.SEEK_SET:
fb.readed = offset
case os.SEEK_CUR:
fb.readed += offset
case os.SEEK_END:
fb.readed = fb.readRange.End - fb.readRange.Begin + offset
default:
return 0, fmt.Errorf("unsupport whence: %d", whence)
}
if fb.readed < 0 {
fb.readed = 0
}
return fb.readed, nil
}
func (fb *fileBlock) Len() int64 {
return fb.readRange.End - fb.readRange.Begin
}
func (fb *fileBlock) Left() int64 {
return fb.readRange.End - fb.readRange.Begin - fb.readed
}
func (fb *fileBlock) Range() ReadRange {
return fb.readRange
}
func (fb *fileBlock) Readed() int64 {
return fb.readed
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/j-star/BaiduPCS-Go.git
git@gitee.com:j-star/BaiduPCS-Go.git
j-star
BaiduPCS-Go
BaiduPCS-Go
v3.5.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385