MCPcopy
hub / github.com/grpc/grpc-go / Read

Method Read

internal/transport/readyreader/ready_reader.go:199–243  ·  view source on GitHub ↗

Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying [ReadyReader], hence n may be less than len(p). If the underlying [ReadyReader] can return a non-zero count with io.EOF, then this Read method can do so as well; see the [i

(p []byte)

Source from the content-addressed store, hash-verified

197// a non-zero count with io.EOF, then this Read method can do so as well; see
198// the [io.Reader] docs.
199func (b *bufReadyReader) Read(p []byte) (n int, err error) {
200 n = len(p)
201 if n == 0 {
202 if b.buffered() > 0 {
203 return 0, nil
204 }
205 return 0, b.readErr()
206 }
207 if b.r == b.w {
208 if b.err != nil {
209 return 0, b.readErr()
210 }
211 if len(p) >= b.bufSize {
212 // Large read, empty buffer.
213 // Read directly into p to avoid copy.
214 b.constPool.buffer = p
215 _, n, b.err = b.rd.ReadOnReady(len(p), &b.constPool)
216 return n, b.readErr()
217 }
218 // One read.
219 b.r = 0
220 b.w = 0
221 b.buf, n, b.err = b.rd.ReadOnReady(b.bufSize, b.pool)
222 if n == 0 {
223 if b.buf != nil {
224 b.pool.Put(b.buf)
225 b.buf = nil
226 }
227 return 0, b.readErr()
228 }
229 b.w += n
230 }
231
232 // copy as much as we can
233 // b.buf must be non-nil since b.r != b.w.
234 buf := *b.buf
235 n = copy(p, buf[b.r:b.w])
236 b.r += n
237 if b.r == b.w {
238 // Consumed entire buffer, release it.
239 b.pool.Put(b.buf)
240 b.buf = nil
241 }
242 return n, nil
243}
244
245type constBufferPool struct {
246 buffer []byte

Callers

nothing calls this directly

Calls 4

bufferedMethod · 0.95
readErrMethod · 0.95
ReadOnReadyMethod · 0.65
PutMethod · 0.65

Tested by

no test coverage detected