// ReadAt implements io.ReaderAt.
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
}
372320
func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
	p := &t.pieces[off/t.info.PieceLength]
	p.waitNoPendingWrites()
	return p.Storage().ReadAt(b, off-p.Info().Offset())
}
174463
func (r *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error) {
	metrics.GetOrRegisterCounter("lazychunkreader.readat", nil).Inc(1)

	var sp opentracing.Span
	var cctx context.Context
	cctx, sp = spancontext.StartSpan(
		r.ctx,
		"lcr.read")
	defer sp.Finish()

	defer func() {
		sp.LogFields(
			olog.Int("off", int(off)),
			olog.Int("read", read))
	}()

	// this is correct, a swarm doc cannot be zero length, so no EOF is expected
	if len(b) == 0 {
		return 0, nil
	}
	quitC := make(chan bool)
	size, err := r.Size(cctx, quitC)
	if err != nil {
		log.Debug("lazychunkreader.readat.size", "size", size, "err", err)
		return 0, err
	}

	errC := make(chan error)

	// }
	var treeSize int64
	var depth int
	// calculate depth and max treeSize
	treeSize = r.chunkSize
	for ; treeSize < size; treeSize *= r.branches {
		depth++
	}
	wg := sync.WaitGroup{}
	length := int64(len(b))
	for d := 0; d < r.depth; d++ {
		off *= r.chunkSize
		length *= r.chunkSize
	}
	wg.Add(1)
	go r.join(cctx, b, off, off+length, depth, treeSize/r.branches, r.chunkData, &wg, errC, quitC)
	go func() {
		wg.Wait()
		close(errC)
	}()

	err = <-errC
	if err != nil {
		log.Debug("lazychunkreader.readat.errc", "err", err)
		close(quitC)
		return 0, err
	}
	if off+int64(len(b)) >= size {
		log.Debug("lazychunkreader.readat.return at end", "size", size, "off", off)
		return int(size - off), io.EOF
	}
	log.Debug("lazychunkreader.readat.errc", "buff", len(b))
	return len(b), nil
}
400789
func (t *TSMReader) ReadStringBlockAt(entry *IndexEntry, vals *[]StringValue) ([]StringValue, error) {
	t.mu.RLock()
	v, err := t.accessor.readStringBlock(entry, vals)
	t.mu.RUnlock()
	return v, err
}
212528
func (file *File) WriteAt(buf []byte, offset int64) (int, error) {
	return file.Fid.Clnt.Write(file.Fid, buf, uint64(offset))
}
51784
func (s Slice) ValueAt(index ValueLength) (Slice, error) {
	if !s.IsObject() {
		return nil, InvalidTypeError{"Expecting type Object"}
	}

	key, err := s.getNthKey(index, false)
	if err != nil {
		return nil, WithStack(err)
	}
	byteSize, err := key.ByteSize()
	if err != nil {
		return nil, WithStack(err)
	}
	return Slice(key[byteSize:]), nil
}
559669
func StringAt(reader io.ReadSeeker, offset int64) (cs ChunkString, err error) {
	// Backup offset
	backup := BackupSeeker(reader)
	// Offsets are relative to start of chunk
	GoToSeeker(reader, offset)
	err = cs.Parse(reader)
	/*if err != nil {
		return
	}*/
	GoToSeeker(reader, backup)
	return
}
27586
func (ch *Chunk) ReadByte() (byte, error) {
	if ch.IsFullyRead() {
		return 0, io.EOF
	}
	var r byte
	err := ch.ReadLE(&r)
	return r, err
}
560584
func (f *File) ReadAt(buf []byte, off int64) (n int, err error) {
	size := f.Size()
	if off >= size {
		return 0, io.EOF
	}
	if n = len(buf); int64(n) > size-off {
		n = int(size - off)
	}

	lfrag := int(uint32(off) % uint32(f.fs.BlockSize))
	off -= int64(lfrag)
	want := lfrag + n
	rfrag := -want & (f.fs.BlockSize - 1)
	want += rfrag

	offb := uint32(off / int64(f.fs.BlockSize))

	nblock := want / f.fs.BlockSize
	for i := 0; i < nblock; i++ {
		b, err := f.dblock(offb + uint32(i))
		if err != nil {
			return 0, err
		}
		dbuf, err := f.fs.readData(b, 0, nil)
		if err != nil {
			return 0, err
		}
		m := copy(buf, dbuf[lfrag:])
		buf = buf[m:]
		lfrag = 0
	}
	return n, nil
}
360633
func OpenReaderAt(r io.ReaderAt, size int64) (*File, error) {
	return OpenReaderAtWithRowLimit(r, size, NoRowLimit)
}
782552
func charAt(s string, i int) byte {
	if i >= 0 && i < len(s) {
		return s[i]
	}
	return s[0]
}
972827
func (br *bufReader) ReadText(encoding Encoding) []byte {
	if br.err != nil {
		return nil
	}

	var text []byte
	delims := encoding.TerminationBytes
	text, br.err = br.readTillDelims(delims)
	br.Discard(len(delims))

	return text
}
425039
func (r *HttpReadSeeker) ReadAt(p []byte, off int64) (n int, err error) {
	var nn int

	r.Seek(off, 0)

	for n < len(p) && err == nil {
		nn, err = r.Read(p[n:])
		n += nn
	}
	return
}
249869
func (f *HttpFile) ReadAt(p []byte, off int64) (int, error) {
	DebugLog(f.Debug).Println("ReadAt", off, len(p))

	if f.client == nil {
		return 0, os.ErrInvalid
	}

	plen := len(p)
	if plen <= 0 {
		return plen, nil
	}

	bytes_range := fmt.Sprintf("bytes=%d-%d", off, off+int64(plen-1))
	resp, err := f.do("GET", headers{"Range": bytes_range})
	defer CloseResponse(resp)

	if err != nil {
		DebugLog(f.Debug).Println("ReadAt error", err)
		return 0, &HttpFileError{Err: err}
	}
	if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
		return 0, io.EOF
	}
	if resp.StatusCode != http.StatusPartialContent {
		return 0, &HttpFileError{Err: fmt.Errorf("Unexpected Status %s", resp.Status)}
	}

	content_range := resp.Header.Get("Content-Range")

	var first, last, total int64
	n, err := fmt.Sscanf(content_range, "bytes %d-%d/%d", &first, &last, &total)
	if err != nil {
		DebugLog(f.Debug).Println("Error", err)
		return 0, err
	}
	if n != 3 {
		return 0, &HttpFileError{Err: fmt.Errorf("Unexpected Content-Range %q (%d)", content_range, n)}
	}

	DebugLog(f.Debug).Println("Range", bytes_range, "Content-Range", content_range)

	n, err = io.ReadFull(resp.Body, p)
	if n > 0 && err == io.EOF {
		// read reached EOF, but archive/zip doesn't like this!
		DebugLog(f.Debug).Println("Read", n, "reached EOF")
		err = nil
	}

	DebugLog(f.Debug).Println("Read", n, err)
	return n, err
}
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
}
818569
func (r *Reader) ReadByte() (byte, error) {
	for r.buffered() < 1 && r.state == nil {
		r.more()
	}
	if r.buffered() < 1 {
		return 0, r.err()
	}
	b := r.data[r.n]
	r.n++
	return b, nil
}
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
}