// HandleRead handles a read request assuming that data is the entire file content.
// It adjusts the amount returned in resp according to req.Offset and req.Size.
559177
func HandleRead(req *ReadRequest, resp *ReadResponse, data []byte) {
	if req.Offset >= int64(len(data)) {
		data = nil
	} else {
		data = data[req.Offset:]
	}
	if len(data) > req.Size {
		data = data[:req.Size]
	}
	n := copy(resp.Data[:req.Size], data)
	resp.Data = resp.Data[:n]
}
1018222
func Handler(w http.ResponseWriter, r *http.Request) {
	values := r.URL.Query()
	for _, c := range []string{"1", "true"} {
		if values.Get("pp") == c {
			prettyPrint = true
		}
	}

	var jsonBytes []byte
	var jsonErr error
	if prettyPrint {
		jsonBytes, jsonErr = json.MarshalIndent(GetStats(), "", "  ")
	} else {
		jsonBytes, jsonErr = json.Marshal(GetStats())
	}
	var body string
	if jsonErr != nil {
		body = jsonErr.Error()
	} else {
		body = string(jsonBytes)
	}

	if newLineTerm {
		body += "\n"
	}

	headers := make(map[string]string)
	headers["Content-Type"] = "application/json"
	headers["Content-Length"] = strconv.Itoa(len(body))
	for name, value := range headers {
		w.Header().Set(name, value)
	}

	if jsonErr != nil {
		w.WriteHeader(http.StatusInternalServerError)
	} else {
		w.WriteHeader(http.StatusOK)
	}

	io.WriteString(w, body)
}
629476
func (m *Messenger) HandleRead(f ReadHandler) {
	m.readHandlers = append(m.readHandlers, f)
}
253129
func (r *HttpReadSeeker) Read(p []byte) (n int, err error) {
	if r.r == nil {
		err = r.rangeRequest()
	}
	if r.r != nil {
		n, err = r.r.Read(p)
		r.pos += int64(n)
	}
	return
}
406878
func (c *Characteristic) HandleReadFunc(f func(rsp ResponseWriter, req *ReadRequest)) {
	c.HandleRead(ReadHandlerFunc(f))
}
987191
func ReadResp(r *Reader) (Resp, error) {
	atom, err := r.ReadAtom()
	if err != nil {
		return nil, err
	}
	tag, ok := atom.(string)
	if !ok {
		return nil, newParseError("response tag is not an atom")
	}

	if tag == "+" {
		if err := r.ReadSp(); err != nil {
			r.UnreadRune()
		}

		resp := &ContinuationReq{}
		resp.Info, err = r.ReadInfo()
		if err != nil {
			return nil, err
		}

		return resp, nil
	}

	if err := r.ReadSp(); err != nil {
		return nil, err
	}

	// Can be either data or status
	// Try to parse a status
	var fields []interface{}
	if atom, err := r.ReadAtom(); err == nil {
		fields = append(fields, atom)

		if err := r.ReadSp(); err == nil {
			if name, ok := atom.(string); ok {
				status := StatusRespType(name)
				switch status {
				case StatusRespOk, StatusRespNo, StatusRespBad, StatusRespPreauth, StatusRespBye:
					resp := &StatusResp{
						Tag:  tag,
						Type: status,
					}

					char, _, err := r.ReadRune()
					if err != nil {
						return nil, err
					}
					r.UnreadRune()

					if char == '[' {
						// Contains code & arguments
						resp.Code, resp.Arguments, err = r.ReadRespCode()
						if err != nil {
							return nil, err
						}
					}

					resp.Info, err = r.ReadInfo()
					if err != nil {
						return nil, err
					}

					return resp, nil
				}
			}
		} else {
			r.UnreadRune()
		}
	} else {
		r.UnreadRune()
	}

	// Not a status so it's data
	resp := &DataResp{Tag: tag}

	var remaining []interface{}
	remaining, err = r.ReadLine()
	if err != nil {
		return nil, err
	}

	resp.Fields = append(fields, remaining...)
	return resp, nil
}
676846
func (c chunkedLimitedReader) Read(p []byte) (n int, err error) {
	// If we've already read all our chunks and the remainders are <= 0, we're done
	if c.d.doneChunks >= c.d.numChunks || (c.d.remaining <= 0 && c.d.chunkRem <= 0) {
		return 0, io.EOF
	}

	// Data is done, returning only buffer bytes now
	if c.d.remaining <= 0 {
		if int64(len(p)) > c.d.chunkRem {
			p = p[0:c.d.chunkRem]
		}

		for i := range p {
			p[i] = 0
		}

		c.d.chunkRem -= int64(len(p))
		return len(p), nil
	}

	// Data is not yet done, but chunk is
	if c.d.chunkRem <= 0 {
		return 0, io.EOF
	}

	// Data and chunk not yet done, need to read from outside reader
	if int64(len(p)) > c.d.remaining || int64(len(p)) > c.d.chunkRem {
		rem := int64(math.Min(float64(c.d.remaining), float64(c.d.chunkRem)))
		p = p[0:rem]
	}

	n, err = c.d.reader.Read(p)
	c.d.remaining -= int64(n)
	c.d.chunkRem -= int64(n)

	return
}
1116125
func (self *_RegExp_parser) read() {
	if self.offset < self.length {
		self.chrOffset = self.offset
		chr, width := rune(self.str[self.offset]), 1
		if chr >= utf8.RuneSelf { // !ASCII
			chr, width = utf8.DecodeRuneInString(self.str[self.offset:])
			if chr == utf8.RuneError && width == 1 {
				self.error(self.chrOffset, "Invalid UTF-8 character")
			}
		}
		self.offset += width
		self.chr = chr
	} else {
		self.chrOffset = self.length
		self.chr = -1 // EOF
	}
}
1054816
func (f *FileReadSeeker) Read(p []byte) (int, error) {
	return f.file.Read(p)
}
583071
func (f HandlerFunc) Serve(ctx context.Context, in io.Reader, out io.Writer) {
	f(ctx, in, out)
}
330395
func (b *retryableRequestBody) Read(p []byte) (n int, err error) {
	return b.body.Read(p)
}
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)
}
500885
func Read(file string) (*Data, error) {
	data := New()
	if err := data.Open(file); err != nil {
		return nil, err
	}
	return data, nil
}
392997
func (d *Descriptor) HandleRead(h ReadHandler) {
	if d.value != nil {
		panic("descriptor has been configured with a static value")
	}
	d.props |= CharRead
	// d.secure |= CharRead
	d.rhandler = h
}
1203682
func (rbp *responseBodyProgress) Read(p []byte) (n int, err error) {
	n, err = rbp.responseBody.Read(p)
	rbp.offset += int64(n)

	// Invokes the user's callback method to report progress
	rbp.pr(rbp.offset)
	return
}
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)
}
1057193
func (this *Response) ReadAll() ([]byte, error) {
	var reader io.ReadCloser
	var err error
	switch this.Header.Get("Content-Encoding") {
	case "gzip":
		reader, err = gzip.NewReader(this.Body)
		if err != nil {
			return nil, err
		}
	default:
		reader = this.Body
	}

	defer reader.Close()
	return ioutil.ReadAll(reader)
}