// CopyReaderAt copies to a writer from a given reader at for the given
// number of bytes. This copy does not commit the writer.
977356
func CopyReaderAt(cw Writer, ra ReaderAt, n int64) error {
	ws, err := cw.Status()
	if err != nil {
		return err
	}

	_, err = copyWithBuffer(cw, io.NewSectionReader(ra, ws.Offset, n))
	return err
}
459068
func (r *copyReader) init() {
	_, r.rerr = r.WriteTo(&r.rbuf)
}
1366536
func (in *inode) ReadAt(p []byte, off int64) (n int, err error) {
	if !in.isFile() {
		panic("ReadAt called on non-file.")
	}

	// Ensure the offset is in range.
	if off > int64(len(in.contents)) {
		err = io.EOF
		return
	}

	// Read what we can.
	n = copy(p, in.contents[off:])
	if n < len(p) {
		err = io.EOF
	}

	return
}
74694
func (r *importReader) readByte() byte {
	c, err := r.b.ReadByte()
	if err == nil {
		r.buf = append(r.buf, c)
		if c == 0 {
			err = errNUL
		}
	}
	if err != nil {
		if err == io.EOF {
			r.eof = true
		} else if r.err == nil {
			r.err = err
		}
		c = 0
	}
	return c
}
364908
func (tx *Tx) CopyFromReader(r io.Reader, sql string) (commandTag CommandTag, err error) {
	if tx.status != TxStatusInProgress {
		return CommandTag(""), ErrTxClosed
	}

	return tx.conn.CopyFromReader(r, sql)
}
553992
func (r Rope) ReadAt(p []byte, off int64) (n int, err error) {
	expected := len(p)
	if off < 0 {
		return 0, errors.New("Rope.ReadAt: invalid offset")
	}

	if len(p) == 0 {
		return
	}

	if r.node == nil {
		if len(p) > 0 {
			err = io.EOF
		}
		return
	}
	n = r.node.readAt(p, off)
	if n < expected {
		err = io.EOF
	}
	return
}
842185
func (wpr *Wrapper) WriteAt(p []byte, off int64) (n int, err error) {
	wrap := NewWrapWriter(wpr.rwa, wpr.O+off, wpr.N)
	w := limio.LimitWriter(wrap, wpr.N-off)
	n, err = w.Write(p)
	if wpr.L < off+int64(n) {
		wpr.L = int64(n) + off
	}
	return n, err
}
1182279
func (r *countReader) ReadByte() (byte, error) {
	b, err := r.fr.ReadByte()
	if err == nil {
		r.off++
	}
	return b, err
}
1048037
func CopyBatch(dst BatchWriter, src Reader, batchSize int) (cnt int, err error) {
	if batchSize <= 0 {
		batchSize = DefaultBatch
	}
	buf := make([]Quad, batchSize)
	bsrc, ok := src.(BatchReader)
	if !ok {
		bsrc = batchReader{src}
	}
	var n int
	for err == nil {
		n, err = bsrc.ReadQuads(buf)
		if err != nil && err != io.EOF {
			return
		}
		eof := err == io.EOF
		n, err = dst.WriteQuads(buf[:n])
		cnt += n
		if eof {
			break
		}
	}
	return
}
147490
func (c *Conn) CopyBytes() error {
	defer c.Close()

	go func() {
		// Need this since Copy is only guaranteed to stop when it's source reader
		// (second arg) hits EOF or error but either conn might close first possibly
		// causing this goroutine to exit but not the outer one. See
		// TestConnSrcClosing which will fail if you comment the defer below.
		defer c.Close()
		io.Copy(&c.dstW, c.src)
	}()

	_, err := io.Copy(&c.srcW, c.dst)
	// Note that we don't wait for the other goroutine to finish because it either
	// already has due to it's src conn closing, or it will once our defer fires
	// and closes the source conn. No need for the extra coordination.
	if atomic.LoadInt32(&c.stopping) == 1 {
		return nil
	}
	return err
}
444421
func (a *BytesRef) copyBytes(other *BytesRef) {
	if len(a.Bytes)-a.Offset < other.Length {
		a.Bytes = make([]byte, other.Length)
		a.Offset = 0
	}
	copy(a.Bytes, other.Bytes[other.Offset:other.Offset+other.Length])
	a.Length = other.Length
}
1057554
func (c *Conn) CopyFromReader(r io.Reader, sql string) (CommandTag, error) {
	if err := c.sendSimpleQuery(sql); err != nil {
		return "", err
	}

	if err := c.readUntilCopyInResponse(); err != nil {
		return "", err
	}
	buf := c.wbuf

	buf = append(buf, copyData)
	sp := len(buf)
	for {
		n, err := r.Read(buf[5:cap(buf)])
		if err == io.EOF && n == 0 {
			break
		}
		buf = buf[0 : n+5]
		pgio.SetInt32(buf[sp:], int32(n+4))

		if _, err := c.conn.Write(buf); err != nil {
			return "", err
		}
	}

	buf = buf[:0]
	buf = append(buf, copyDone)
	buf = pgio.AppendInt32(buf, 4)

	if _, err := c.conn.Write(buf); err != nil {
		return "", err
	}

	for {
		msg, err := c.rxMsg()
		if err != nil {
			return "", err
		}

		switch msg := msg.(type) {
		case *pgproto3.ReadyForQuery:
			c.rxReadyForQuery(msg)
			return "", err
		case *pgproto3.CommandComplete:
			return CommandTag(msg.CommandTag), nil
		case *pgproto3.ErrorResponse:
			return "", c.rxErrorResponse(msg)
		default:
			return "", c.processContextFreeMsg(msg)
		}
	}
}
735485
func (pr *Reader) ReadOffset(pd *Decoder, rcs RangeCodes) uint {
	rc := rcs[pr.ReadSymbol(pd)]
	return uint(rc.Base) + pr.ReadBits(uint(rc.Len))
}
478474
func BytesReader(b []byte, d EncodingHint) io.Reader {
	return NewReader(bytes.NewReader(b), d)
}
563177
func (w *WrapWriter) WriteAt(p []byte, off int64) (n int, err error) {
	return Wrap(w, p, off, w.wrapAt)
}
444853
func (d *Description) copyTo(s string, length int) []byte {
	b := make([]byte, length)
	copy(b, []byte(s))
	return b
}
719557
func (rb *reorderBuffer) runeAt(n int) rune {
	inf := rb.rune[n]
	r, _ := utf8.DecodeRune(rb.byte[inf.pos : inf.pos+inf.size])
	return r
}