From a8645afbcf682e2990ee2f3eaedb6a758f7791e4 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 8 Mar 2018 23:16:31 +0100 Subject: [PATCH] pbreader: use ReadFull instead of reimplementing it License: MIT Signed-off-by: Jakub Sztandera --- unixfs/io/pbdagreader.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/unixfs/io/pbdagreader.go b/unixfs/io/pbdagreader.go index e5fbaa928e0..5be60bd8e14 100644 --- a/unixfs/io/pbdagreader.go +++ b/unixfs/io/pbdagreader.go @@ -166,27 +166,20 @@ func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) { total := 0 for { // Attempt to fill bytes from cached buffer - n, err := dr.buf.Read(b[total:]) + n, err := io.ReadFull(dr.buf, b[total:]) total += n dr.offset += int64(n) - if err != nil { - // EOF is expected - if err != io.EOF { - return total, err - } - } - - // If weve read enough bytes, return - if total == len(b) { + switch err { + // io.EOF will happen is dr.buf had noting more to read (n == 0) + case io.EOF, io.ErrUnexpectedEOF: + // do nothing + case nil: return total, nil + default: + return total, err } - // We haven't hit the end yet. - if err != io.EOF { - continue - } - - // Otherwise, load up the next block + // if we are not done with the output buffer load next block err = dr.precalcNextBuf(ctx) if err != nil { return total, err