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.
This commit is contained in:
Juan Batiz-Benet 2014-11-18 23:03:58 -08:00 committed by Brian Tiger Chow
parent 706ebe8d24
commit f6235c5cc6

View File

@ -28,28 +28,17 @@ func (ss *SizeSplitter) Split(r io.Reader) chan []byte {
for {
// log.Infof("making chunk with size: %d", ss.Size)
chunk := make([]byte, ss.Size)
sofar := 0
// this-chunk loop (keep reading until this chunk full)
for {
nread, err := r.Read(chunk[sofar:])
sofar += nread
if err == io.EOF {
if sofar > 0 {
// log.Infof("sending out chunk with size: %d", sofar)
out <- chunk[:sofar]
}
return
}
if err != nil {
log.Errorf("Block split error: %s", err)
return
}
if sofar == ss.Size {
// log.Infof("sending out chunk with size: %d", sofar)
out <- chunk[:sofar]
break // break out of this-chunk loop
}
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.Errorf("Block split error: %s", err)
return
}
}
}()