@cryptotaxi247 / kubo / commits / a8645afbc

pbreader: use ReadFull instead of reimplementing it

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Mar 8, 2018 at 23:16 UTC a8645afbcf682e2990ee2f3eaedb6a758f7791e4
1 file changed +9 -16
unixfs/io/pbdagreader.go
+9 -16
@@ -166,27 +166,20 @@ func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) {
166 total := 0
167 for {
168 // Attempt to fill bytes from cached buffer
169 - n, err := dr.buf.Read(b[total:])
169 + n, err := io.ReadFull(dr.buf, b[total:])
170 total += n
171 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:
177 return total, nil
178 + default:
179 + return total, err
180 }
181
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
183 err = dr.precalcNextBuf(ctx)
184 if err != nil {
185 return total, err