Skip to content

Commit a8645af

Browse files
committed
pbreader: use ReadFull instead of reimplementing it
License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 904be7c commit a8645af

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

unixfs/io/pbdagreader.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,20 @@ func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
166166
total := 0
167167
for {
168168
// Attempt to fill bytes from cached buffer
169-
n, err := dr.buf.Read(b[total:])
169+
n, err := io.ReadFull(dr.buf, b[total:])
170170
total += n
171171
dr.offset += int64(n)
172-
if err != nil {
173-
// EOF is expected
174-
if err != io.EOF {
175-
return total, err
176-
}
177-
}
178-
179-
// If weve read enough bytes, return
180-
if total == len(b) {
172+
switch err {
173+
// io.EOF will happen is dr.buf had noting more to read (n == 0)
174+
case io.EOF, io.ErrUnexpectedEOF:
175+
// do nothing
176+
case nil:
181177
return total, nil
178+
default:
179+
return total, err
182180
}
183181

184-
// We haven't hit the end yet.
185-
if err != io.EOF {
186-
continue
187-
}
188-
189-
// Otherwise, load up the next block
182+
// if we are not done with the output buffer load next block
190183
err = dr.precalcNextBuf(ctx)
191184
if err != nil {
192185
return total, err

0 commit comments

Comments
 (0)