// Read from network buffer
549943
func (mi *OsipsMiDatagramConnector) readDatagram() ([]byte, error) {
	var buf [65457]byte
	readBytes, _, err := mi.conn.ReadFromUDP(buf[0:])
	if err != nil {
		mi.disconnect()
		return nil, err
	}
	return buf[:readBytes], nil
}
406166
func (c *Conn) Read(_ context.Context, msg *cdproto.Message) error {
	// get websocket reader
	typ, r, err := c.conn.NextReader()
	if err != nil {
		return err
	}
	if typ != websocket.TextMessage {
		return ErrInvalidWebsocketMessage
	}

	// Unmarshal via a bytes.Buffer. Don't use UnmarshalFromReader, as that
	// uses ioutil.ReadAll, which uses a brand new bytes.Buffer each time.
	// That doesn't reuse any space.
	buf, err := c.bufReadAll(r)
	if err != nil {
		return err
	}
	if c.dbgf != nil {
		c.dbgf("<- %s", buf)
	}

	// Reuse the easyjson lexer.
	if err := unmarshal(&c.lexer, buf, msg); err != nil {
		return err
	}

	// bufReadAll uses the buffer space directly, and msg.Result is an
	// easyjson.RawMessage, so we must make a copy of those bytes to prevent
	// data races. This still allocates much less than using a new buffer
	// each time.
	msg.Result = append([]byte{}, msg.Result...)
	return nil
}
880049
func (r *Reader) Read(b []byte) (n int, err error) {
	for {
		// If there's no data in the buffer, we'll grab the next record
		// and set the internal buffer to point to the data in that
		// record. When all data from buf is read, Done() will be called
		// on the record.
		if len(r.buf) == 0 {
			select {
			case record, ok := <-r.records:
				if !ok {
					// Channel is closed, return io.EOF.
					err = io.EOF
					return
				}

				r.buf = record.Data()
				r.done = record.Done
			default:
				// By convention, Read should return rather than wait
				// for data to become available. If no data is available
				// at this time, we'll return what we've copied
				// so far.
				return
			}
		}

		n += r.copy(b[n:])
		if n == len(b) {
			return
		}
	}

	return
}
1048401
func (t *trackingConn) Read(b []byte) (int, error) {
	n, err := t.Conn.Read(b)
	t.UpdateClientActivity()
	return n, err
}
270691
func (c *conn) Read(b []byte) (int, error) {
	return c.buf.Read(b)
}
638068
func (c *managedConnImpl) Read(b []byte) (n int, err error) {
	conn, err := c.rawConn()
	if err != nil {
		return 0, err
	}

	if c.options.ReadTimeout > 0 {
		deadline := c.options.getCurrentTime().Add(c.options.ReadTimeout)
		_ = conn.SetReadDeadline(deadline)
	}
	n, err = conn.Read(b)
	if err != nil {
		var localAddr string
		if conn.LocalAddr() != nil {
			localAddr = conn.LocalAddr().String()
		} else {
			localAddr = "(nil)"
		}

		var remoteAddr string
		if conn.RemoteAddr() != nil {
			remoteAddr = conn.RemoteAddr().String()
		} else {
			remoteAddr = "(nil)"
		}
		err = errors.Wrapf(err, "Read error from host: %s <-> %s", localAddr, remoteAddr)
	}
	return
}
415804
func (c ConnWithTimeouts) Read(b []byte) (int, error) {
	if c.readTimeout == 0 {
		return c.Conn.Read(b)
	}
	if err := c.Conn.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil {
		return 0, err
	}
	return c.Conn.Read(b)
}
274739
func (c *Conn) Read(b []byte) (n int, err error) {
	// In order to reconcile the differences between the record abstraction
	// of our AEAD connection, and the stream abstraction of TCP, we
	// maintain an intermediate read buffer. If this buffer becomes
	// depleted, then we read the next record, and feed it into the
	// buffer. Otherwise, we read directly from the buffer.
	if c.readBuf.Len() == 0 {
		plaintext, err := c.noise.ReadMessage(c.conn)
		if err != nil {
			return 0, err
		}

		if _, err := c.readBuf.Write(plaintext); err != nil {
			return 0, err
		}
	}

	return c.readBuf.Read(b)
}
1291311
func (c *Conn) Read(b []byte) (n int, err error) {
	if err = c.Handshake(); err != nil {
		return
	}
	if len(b) == 0 {
		// Put this after Handshake, in case people were calling
		// Read(nil) for the side effect of the Handshake.
		return
	}

	c.in.Lock()
	defer c.in.Unlock()

	// Some OpenSSL servers send empty records in order to randomize the
	// CBC IV. So this loop ignores a limited number of empty records.
	const maxConsecutiveEmptyRecords = 100
	for emptyRecordCount := 0; emptyRecordCount <= maxConsecutiveEmptyRecords; emptyRecordCount++ {
		for c.input == nil && c.in.err == nil {
			if err := c.readRecord(recordTypeApplicationData); err != nil {
				// Soft error, like EAGAIN
				return 0, err
			}
		}
		if err := c.in.err; err != nil {
			return 0, err
		}

		n, err = c.input.Read(b)
		if c.input.off >= len(c.input.data) {
			c.in.freeBlock(c.input)
			c.input = nil
		}

		// If a close-notify alert is waiting, read it so that
		// we can return (n, EOF) instead of (n, nil), to signal
		// to the HTTP response reading goroutine that the
		// connection is now closed. This eliminates a race
		// where the HTTP response reading goroutine would
		// otherwise not observe the EOF until its next read,
		// by which time a client goroutine might have already
		// tried to reuse the HTTP connection for a new
		// request.
		// See https://codereview.appspot.com/76400046
		// and https://golang.org/issue/3514
		if ri := c.rawInput; ri != nil &&
			n != 0 && err == nil &&
			c.input == nil && len(ri.data) > 0 && recordType(ri.data[0]) == recordTypeAlert {
			if recErr := c.readRecord(recordTypeApplicationData); recErr != nil {
				err = recErr // will be io.EOF on closeNotify
			}
		}

		if n != 0 || err != nil {
			return n, err
		}
	}

	return 0, io.ErrNoProgress
}
288147
func (c *meteredConn) Read(b []byte) (n int, err error) {
	n, err = c.Conn.Read(b)
	ingressTrafficMeter.Mark(int64(n))
	c.lock.RLock()
	if c.trafficMetered {
		c.ingressMeter.Mark(int64(n))
	}
	c.lock.RUnlock()
	return n, err
}
989329
func (c *Conn) Read(b []byte) (int, error) {
	c.readMu.Lock()
	defer c.readMu.Unlock()

	deadline := c.readCancel()

	numRead := 0
	for numRead != len(b) {
		if len(c.read) == 0 {
			var err error
			c.read, err = commonRead(c.ep, c.wq, deadline, nil, c, numRead != 0)
			if err != nil {
				if numRead != 0 {
					return numRead, nil
				}
				return numRead, err
			}
		}
		n := copy(b[numRead:], c.read)
		c.read.TrimFront(n)
		numRead += n
		if len(c.read) == 0 {
			c.read = nil
		}
	}
	return numRead, nil
}
696318
func (s *UDPSession) Read(b []byte) (n int, err error) {
	for {
		s.mu.Lock()
		if len(s.bufptr) > 0 { // copy from buffer into b
			n = copy(b, s.bufptr)
			s.bufptr = s.bufptr[n:]
			s.mu.Unlock()
			atomic.AddUint64(&DefaultSnmp.BytesReceived, uint64(n))
			return n, nil
		}

		if s.isClosed {
			s.mu.Unlock()
			return 0, errors.New(errBrokenPipe)
		}

		if size := s.kcp.PeekSize(); size > 0 { // peek data size from kcp
			if len(b) >= size { // receive data into 'b' directly
				s.kcp.Recv(b)
				s.mu.Unlock()
				atomic.AddUint64(&DefaultSnmp.BytesReceived, uint64(size))
				return size, nil
			}

			// if necessary resize the stream buffer to guarantee a sufficent buffer space
			if cap(s.recvbuf) < size {
				s.recvbuf = make([]byte, size)
			}

			// resize the length of recvbuf to correspond to data size
			s.recvbuf = s.recvbuf[:size]
			s.kcp.Recv(s.recvbuf)
			n = copy(b, s.recvbuf)   // copy to 'b'
			s.bufptr = s.recvbuf[n:] // pointer update
			s.mu.Unlock()
			atomic.AddUint64(&DefaultSnmp.BytesReceived, uint64(n))
			return n, nil
		}

		// deadline for current reading operation
		var timeout *time.Timer
		var c <-chan time.Time
		if !s.rd.IsZero() {
			if time.Now().After(s.rd) {
				s.mu.Unlock()
				return 0, errTimeout{}
			}

			delay := s.rd.Sub(time.Now())
			timeout = time.NewTimer(delay)
			c = timeout.C
		}
		s.mu.Unlock()

		// wait for read event or timeout
		select {
		case <-s.chReadEvent:
		case <-c:
		case <-s.die:
		case err = <-s.chReadError:
			if timeout != nil {
				timeout.Stop()
			}
			return n, err
		}

		if timeout != nil {
			timeout.Stop()
		}
	}
}
568043
func (d *Decoder) Read() uint64 {
	v := d.buf[d.i]
	return v
}
1202221
func (c *Conn) Read(p []byte) (int, error) {
	i, err := c.NetConn.Read(p)
	return i, err
}
803226
func (b *Buffer) read() []*rfc5424.Message {
	if b.length == -1 {
		return nil
	}

	buf := make([]*rfc5424.Message, 0, b.length)
	msg := b.head
	for msg != nil {
		buf = append(buf, &msg.Message)
		msg = msg.next
	}
	return buf
}
501459
func (r *Reader) Read(b []byte) (int, error) {
	// if we have data in the buffer, just
	// return that.
	if r.buffered() != 0 {
		x := copy(b, r.data[r.n:])
		r.n += x
		return x, nil
	}
	var n int
	// we have no buffered data; determine
	// whether or not to buffer or call
	// the underlying reader directly
	if len(b) >= cap(r.data) {
		n, r.state = r.r.Read(b)
	} else {
		r.more()
		n = copy(b, r.data)
		r.n = n
	}
	if n == 0 {
		return 0, r.err()
	}
	return n, nil
}
727321
func (l *tomlLexer) read() rune {
	r := l.peek()
	if r == '\n' {
		l.endbufferLine++
		l.endbufferCol = 1
	} else {
		l.endbufferCol++
	}
	l.inputIdx++
	return r
}