// reads a protocol one byte at a time.
333574
func (nc *Conn) readProto() (string, error) {
	var (
		_buf     = [10]byte{}
		buf      = _buf[:0]
		b        = [1]byte{}
		protoEnd = byte('\n')
	)
	for {
		if _, err := nc.conn.Read(b[:1]); err != nil {
			// Do not report EOF error
			if err == io.EOF {
				return string(buf), nil
			}
			return "", err
		}
		buf = append(buf, b[0])
		if b[0] == protoEnd {
			return string(buf), nil
		}
	}
}
324164
func (p *TCompactProtocol) ReadByte() (int8, error) {
	v, err := p.readByteDirect()
	if err != nil {
		return 0, NewTProtocolException(err)
	}
	return int8(v), err
}
924380
func (spec *ConnectionSpec) Protocol(protocol Protocol) *ConnectionSpec {
	spec.protocol = protocol
	return spec
}
327238
func (s *Stream) Read(p []byte) (int, error) {
	n, err := s.stream.Read(p)
	// TODO: push this down to a lower level for better accuracy.
	if s.conn.swarm.bwc != nil {
		s.conn.swarm.bwc.LogRecvMessage(int64(n))
		s.conn.swarm.bwc.LogRecvMessageStream(int64(n), s.Protocol(), s.Conn().RemotePeer())
	}
	// If we observe an EOF, this stream is now closed for reading.
	// If we're already closed for writing, this stream is now fully closed.
	if err == io.EOF {
		s.state.Lock()
		switch s.state.v {
		case streamCloseWrite:
			s.state.v = streamCloseBoth
			s.remove()
		case streamOpen:
			s.state.v = streamCloseRead
		}
		s.state.Unlock()
	}
	return n, err
}
330395
func (b *retryableRequestBody) Read(p []byte) (n int, err error) {
	return b.body.Read(p)
}
547701
func (w *WebSockWrapper) Read(out []byte) (n int, err error) {
	var data []byte

	if w.mode == WebSocketBinaryMode {
		err = websocket.Message.Receive(w.ws, &data)
	} else {
		var utf8 string
		err = websocket.Message.Receive(w.ws, &utf8)
		switch err {
		case nil:
			data, err = w.decoder.Bytes([]byte(utf8))
		case io.EOF:
			return 0, io.EOF
		default:
			log.Error(err)
		}
	}
	if err != nil {
		return 0, trace.Wrap(err)
	}
	if len(out) < len(data) {
		log.Warningf("websocket failed to receive everything: %d vs %d", len(out), len(data))
	}
	return copy(out, data), nil
}
148400
func (c *CountingReader) ReadByte() (byte, error) {
	// If our underlying reader is a ByteReader, use its ReadByte directly.
	if br, ok := c.Reader.(io.ByteReader); ok {
		b, err := br.ReadByte()
		if err == nil {
			c.Count++
		}
		return b, err
	}

	data := []byte{0}
	amount, err := c.Reader.Read(data)
	if amount != 0 {
		c.Count += int64(amount)
		return data[0], err
	}
	return 0, err
}
1307645
func (s *Slice) Read(p []byte) (n int, err error) {
	if len(p) == 0 {
		return 0, nil
	}

	if len(*s) == 0 {
		return 0, io.EOF
	}
	n = copy(p, *s)

	if n == len(*s) {
		*s = nil
	} else {
		*s = (*s)[n:]
	}

	return n, nil
}
76176
func (c *Conn) readPacket() ([]byte, error) {
	// Optimize for a single packet case.
	data, err := c.readOnePacket()
	if err != nil {
		return nil, err
	}

	// This is a single packet.
	if len(data) < MaxPacketSize {
		return data, nil
	}

	// There is more than one packet, read them all.
	for {
		next, err := c.readOnePacket()
		if err != nil {
			return nil, err
		}

		if len(next) == 0 {
			// Again, the packet after a packet of exactly size MaxPacketSize.
			break
		}

		data = append(data, next...)
		if len(next) < MaxPacketSize {
			break
		}
	}

	return data, nil
}
442886
func (st *state) Read(p []byte) (n int, err error) {
	if st.br == nil {
		var s []byte
		if s, err = prep(st.r); err != nil {
			return
		}
		st.br = bytes.NewReader(s)
	}
	return st.br.Read(p)
}
647004
func (b *ByteBuffer) ReadBytes(delim byte) ([]byte, error) {
	b.Lock()
	defer b.Unlock()
	return b.b.ReadBytes(delim)
}
1230360
func (r *BodyWithCallback) Read(p []byte) (int, error) {
	n, err := r.ReadSeekCloser.Read(p)

	if n > 0 {
		r.readSize += int64(n)

		if (err == nil || err == io.EOF) && r.c != nil {
			err = r.c(r.totalSize, r.readSize, n)
		}
	}
	return n, err
}
1220868
func (c *I2PConn) Read(d []byte) (n int, err error) {
	n, err = c.c.Read(d)
	return
}
680037
func (p *conn) Read(b []byte) (int, error) {
	var err error
	p.initOnce.Do(func() { err = p.checkPrefix() })
	if err != nil {
		return 0, err
	}
	if p.connBuf != nil {
		n := copy(b, p.connBuf)
		p.connBuf = p.connBuf[n:]
		if len(p.connBuf) == 0 {
			p.connBuf = nil
		}
		if len(b) == n {
			return n, nil
		}
		readN, err := p.Conn.Read(b[n:])
		return readN + n, err
	}
	return p.Conn.Read(b)
}
167076
func (r *eofSignal) Read(buf []byte) (int, error) {
	if r.count == 0 {
		if r.eof != nil {
			r.eof <- struct{}{}
			r.eof = nil
		}
		return 0, io.EOF
	}

	max := len(buf)
	if int(r.count) < len(buf) {
		max = int(r.count)
	}
	n, err := r.wrapped.Read(buf[:max])
	r.count -= uint32(n)
	if (err != nil || r.count == 0) && r.eof != nil {
		r.eof <- struct{}{} // tell Peer that msg has been consumed
		r.eof = nil
	}
	return n, err
}
1274529
func (r *unsynchroniser) Read(p []byte) (int, error) {
	b := make([]byte, 1)
	i := 0
	for i < len(p) {
		if n, err := r.Reader.Read(b); err != nil || n == 0 {
			return i, err
		}
		if r.ff && b[0] == 0x00 {
			r.ff = false
			continue
		}
		p[i] = b[0]
		i++
		r.ff = (b[0] == 0xFF)
	}
	return i, nil
}
1001392
func (r *reader) readByte() byte {
	// Fast path if we're byte aligned
	if r.bitCount == 0 {
		return r.nextByte()
	}

	return byte(r.readBits(8))
}