forked from 1290799223/trafficConsume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakefile.go
61 lines (50 loc) · 1.03 KB
/
fakefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fakefile
import (
"crypto/sha1"
"io"
"github.com/anacrolix/torrent/metainfo"
)
type FakeFile struct {
Size int
FillByte byte
}
func (f *FakeFile) BuildFakePieces(pieceLen int64) []byte {
var cumB []byte
cycleNum := int64(f.Size) / pieceLen
r := int64(f.Size) % pieceLen
p := make([]byte, pieceLen)
for i := range p {
p[i] = f.FillByte
}
b := sha1.Sum(p)
rb := sha1.Sum(p[:r])
for i := int64(0); i < cycleNum; i++ {
cumB = append(cumB, b[:]...)
}
cumB = append(cumB, rb[:]...)
return cumB
}
func (f *FakeFile) Read(p []byte) (n int, err error) {
readSize := len(p)
r := f.Size
if r < readSize {
readSize = r
}
for i := 0; i < readSize; i++ {
p[i] = f.FillByte
}
r -= readSize
if r <= 0 {
err = io.EOF
}
return readSize, err
}
func (f *FakeFile) BuildFakeFileInfo() *metainfo.Info {
info := &metainfo.Info{
Name: "fake.file",
Length: int64(f.Size),
PieceLength: metainfo.ChoosePieceLength(int64(f.Size)),
}
info.Pieces = f.BuildFakePieces(info.PieceLength)
return info
}