mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-09 10:18:04 +08:00
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:
parent
706ebe8d24
commit
f6235c5cc6
@ -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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user