github.com/anacrolix/torrent@v1.61.0/bencode/scanner.go (about) 1 package bencode 2 3 import ( 4 "errors" 5 "io" 6 ) 7 8 // Implements io.ByteScanner over io.Reader, for use in Decoder, to ensure 9 // that as little as the undecoded input Reader is consumed as possible. 10 type scanner struct { 11 r io.Reader 12 b [1]byte // Buffer for ReadByte 13 unread bool // True if b has been unread, and so should be returned next 14 } 15 16 func (me *scanner) Read(b []byte) (int, error) { 17 return me.r.Read(b) 18 } 19 20 func (me *scanner) ReadByte() (byte, error) { 21 if me.unread { 22 me.unread = false 23 return me.b[0], nil 24 } 25 for { 26 n, err := me.r.Read(me.b[:]) 27 switch n { 28 case 0: 29 // io.Reader.Read says to try again if there's no error and no bytes returned. We can't 30 // signal that the caller should do this method's interface. 31 if err != nil { 32 return 0, err 33 } 34 panic(err) 35 case 1: 36 // There's no way to signal that the byte is valid unless error is nil. 37 return me.b[0], nil 38 default: 39 if err != nil { 40 // I can't see why Read would return more bytes than expected, but the error should 41 // tell us why. 42 panic(err) 43 } 44 panic(n) 45 } 46 } 47 } 48 49 func (me *scanner) UnreadByte() error { 50 if me.unread { 51 return errors.New("byte already unread") 52 } 53 me.unread = true 54 return nil 55 }