// AcquireGzipWriter prepares a gzip writer and returns it
//
// see ReleaseGzipWriter
559421
func (p *GzipPool) AcquireGzipWriter(w io.Writer) *gzip.Writer {
	v := p.Get()
	if v == nil {
		gzipWriter, err := gzip.NewWriterLevel(w, p.Level)
		if err != nil {
			return nil
		}
		return gzipWriter
	}
	gzipWriter := v.(*gzip.Writer)
	gzipWriter.Reset(w)
	return gzipWriter
}
956100
func (f *gzipReader) Close() error {
	var errors []error
	if f.ReadCloser != nil {
		errors = append(errors, f.ReadCloser.Close())
		f.ReadCloser = nil
	}
	if f.file != nil {
		errors = append(errors, f.file.Close())
		f.file = nil
	}
	return trace.NewAggregate(errors...)
}
244436
func GetGzipOption(defaultValue bool, options map[string]interface{}) bool {
	gzipOpt := options["gzip"] // we only need that, so don't create new map to keep the options.
	if b, isBool := gzipOpt.(bool); isBool {
		return b
	}
	return defaultValue
}
222244
func AcquireWriter(w io.Writer) *Writer {
	v := writerPool.Get()
	if v == nil {
		qw := &Writer{}
		qw.e.w = &htmlEscapeWriter{}
		v = qw
	}
	qw := v.(*Writer)
	qw.e.w.(*htmlEscapeWriter).w = w
	qw.n.w = w
	return qw
}
561718
func New(target io.Writer) Archive {
	gw := gzip.NewWriter(target)
	return Archive{
		gw: gw,
	}
}
1035877
func (zip JSONMeddler) PreWrite(field interface{}) (saveValue interface{}, err error) {
	buffer := new(bytes.Buffer)

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

		return buffer.Bytes(), nil
	}

	// json encode
	jsonEncoder := json.NewEncoder(buffer)
	if err := jsonEncoder.Encode(field); err != nil {
		return nil, fmt.Errorf("JSON encoding error: %v", err)
	}
	return buffer.Bytes(), nil
}
1030341
func (c *Pool) Acquire(w http.ResponseWriter, r *http.Request) Context {
	ctx := c.pool.Get().(Context)
	ctx.BeginRequest(w, r)
	return ctx
}
999112
func WriteTarGzip(w io.Writer, fs VFS) error {
	gw, err := gzip.NewWriterLevel(w, gzip.BestCompression)
	if err != nil {
		return err
	}
	if err := WriteTar(gw, fs); err != nil {
		return err
	}
	return gw.Close()
}
1202930
func GZip(h http.Handler) http.Handler {
	hfunc := func(w http.ResponseWriter, r *http.Request) {
		ae := strings.Split(r.Header.Get("Accept-Encoding"), ",")
		acceptsGzip := false
		for i := range ae {
			if strings.TrimSpace(ae[i]) == "gzip" {
				acceptsGzip = true
				break
			}
		}
		if acceptsGzip {
			// Remove "Accept-Encoding" header to ensure applications further
			// down the line don't attempt to gzip the response again.
			r.Header.Del("Accept-Encoding")
			gzw := gzip.NewWriter(w)
			wrapper := &gzipWrapper{w, gzw, &sync.Mutex{}, true, false}
			hijacker, ok := w.(http.Hijacker)
			if ok {
				// if writer accepts hijacking pass a hijackable wrapper
				hijackableWrapper := &gzipHijacker{wrapper, hijacker, false}
				defer func() {
					if !hijackableWrapper.hijacked {
						gzw.Close()
					}
				}()
				w = hijackableWrapper
			} else {
				defer gzw.Close()
				w = wrapper
			}
		}
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(hfunc)
}
1276258
func ExtractTar(r *Registry, in io.Reader) error {
	return extractTar(r, in, true)
}
1063607
func (z *Writer) Write(p []byte) (int, error) {
	if z.err != nil {
		return 0, z.err
	}
	var n int
	// Write the GZIP header lazily.
	if !z.wroteHeader {
		z.wroteHeader = true
		z.buf[0] = gzipID1
		z.buf[1] = gzipID2
		z.buf[2] = gzipDeflate
		z.buf[3] = 0
		if z.Extra != nil {
			z.buf[3] |= 0x04
		}
		if z.Name != "" {
			z.buf[3] |= 0x08
		}
		if z.Comment != "" {
			z.buf[3] |= 0x10
		}
		le.PutUint32(z.buf[4:8], uint32(z.ModTime.Unix()))
		if z.level == BestCompression {
			z.buf[8] = 2
		} else if z.level == BestSpeed {
			z.buf[8] = 4
		} else {
			z.buf[8] = 0
		}
		z.buf[9] = z.OS
		n, z.err = z.w.Write(z.buf[:10])
		if z.err != nil {
			return n, z.err
		}
		if z.Extra != nil {
			z.err = z.writeBytes(z.Extra)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Name != "" {
			z.err = z.writeString(z.Name)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Comment != "" {
			z.err = z.writeString(z.Comment)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.compressor == nil {
			z.compressor, _ = flate.NewWriter(z.w, z.level)
		}
	}
	z.size += uint32(len(p))
	z.digest = crc32.Update(z.digest, crc32.IEEETable, p)
	n, z.err = z.compressor.Write(p)
	return n, z.err
}
1297408
func (c *Client) CreateGzip(i *CreateGzipInput) (*Gzip, error) {
	if i.Service == "" {
		return nil, ErrMissingService
	}

	if i.Version == 0 {
		return nil, ErrMissingVersion
	}

	path := fmt.Sprintf("/service/%s/version/%d/gzip", i.Service, i.Version)
	resp, err := c.PostForm(path, i, nil)
	if err != nil {
		return nil, err
	}

	var gzip *Gzip
	if err := decodeJSON(&gzip, resp.Body); err != nil {
		return nil, err
	}
	return gzip, nil
}
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)
}
25845
func MarshalGzipped(layers Layers) ([]byte, error) {
	data, err := Marshal(layers)
	if err != nil {
		return nil, err
	}

	buf := bytes.NewBuffer(nil)
	gzwriter := gzip.NewWriter(buf)

	_, err = gzwriter.Write(data)
	if err != nil {
		return nil, errors.WithMessage(err, "failed to write gz data")
	}

	err = gzwriter.Close()
	if err != nil {
		return nil, errors.WithMessage(err, "failed to close gzwriter")
	}

	return buf.Bytes(), nil
}
305254
func GZIPHandler(w http.ResponseWriter, r *http.Request) {
	h, _, _ := net.SplitHostPort(r.RemoteAddr)

	v := gzipResponse{
		headersResponse: headersResponse{getHeaders(r)},
		ipResponse:      ipResponse{h},
		Gzipped:         true,
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Add("Content-Encoding", "gzip")
	ww := gzip.NewWriter(w)
	defer ww.Close() // flush
	if err := writeJSON(ww, v); err != nil {
		writeErrorJSON(w, errors.Wrap(err, "failed to write json"))
	}
}
921096
func GzipHandler(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") || w.Header().Get("Content-Encoding") != "" || w.Header().Get("Vary") != "" {
			h.ServeHTTP(w, r)
			return
		}
		w.Header().Set("Content-Encoding", "gzip")
		w.Header().Set("Vary", "Accept-Encoding")
		gz := gzip.NewWriter(w)
		defer gz.Close()
		h.ServeHTTP(&gzipResponseWriter{
			Writer:         gz,
			ResponseWriter: w,
		}, r)
	})
}
383812
func GzipWalker(file string, tmp io.Writer) error {
	f, err := os.Open(file)

	if err != nil {
		return err
	}

	defer f.Close()

	//gzipper
	gz := gzip.NewWriter(tmp)
	defer gz.Close()

	_, err = io.Copy(gz, f)

	return err
}