1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:24:42 -07:00

image/gif: simplify blockReader.Read.

Inverting the tests avoids recursion and simplifies the flow.

R=nigeltao
CC=golang-dev
https://golang.org/cl/4551057
This commit is contained in:
Rob Pike 2011-05-24 11:02:44 +10:00
parent f5a011dd0c
commit 4c945c2cfc

View File

@ -94,28 +94,26 @@ type blockReader struct {
tmp [256]byte tmp [256]byte
} }
func (b *blockReader) Read(p []byte) (n int, err os.Error) { func (b *blockReader) Read(p []byte) (int, os.Error) {
if len(p) == 0 { if len(p) == 0 {
return return 0, nil
} }
if len(b.slice) > 0 { if len(b.slice) == 0 {
n = copy(p, b.slice) blockLen, err := b.r.ReadByte()
b.slice = b.slice[n:] if err != nil {
return return 0, err
}
if blockLen == 0 {
return 0, os.EOF
}
b.slice = b.tmp[0:blockLen]
if _, err = io.ReadFull(b.r, b.slice); err != nil {
return 0, err
}
} }
var blockLen uint8 n := copy(p, b.slice)
blockLen, err = b.r.ReadByte() b.slice = b.slice[n:]
if err != nil { return n, nil
return
}
if blockLen == 0 {
return 0, os.EOF
}
b.slice = b.tmp[0:blockLen]
if _, err = io.ReadFull(b.r, b.slice); err != nil {
return
}
return b.Read(p)
} }
// decode reads a GIF image from r and stores the result in d. // decode reads a GIF image from r and stores the result in d.