-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitting.go
47 lines (39 loc) · 927 Bytes
/
splitting.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
// package chunk implements streaming block splitters
package chunk
import (
"io"
"github.com/ipfs/go-ipfs/util"
)
var log = util.Logger("chunk")
var DefaultBlockSize = 1024 * 256
var DefaultSplitter = &SizeSplitter{Size: DefaultBlockSize}
type BlockSplitter interface {
Split(r io.Reader) chan []byte
}
type SizeSplitter struct {
Size int
}
func (ss *SizeSplitter) Split(r io.Reader) chan []byte {
out := make(chan []byte)
go func() {
defer close(out)
// all-chunks loop (keep creating chunks)
for {
// log.Infof("making chunk with size: %d", ss.Size)
chunk := make([]byte, ss.Size)
nread, err := io.ReadFull(r, chunk)
if nread > 0 {
// log.Infof("sending out chunk with size: %d", sofar)
out <- chunk[:nread]
}
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
if err != nil {
log.Debugf("Block split error: %s", err)
return
}
}
}()
return out
}