1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-02 20:32:58 +08:00

pbreader: use ReadFull instead of reimplementing it

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera
2018-03-08 23:16:31 +01:00
parent 904be7c94d
commit a8645afbcf

View File

@ -166,27 +166,20 @@ func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
total := 0 total := 0
for { for {
// Attempt to fill bytes from cached buffer // Attempt to fill bytes from cached buffer
n, err := dr.buf.Read(b[total:]) n, err := io.ReadFull(dr.buf, b[total:])
total += n total += n
dr.offset += int64(n) dr.offset += int64(n)
if err != nil { switch err {
// EOF is expected // io.EOF will happen is dr.buf had noting more to read (n == 0)
if err != io.EOF { case io.EOF, io.ErrUnexpectedEOF:
// do nothing
case nil:
return total, nil
default:
return total, err return total, err
} }
}
// If weve read enough bytes, return // if we are not done with the output buffer load next block
if total == len(b) {
return total, nil
}
// We haven't hit the end yet.
if err != io.EOF {
continue
}
// Otherwise, load up the next block
err = dr.precalcNextBuf(ctx) err = dr.precalcNextBuf(ctx)
if err != nil { if err != nil {
return total, err return total, err