@cryptotaxi247 / kubo / commits / f6235c5cc

importer: simplified splitter

The splitter is simplified using io.ReadFull, as this function does exactly what we wanted. I believe io.ErrUnexpectedEOF should be handled as an EOF here, but please correct me if I'm wrong.

Juan Batiz-Benet committed Nov 18, 2014 at 23:03 UTC f6235c5cc6a0880abe602a558a98cc9cdbc3f262
1 file changed +11 -22
importer/chunk/splitting.go
+11 -22
@@ -28,28 +28,17 @@ func (ss *SizeSplitter) Split(r io.Reader) chan []byte {
28 for {
29 // log.Infof("making chunk with size: %d", ss.Size)
30 chunk := make([]byte, ss.Size)
31 - sofar := 0
32 -
33 - // this-chunk loop (keep reading until this chunk full)
34 - for {
35 - nread, err := r.Read(chunk[sofar:])
36 - sofar += nread
37 - if err == io.EOF {
38 - if sofar > 0 {
39 - // log.Infof("sending out chunk with size: %d", sofar)
40 - out <- chunk[:sofar]
41 - }
42 - return
43 - }
44 - if err != nil {
45 - log.Errorf("Block split error: %s", err)
46 - return
47 - }
48 - if sofar == ss.Size {
49 - // log.Infof("sending out chunk with size: %d", sofar)
50 - out <- chunk[:sofar]
51 - break // break out of this-chunk loop
52 - }
31 + nread, err := io.ReadFull(r, chunk)
32 + if nread > 0 {
33 + // log.Infof("sending out chunk with size: %d", sofar)
34 + out <- chunk[:nread]
35 + }
36 + if err == io.EOF || err == io.ErrUnexpectedEOF {
37 + return
38 + }
39 + if err != nil {
40 + log.Errorf("Block split error: %s", err)
41 + return
42 }
43 }
44 }()