提交 a8645afb 作者: Jakub Sztandera

pbreader: use ReadFull instead of reimplementing it

License: MIT
Signed-off-by: 's avatarJakub Sztandera <kubuxu@protonmail.ch>
上级 904be7c9
...@@ -166,27 +166,20 @@ func (dr *PBDagReader) CtxReadFull(ctx context.Context, b []byte) (int, error) { ...@@ -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:
return total, err // do nothing
} case nil:
}
// If weve read enough bytes, return
if total == len(b) {
return total, nil return total, nil
default:
return total, err
} }
// We haven't hit the end yet. // if we are not done with the output buffer load next block
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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论