// writeGzip writes a compressed form of p to the underlying io.Writer. The
// compressed bytes are not necessarily flushed until the Writer is closed.
488167
func writeGzip(w io.Writer, b []byte) (int, error) {
	gzipWriter := acquireGzipWriter(w)
	n, err := gzipWriter.Write(b)
	if err != nil {
		releaseGzipWriter(gzipWriter)
		return -1, err
	}
	err = gzipWriter.Flush()
	releaseGzipWriter(gzipWriter)
	return n, err
}
885992
func (this *Writer) Write(p []byte) (n int, err error) {
	// write data into our internal buffer
	bytesRead, bytesWritten, err := this.converter.Convert(p, this.buffer[this.writePos:])

	// update bytes written for return
	n += bytesRead
	this.writePos += bytesWritten

	// checks for when we have a full buffer
	for this.writePos > 0 {
		// if we have an error, just return it
		if this.err != nil {
			return
		}

		// else empty the buffer
		this.emptyBuffer()
	}

	return n, err
}
48626
func (f *FileWriter) Write(p []byte) (int, error) {
	f.mu.Lock()
	n, err := f.f.Write(p)
	f.mu.Unlock()
	return n, err
}
506271
func Gzip(c lars.Context) {

	c.Response().Header().Add(lars.Vary, lars.AcceptEncoding)

	if strings.Contains(c.Request().Header.Get(lars.AcceptEncoding), lars.Gzip) {

		gw := writerPool.Get().(*gzipWriter)
		gw.sniffComplete = false
		w := gw.Writer.(*gzip.Writer)
		w.Reset(c.Response().Writer())
		gw.ResponseWriter = c.Response().Writer()

		defer func() {

			if !gw.sniffComplete {
				// We have to reset response to it's pristine state when
				// nothing is written to body.
				c.Response().Header().Del(lars.ContentEncoding)
				w.Reset(ioutil.Discard)
			}

			w.Close()
			writerPool.Put(gw)
		}()

		c.Response().Header().Set(lars.ContentEncoding, lars.Gzip)
		c.Response().SetWriter(gw)
	}

	c.Next()
}
1154754
func WriteZip(w io.Writer, fs VFS) error {
	zw := zip.NewWriter(w)
	err := copyVFS(fs, func(p string, info os.FileInfo, f io.Reader) error {
		hdr, err := zip.FileInfoHeader(info)
		if err != nil {
			return err
		}
		hdr.Name = p
		fw, err := zw.CreateHeader(hdr)
		if err != nil {
			return err
		}
		_, err = io.Copy(fw, f)
		return err
	})
	if err != nil {
		return err
	}
	return zw.Close()
}
1155758
func NewGzip(level int) *gzipMiddleware {
	h := &gzipMiddleware{}
	h.pool.New = func() interface{} {
		gz, err := gzip.NewWriterLevel(ioutil.Discard, level)
		if err != nil {
			panic(err)
		}
		return gz
	}
	return h
}
1210013
func (gz *Gz) Compress(in io.Reader, out io.Writer) error {
	w, err := gzip.NewWriterLevel(out, gz.CompressionLevel)
	if err != nil {
		return err
	}
	defer w.Close()
	_, err = io.Copy(w, in)
	return err
}
244601
func (w *Writer) Write(p []byte) (nRet int, errRet error) {
	if w.ibuf == nil {
		// Do not buffer incoming bytes. This does not perform or compress well
		// if the caller of Writer.Write writes many small slices. This
		// behavior is therefore deprecated, but still supported for backwards
		// compatibility with code that doesn't explicitly Flush or Close.
		return w.write(p)
	}

	// The remainder of this method is based on bufio.Writer.Write from the
	// standard library.

	for len(p) > (cap(w.ibuf)-len(w.ibuf)) && w.err == nil {
		var n int
		if len(w.ibuf) == 0 {
			// Large write, empty buffer.
			// Write directly from p to avoid copy.
			n, _ = w.write(p)
		} else {
			n = copy(w.ibuf[len(w.ibuf):cap(w.ibuf)], p)
			w.ibuf = w.ibuf[:len(w.ibuf)+n]
			w.Flush()
		}
		nRet += n
		p = p[n:]
	}
	if w.err != nil {
		return nRet, w.err
	}
	n := copy(w.ibuf[len(w.ibuf):cap(w.ibuf)], p)
	w.ibuf = w.ibuf[:len(w.ibuf)+n]
	nRet += n
	return nRet, nil
}
1097266
func (zip GobMeddler) PreWrite(field interface{}) (saveValue interface{}, err error) {
	buffer := new(bytes.Buffer)

	if zip {
		// gob encode and gzip
		gzipWriter := gzip.NewWriter(buffer)
		defer gzipWriter.Close()
		gobEncoder := gob.NewEncoder(gzipWriter)
		if err := gobEncoder.Encode(field); err != nil {
			return nil, fmt.Errorf("Gob encoding/gzip error: %v", err)
		}
		if err := gzipWriter.Close(); err != nil {
			return nil, fmt.Errorf("Closing gzip writer: %v", err)
		}

		return buffer.Bytes(), nil
	}

	// gob encode
	gobEncoder := gob.NewEncoder(buffer)
	if err := gobEncoder.Encode(field); err != nil {
		return nil, fmt.Errorf("Gob encoding error: %v", err)
	}
	return buffer.Bytes(), nil
}
1150154
func (w *Writer) write(p []byte) error {
	for _, b := range p {
		if b == '\n' || b == '\r' {
			// If the previous byte was \r, the CRLF has already been inserted.
			if w.cr && b == '\n' {
				w.cr = false
				continue
			}

			if b == '\r' {
				w.cr = true
			}

			if err := w.checkLastByte(); err != nil {
				return err
			}
			if err := w.insertCRLF(); err != nil {
				return err
			}
			continue
		}

		if w.i == lineMaxLen-1 {
			if err := w.insertSoftLineBreak(); err != nil {
				return err
			}
		}

		w.line[w.i] = b
		w.i++
		w.cr = false
	}

	return nil
}
436200
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
	w.applyFilter()
	if w.dogzip {
		// Write compressed
		return w.Writer.Write(b)
	}
	// Write uncompressed
	return w.ResponseWriter.Write(b)
}
699403
func (w *writer) write(p []byte) (int, error) {
	var err error

	if len(p) > MaxBlockSize {
		return 0, errors.New(fmt.Sprintf("block too large %d > %d", len(p), MaxBlockSize))
	}

	w.dst = w.dst[:cap(w.dst)] // Encode does dumb resize w/o context. reslice avoids alloc.
	w.dst, err = snappy.Encode(w.dst, p)
	if err != nil {
		return 0, err
	}
	block := w.dst
	n := len(p)
	compressed := true

	// check for data which is better left uncompressed.  this is determined if
	// the encoded content is longer than the source.
	if len(w.dst) >= len(p) {
		compressed = false
		block = p[:n]
	}

	if !w.sentStreamID {
		_, err := w.writer.Write(streamID)
		if err != nil {
			return 0, err
		}
		w.sentStreamID = true
	}

	// set the block type
	if compressed {
		writeHeader(w.hdr, blockCompressed, block, p[:n])
	} else {
		writeHeader(w.hdr, blockUncompressed, block, p[:n])
	}

	_, err = w.writer.Write(w.hdr)
	if err != nil {
		return 0, err
	}

	_, err = w.writer.Write(block)
	if err != nil {
		return 0, err
	}

	return n, nil
}
27313
func (s *cryptoStreamImpl) Write(p []byte) (int, error) {
	s.writeBuf = append(s.writeBuf, p...)
	return len(p), nil
}
1089312
func (w *fsHandler) Gzip(enable bool) StaticHandlerBuilder {
	w.gzip = enable
	return w
}
384156
func (grw gzipResponseWriter) Write(b []byte) (int, error) {
	if len(grw.Header().Get(headerContentType)) == 0 {
		grw.Header().Set(headerContentType, http.DetectContentType(b))
	}
	if len(grw.Header().Get(headerContentEncoding)) > 0 {
		// compress the content
		return grw.gz.Write(b)
	}
	// no compress
	grw.gz.Reset(ioutil.Discard)
	return grw.napWriter.Write(b)
}
233956
func (w *Writer) Write(p []byte) (n int, err error) {
	return w.buf.Write(p)
}
888846
func newWriter(c Config, w io.Writer) (*gzip.Writer, error) {
	if c.Level >= gzip.BestSpeed && c.Level <= gzip.BestCompression {
		return gzip.NewWriterLevel(w, c.Level)
	}
	return gzip.NewWriter(w), nil
}