MCPcopy
hub / github.com/Terry-Mao/goim / Peek

Method Peek

pkg/bufio/bufio.go:128–150  ·  view source on GitHub ↗

Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns fewer than n bytes, it also returns an error explaining why the read is short. The error is ErrBufferFull if n is larger than b's buffer size.

(n int)

Source from the content-addressed store, hash-verified

126// also returns an error explaining why the read is short. The error is
127// ErrBufferFull if n is larger than b's buffer size.
128func (b *Reader) Peek(n int) ([]byte, error) {
129 if n < 0 {
130 return nil, ErrNegativeCount
131 }
132 if n > len(b.buf) {
133 return nil, ErrBufferFull
134 }
135 // 0 <= n <= len(b.buf)
136 for b.w-b.r < n && b.err == nil {
137 b.fill() // b.w-b.r < len(b.buf) => buffer is not full
138 }
139
140 var err error
141 if avail := b.w - b.r; avail < n {
142 // not enough data in buffer
143 n = avail
144 err = b.readErr()
145 if err == nil {
146 err = ErrBufferFull
147 }
148 }
149 return b.buf[b.r : b.r+n], err
150}
151
152// Pop returns the next n bytes with advancing the reader. The bytes stop
153// being valid at the next read call. If Pop returns fewer than n bytes, it

Callers 8

PopMethod · 0.95
TestPeekFunction · 0.45
TestReaderDiscardFunction · 0.45
WriteToMethod · 0.45
WriteTCPMethod · 0.45
WriteTCPHeartMethod · 0.45
WriteWebsocketMethod · 0.45
WriteWebsocketHeartMethod · 0.45

Calls 2

fillMethod · 0.95
readErrMethod · 0.95

Tested by 2

TestPeekFunction · 0.36
TestReaderDiscardFunction · 0.36