// getInternalReader because we implement io.ReadCloser and optionally hold a large buffer of the response (created by
// the user's request)
484851
func (r *Response) getInternalReader() io.Reader {

	if r.internalByteBuffer.Len() != 0 {
		return r.internalByteBuffer
	}
	return r
}
881735
func NewReadCloser(r io.Reader, c io.Closer) io.ReadCloser {
	return &readCloser{Reader: r, closer: c}
}
798815
func (i *BatchIndexer) addInternal(doc *Doc) (int, error) {
	b, err := json.Marshal(&indexDoc{Doc: doc})
	if err != nil {
		return i.cnt, err
	}
	i.buf.Write(b)
	i.buf.Write([]byte("\n"))
	b, err = json.Marshal(doc.Source)
	if err != nil {
		return i.cnt, nil
	}
	i.buf.Write(b)
	i.buf.Write([]byte("\n"))
	i.cnt++
	return i.cnt, nil
}
784788
func (r *Response) ClearInternalBuffer() {

	if r == nil || r.internalByteBuffer == nil {
		return
	}

	r.internalByteBuffer.Reset()
}
1091451
func GetDecompressor(namespace string, in io.Reader) (io.ReadCloser, error) {
	if isCompressed(namespace) {
		return zlib.NewReader(in)
	}
	return ioutil.NopCloser(in), nil
}
47882
func (s *boshDirector) GetInfo() (io.ReadCloser, error) {
	endpoint := fmt.Sprintf(ERDirectorInfoURL, s.ip)
	return s.get(endpoint)
}
833746
func WithIoReader(io io.ReadCloser) Reader {
	csvReader := csv.NewReader(io)
	csvReader.FieldsPerRecord = -1
	return WithCsvReader(csvReader, io)
}
619970
func (s *Stdin) NewReader(id string) (io.ReadCloser, error) {
	return os.Stdin, nil
}
828832
func (m *Client) NewReader(o string) (io.ReadCloser, error) {
	return m.NewReaderWithContext(context.Background(), o)
}
643284
func ErrorInternal(c web.C, rw http.ResponseWriter, err error) {
	// produce stack backtrace, max 64KB
	const size = 64 << 10 // 64KB
	buf := make([]byte, size)
	buf = buf[:runtime.Stack(buf, false)]
	lines := strings.Split(string(buf), "\n")
	c.Env["stack"] = lines[3:]

	if err != nil {
		ErrorString(c, rw, 500, err.Error())
	} else {
		ErrorString(c, rw, 500, "nil err passed into gojiutil.ErrorInternal")
	}
}
828281
func (i *Image) LayerReader(id string) (io.ReadCloser, error) {
	resp, err := i.getResponse(fmt.Sprintf("v1/images/%s/layer", id))
	if err != nil {
		return nil, err
	}
	return resp.Body, nil
}
561149
func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
	return &ReadCloserWrapper{
		Reader: r,
		closer: closer,
	}
}
575674
func StringReader(body string) io.ReadCloser {
	b := bytes.NewReader([]byte(body))

	rc, ok := io.Reader(b).(io.ReadCloser)
	if !ok && b != nil {
		rc = ioutil.NopCloser(b)
	}

	return rc
}
753553
func contentReadSeekerCloser(r resource.Resource) (hugio.ReadSeekCloser, error) {
	switch rr := r.(type) {
	case resource.ReadSeekCloserResource:
		rc, err := rr.ReadSeekCloser()
		if err != nil {
			return nil, err
		}
		return rc, nil
	default:
		return nil, fmt.Errorf("cannot transform content of Resource of type %T", r)

	}
}
179732
func (s *storageImpl) GetReader(c context.Context, ref *api.ObjectRef) (r gs.Reader, err error) {
	defer func() { err = grpcutil.GRPCifyAndLogErr(c, err) }()

	if err = common.ValidateObjectRef(ref, common.KnownHash); err != nil {
		return nil, errors.Annotate(err, "bad ref").Err()
	}

	cfg, err := s.settings(c)
	if err != nil {
		return nil, err
	}

	r, err = s.getGS(c).Reader(c, cfg.ObjectPath(ref), 0)
	if err != nil {
		ann := errors.Annotate(err, "can't read the object")
		if gs.StatusCode(err) == http.StatusNotFound {
			ann.Tag(grpcutil.NotFoundTag)
		}
		return nil, ann.Err()
	}
	return r, nil
}
604627
func IsInternalError(err error) (bool, error) {
	e, ok := errs.Cause(err).(InternalError)
	if !ok {
		return false, nil
	}
	return true, e
}
1294641
func getReadingByIdHandler(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	vars := mux.Vars(r)
	id := vars["id"]

	switch r.Method {
	case http.MethodGet:
		reading, err := getReadingById(id)
		if err != nil {
			switch err := err.(type) {
			case *errors.ErrDbNotFound:
				http.Error(w, err.Error(), http.StatusNotFound)
				return
			default: //return an error on everything else.
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}

		encode(reading, w)
	}
}