// Writes to the underlying output stream if not null.
558433
func (p *StreamTransport) Write(buf []byte) (int, error) {
	if p.Writer == nil {
		return 0, NewTTransportException(NOT_OPEN, "Cannot write to null outputStream")
	}
	n, err := p.Writer.Write(buf)
	return n, NewTTransportExceptionFromError(err)
}
1172283
func (c *chunkedIntCoder) Write(w io.Writer) (int, error) {
	bufNeeded := binary.MaxVarintLen64 * (1 + len(c.chunkLens))
	if len(c.buf) < bufNeeded {
		c.buf = make([]byte, bufNeeded)
	}
	buf := c.buf

	// convert the chunk lengths into chunk offsets
	chunkOffsets := modifyLengthsToEndOffsets(c.chunkLens)

	// write out the number of chunks & each chunk offsets
	n := binary.PutUvarint(buf, uint64(len(chunkOffsets)))
	for _, chunkOffset := range chunkOffsets {
		n += binary.PutUvarint(buf[n:], chunkOffset)
	}

	tw, err := w.Write(buf[:n])
	if err != nil {
		return tw, err
	}

	// write out the data
	nw, err := w.Write(c.final)
	tw += nw
	if err != nil {
		return tw, err
	}
	return tw, nil
}
1037296
func (r *ResponseReverter) Write(buf []byte) (int, error) {
	n, err := r.ResponseWriter.Write(buf)
	return n, err
}
324094
func (w *stdWriter) Write(buf []byte) (n int, err error) {
	if w == nil || w.Writer == nil {
		return 0, errors.New("Writer not instantiated")
	}
	if buf == nil {
		return 0, nil
	}

	header := [stdWriterPrefixLen]byte{stdWriterFdIndex: w.prefix}
	binary.BigEndian.PutUint32(header[stdWriterSizeIndex:], uint32(len(buf)))

	line := append(header[:], buf...)

	n, err = w.Writer.Write(line)
	n -= stdWriterPrefixLen

	if n < 0 {
		n = 0
	}
	return
}
923594
func (l *Logger) Write(p []byte) (wLen int, err error) {
	var write = func(w io.Writer, isStdFile bool) {
		x := p
		if !isStdFile && l.flags&LnoFileAnsi != 0 {
			// TODO: If Lcolor is used, then no coloring should
			// have to be stripped. Inefficient.
			x = stripAnsiByte(x)
		}
		wLen, err = w.Write(x)
		if wLen != len(p) {
			err = io.ErrShortWrite
		}
	}
	for _, w := range l.streams {
		wIface := reflect.ValueOf(w).Interface()
		switch wType := wIface.(type) {
		case *os.File:
			if wType == os.Stdout || wType == os.Stderr {
				write(w, true)
				continue
			}
			write(w, false)
		default:
			write(w, false)
		}
	}
	return
}
513332
func (ro *RunningOutput) Write() error {
	if output, ok := ro.Output.(telegraf.AggregatingOutput); ok {
		ro.aggMutex.Lock()
		metrics := output.Push()
		ro.buffer.Add(metrics...)
		output.Reset()
		ro.aggMutex.Unlock()
	}

	atomic.StoreInt64(&ro.newMetricsCount, 0)

	// Only process the metrics in the buffer now.  Metrics added while we are
	// writing will be sent on the next call.
	nBuffer := ro.buffer.Len()
	nBatches := nBuffer/ro.MetricBatchSize + 1
	for i := 0; i < nBatches; i++ {
		batch := ro.buffer.Batch(ro.MetricBatchSize)
		if len(batch) == 0 {
			break
		}

		err := ro.write(batch)
		if err != nil {
			ro.buffer.Reject(batch)
			return err
		}
		ro.buffer.Accept(batch)
	}
	return nil
}
637811
func (l *bunyanLog) write(stream Stream, level int) error {
	data := make([]byte, 0)
	argl := len(l.args)

	if argl == 0 {
		return nil
	}

	d := make(map[string]interface{})
	d["v"] = 0
	d["level"] = level
	d["name"] = stream.Name
	d["hostname"] = l.logger.hostname
	d["pid"] = os.Getppid()
	d["time"] = nowTimestamp()

	// add static fields first
	for key, value := range l.logger.staticFields {
		if canSetField(key) {
			d[key] = l.serialize(key, value)
		}
	}

	// add passed fields/data last
	if argl == 1 && typeName(l.args[0]) == "string" {
		// if 1 argument that is a string, the string is the msg
		d["msg"] = l.serialize("msg", l.args[0])
	} else if isError(l.args[0]) {
		// if the first argument is an error, set error field with string value of error
		d["error"] = l.serialize("error", l.args[0])
		if argl == 2 {
			d["msg"] = l.serialize("msg", l.args[1])
		} else if argl > 2 {
			d["msg"] = l.serialize("msg", l.sprintf(l.args[1:]))
		}
	} else if isHashMap(l.args[0]) {
		// if the first argument is a hashmap, process its values
		for key, value := range l.args[0].(map[string]interface{}) {
			if canSetField(key) {
				d[key] = l.serialize(key, value)
			}
		}
		if argl == 2 {
			d["msg"] = l.serialize("msg", l.args[1])
		} else if argl > 2 {
			d["msg"] = l.serialize("msg", l.sprintf(l.args[1:]))
		}
	} else {
		d["msg"] = l.serialize("msg", l.sprintf(l.args))
	}

	// marshal the json
	if jsonData, err := json.Marshal(d); err != nil {
		return err
	} else {
		data = []byte(fmt.Sprintf("%s\n", string(jsonData)))
	}

	switch stream.Type {
	case LogTypeStream:
		return l.writeStream(stream, data)
	case LogTypeFile:
		return l.writeFile(stream, data)
	case LogTypeRotatingFile:
		return l.writeRotatingFile(stream, data)
	case LogTypeRaw:
		return l.writeStream(stream, data)
	}
	return nil
}
797462
func (w *BackupStreamWriter) Write(b []byte) (int, error) {
	if w.bytesLeft < int64(len(b)) {
		return 0, fmt.Errorf("too many bytes by %d", int64(len(b))-w.bytesLeft)
	}
	n, err := w.w.Write(b)
	w.bytesLeft -= int64(n)
	return n, err
}
378683
func (w *Writer) Write(p []byte) (int, error) {
	c, l, ln := cap(w.buf), len(w.buf), len(p)
	avail := c - l

	// requires flush
	if avail < ln {
		if err := w.Flush(); err != nil {
			return 0, err
		}
		l = len(w.buf)
	}
	// too big to fit in buffer;
	// write directly to w.w
	if c < ln {
		return w.w.Write(p)
	}

	// grow buf slice; copy; return
	w.buf = w.buf[:l+ln]
	return copy(w.buf[l:], p), nil
}
84776
func (xw *Writer) Write(buf []byte) (int, error) {
	if xw.err != nil {
		return 0, xw.err
	}

	var n, cnt int
	for len(buf) > 0 && xw.err == nil {
		// Flush chunk if necessary.
		remain := xw.nchk - xw.zw.InputOffset
		if remain <= 0 {
			xw.err = xw.Flush(FlushFull)
			continue
		}
		if remain > int64(len(buf)) {
			remain = int64(len(buf))
		}

		// Compress data for current chunk.
		offset := xw.zw.OutputOffset
		n, xw.err = xw.zw.Write(buf[:remain])
		xw.OutputOffset += xw.zw.OutputOffset - offset
		buf = buf[n:]
		cnt += n
	}

	xw.InputOffset += int64(cnt)
	return cnt, xw.err
}
1170457
func (aw *ansiWriter) Write(p []byte) (total int, err error) {
	if len(p) == 0 {
		return 0, nil
	}

	logger.Infof("Write: % x", p)
	logger.Infof("Write: %s", string(p))
	return aw.parser.Parse(p)
}
301493
func (pw *Writer) Write(buf []byte) (cnt int, err error) {
	if pw.numBits > 0 || pw.cntBuf > 0 {
		if pw.numBits%8 != 0 {
			return 0, errorf(errors.Invalid, "non-aligned bit buffer")
		}
		if _, err := pw.Flush(); err != nil {
			return 0, err
		}
	}
	cnt, err = pw.wr.Write(buf)
	pw.Offset += int64(cnt)
	return cnt, err
}
827772
func (c *rwConn) Write(p []byte) (int, error) {
	n, err := c.BufWriter.Write(p)
	if err := c.BufWriter.Flush(); err != nil {
		return 0, err
	}
	return n, err
}
1093891
func (w *Writer) Write(p []byte) (n int, err error) {
	for i, b := range p {
		switch {
		// Simple writes are done in batch.
		case b >= '!' && b <= '~' && b != '=':
			continue
		case isWhitespace(b) || !w.Binary && (b == '\n' || b == '\r'):
			continue
		}

		if i > n {
			if err := w.write(p[n:i]); err != nil {
				return n, err
			}
			n = i
		}

		if err := w.encode(b); err != nil {
			return n, err
		}
		n++
	}

	if n == len(p) {
		return n, nil
	}

	if err := w.write(p[n:]); err != nil {
		return n, err
	}

	return len(p), nil
}
697688
func streamWrite(ptr unsafe.Pointer, size, nmemb C.size_t, userData unsafe.Pointer) C.size_t {
	if size == 0 || nmemb == 0 {
		return nmemb
	}
	writer := callbackData.Get(userData).(io.Writer)
	buf := make([]byte, 0)
	hdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
	hdr.Data = uintptr(ptr)
	hdr.Len = int(size * nmemb)
	hdr.Cap = hdr.Len
	s := int(size)
	for i := 0; i < int(nmemb); i++ {
		if sz, err := writeFull(writer, buf[i*s:(i+1)*s]); sz < int(size) && err != nil {
			return C.size_t(i)
		}
	}
	return nmemb
}
496207
func (h *JSONWriter) Write(w http.ResponseWriter, r *http.Request, e interface{}) {
	h.WriteCode(w, r, http.StatusOK, e)
}
396332
func (b *StringBuilder) Write(p []byte) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, p...)
	return len(p), nil
}