// Read implements io.Reader,
// n == len(p) if err == nil, otherwise n contains the number bytes read before an error occured
504247
func (e *EncodeSession) Read(p []byte) (n int, err error) {
	if e.buf.Len() >= len(p) {
		return e.buf.Read(p)
	}

	for e.buf.Len() < len(p) {
		f, err := e.ReadFrame()
		if err != nil {
			break
		}
		e.buf.Write(f)
	}

	return e.buf.Read(p)
}
589625
func (s *Stream) Read(p []byte) (n int, err error) {
	// Don't request a read if there was an error earlier
	if er := s.trReader.(*transportReader).er; er != nil {
		return 0, er
	}
	s.requestRead(len(p))
	return io.ReadFull(s.trReader, p)
}
851518
func (r *ReaderCloser) Read(p []byte) (int, error) {
	count, err := unix.Read(r.fd, p)
	if count < 0 && err != nil {
		count = 0
	}
	return count, err
}
914738
func (b *ByteBuf) Read(p []byte) (n int, err error) {
	if len(p) == 0 {
		return 0, nil
	}

	size := len(p)
	if len(p) > b.Readable() {
		size = b.Readable()
	}

	n = copy(p, b.buf[b.readerIndex:b.readerIndex+size])
	b.readerIndex += n
	return n, nil
}
550085
func (r Reader) Read(data []byte) (n int, err error) {
	if r.reader == nil {
		return os.Stdin.Read(data)
	}
	return r.reader.Read(data)
}
47188
func (b *MemFile) Read(p []byte) (n int, err error) {
	b.mutex.RLock()
	n, err = b.Buffer.Read(p)
	b.mutex.RUnlock()
	return
}
1135189
func (r *Reader) Read(p []byte) (int, error) {
	if r.err != nil {
		return 0, r.err
	}
	for {
		if r.i < r.j {
			n := copy(p, r.decoded[r.i:r.j])
			r.i += n
			return n, nil
		}
		if !r.readFull(r.buf[:4]) {
			return 0, r.err
		}
		chunkType := r.buf[0]
		if !r.readHeader {
			if chunkType != chunkTypeStreamIdentifier {
				r.err = ErrCorrupt
				return 0, r.err
			}
			r.readHeader = true
		}
		chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
		if chunkLen > len(r.buf) {
			r.err = ErrUnsupported
			return 0, r.err
		}

		// The chunk types are specified at
		// https://code.google.com/p/snappy/source/browse/trunk/framing_format.txt
		switch chunkType {
		case chunkTypeCompressedData:
			// Section 4.2. Compressed data (chunk type 0x00).
			/*
				if chunkLen < checksumSize {
					r.err = ErrCorrupt
					return 0, r.err
				}
			*/
			buf := r.buf[:chunkLen]
			if !r.readFull(buf) {
				return 0, r.err
			}
			/*
				checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
				buf = buf[checksumSize:]
			*/
			n, err := DecodedLen(buf)
			if err != nil {
				r.err = err
				return 0, r.err
			}
			if n > len(r.decoded) {
				r.err = ErrCorrupt
				return 0, r.err
			}
			if _, err := Decode(r.decoded, buf); err != nil {
				r.err = err
				return 0, r.err
			}
			/*
				if crc(r.decoded[:n]) != checksum {
					r.err = ErrCorrupt
					return 0, r.err
				}
			*/
			r.i, r.j = 0, n
			continue

		case chunkTypeUncompressedData:
			// Section 4.3. Uncompressed data (chunk type 0x01).
			if chunkLen < checksumSize {
				r.err = ErrCorrupt
				return 0, r.err
			}
			buf := r.buf[:checksumSize]
			if !r.readFull(buf) {
				return 0, r.err
			}
			checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
			// Read directly into r.decoded instead of via r.buf.
			n := chunkLen - checksumSize
			if !r.readFull(r.decoded[:n]) {
				return 0, r.err
			}
			if crc(r.decoded[:n]) != checksum {
				r.err = ErrCorrupt
				return 0, r.err
			}
			r.i, r.j = 0, n
			continue

		case chunkTypeStreamIdentifier:
			// Section 4.1. Stream identifier (chunk type 0xff).
			if chunkLen != len(magicBody) {
				r.err = ErrCorrupt
				return 0, r.err
			}
			if !r.readFull(r.buf[:len(magicBody)]) {
				return 0, r.err
			}
			for i := 0; i < len(magicBody); i++ {
				if r.buf[i] != magicBody[i] {
					r.err = ErrCorrupt
					return 0, r.err
				}
			}
			continue
		}

		if chunkType <= 0x7f {
			// Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
			r.err = ErrUnsupported
			return 0, r.err

		} else {
			// Section 4.4 Padding (chunk type 0xfe).
			// Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
			if !r.readFull(r.buf[:chunkLen]) {
				return 0, r.err
			}
		}
	}
}
53872
func (r *reader) read() error {
	n, err := r.r.Read(r.b)
	if err != nil {
		return trace.Wrap(err)
	}
	if n < 1 {
		return trace.Errorf("unexpected error, read 0 bytes")
	}

	switch r.b[0] {
	case OKByte:
		return nil
	case WarnByte, ErrByte:
		r.s.Scan()
		if err := r.s.Err(); err != nil {
			return trace.Wrap(err)
		}
		return trace.Errorf(r.s.Text())
	}
	return trace.Errorf("unrecognized command: %#v", r.b)
}
887581
func (r *Reader) Read(p []byte) (n int, err error) {
	if false && debug {
		defer func(p []byte) {
			fmt.Printf("Wrote %v bytes: %q (err=%v)\n", n, p[:n], err)
			if err != nil {
				fmt.Println()
			}
		}(p)
	}

	for len(p) > 0 {
		if len(r.cur) == 0 {
			if len(r.stack) == 0 {
				// No more nodes to read.
				return n, io.EOF
			}
			// Done reading this node.
			r.nextNode()
		}

		m := copy(p, r.cur)
		r.cur = r.cur[m:]
		p = p[m:]
		n += m
	}
	if len(r.cur) == 0 && len(r.stack) == 0 {
		err = io.EOF
	}

	return
}
1220868
func (c *I2PConn) Read(d []byte) (n int, err error) {
	n, err = c.c.Read(d)
	return
}
1084885
func (br *bufReader) Read(p []byte) (n int, err error) {
	if br.err != nil {
		return 0, br.err
	}
	return br.buf.Read(p)
}
167007
func (t *timeoutReader) Read(p []byte) (int, error) {
	// FIXME limit the amount of data read in one chunk so as to not exceed the timeout?
	// Do the read in the background
	type result struct {
		n   int
		err error
	}
	done := make(chan result, 1)
	go func() {
		n, err := t.reader.Read(p)
		done <- result{n, err}
	}()
	// Wait for the read or the timeout
	timer := time.NewTimer(t.timeout)
	defer timer.Stop()
	select {
	case r := <-done:
		return r.n, r.err
	case <-timer.C:
		t.cancel()
		return 0, TimeoutError
	}
	panic("unreachable") // for Go 1.0
}
108011
func (c *CountingReader) Read(buf []byte) (int, error) {
	amount, err := c.Reader.Read(buf)
	c.Count += int64(amount)
	return amount, err
}
726430
func (gr *GroupReader) Read(p []byte) (n int, err error) {
	lenP := len(p)
	if lenP == 0 {
		return 0, errors.New("given empty slice")
	}

	gr.mtx.Lock()
	defer gr.mtx.Unlock()

	// Open file if not open yet
	if gr.curReader == nil {
		if err = gr.openFile(gr.curIndex); err != nil {
			return 0, err
		}
	}

	// Iterate over files until enough bytes are read
	var nn int
	for {
		nn, err = gr.curReader.Read(p[n:])
		n += nn
		if err == io.EOF {
			if n >= lenP {
				return n, nil
			}
			// Open the next file
			if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
				return n, err1
			}
		} else if err != nil {
			return n, err
		} else if nn == 0 { // empty file
			return n, err
		}
	}
}
685290
func (r *Reader) Read(p []byte) (int, error) {
	// If we haven't drawn before, initialize the progress bar
	if r.lastDraw.IsZero() {
		r.initProgress()
	}

	// Read from the underlying source
	n, err := r.Reader.Read(p)

	// Always increment the progress even if there was an error
	r.progress += int64(n)

	// If we don't have any errors, then draw the progress. If we are
	// at the end of the data, then finish the progress.
	if err == nil {
		// Only draw if we read data or we've never read data before (to
		// initialize the progress bar).
		if n > 0 {
			r.drawProgress()
		}
	}
	if err == io.EOF {
		r.finishProgress()
	}

	return n, err
}
859349
func (r *Reader) Read(p []byte) (int, error) {
	r.Lock()
	defer r.Unlock()
	return r.r.Read(p)
}
1246714
func (r *Reader) Read() ([]byte, error) {
	r.Lock()
	defer r.Unlock()

	if _, err := io.ReadFull(r.r, r.buff[:]); err != nil {
		return nil, err
	}

	length := int64(binary.BigEndian.Uint32(r.buff[:]))
	if length < _fastPathFrameSize {
		return r.readFastPath(length)
	}

	var buff bytes.Buffer
	_, err := io.CopyN(&buff, r.r, length)
	if err != nil {
		return nil, err
	}

	return buff.Bytes(), nil
}