kubo/importer/chunk/splitting.go
Juan Batiz-Benet 184c25430b go-vet friendly codebase
- distinguish log.Error and log.Errorf functions
- Initialize structs with field names
- A bit of unreachable code (defers)
2014-10-25 03:46:39 -07:00

46 lines
718 B
Go

package chunk
import (
"io"
"github.com/jbenet/go-ipfs/util"
)
var log = util.Logger("chunk")
var DefaultSplitter = &SizeSplitter{Size: 1024 * 512}
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)
for {
chunk := make([]byte, ss.Size)
nread, err := r.Read(chunk)
if err != nil {
if err == io.EOF {
if nread > 0 {
out <- chunk[:nread]
}
return
}
log.Errorf("Block split error: %s", err)
return
}
if nread < ss.Size {
chunk = chunk[:nread]
}
out <- chunk
}
}()
return out
}