diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2016-01-04 15:26:52 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2016-01-04 15:27:13 +0200 |
commit | a89d890c8c2cede1325eb1c9301e7289f54a2988 (patch) | |
tree | c25287893082ea82bf6aea822c833b899b8f7913 /compress.go | |
parent | Transparently gunzip multipart form data on Request.MultipartForm() call acco... (diff) | |
download | fasthttp-a89d890c8c2cede1325eb1c9301e7289f54a2988.tar.gz fasthttp-a89d890c8c2cede1325eb1c9301e7289f54a2988.tar.bz2 fasthttp-a89d890c8c2cede1325eb1c9301e7289f54a2988.zip |
Added WriteGzip and WriteGunzip helper functions
Diffstat (limited to 'compress.go')
-rw-r--r-- | compress.go | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/compress.go b/compress.go index f619cdd..1da5132 100644 --- a/compress.go +++ b/compress.go @@ -119,10 +119,30 @@ var gzipWriterPoolMap = func() map[int]*sync.Pool { // * CompressDefaultCompression func AppendGzipBytesLevel(dst, src []byte, level int) []byte { w := &byteSliceWriter{dst} + WriteGzipLevel(w, src, level) + return w.b +} + +// WriteGzip writes gzipped p to w using the given compression level +// and returns the number of compressed bytes written to w. +// +// Supported compression levels are: +// +// * CompressNoCompression +// * CompressBestSpeed +// * CompressBestCompression +// * CompressDefaultCompression +func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) { zw := acquireGzipWriter(w, level) - zw.Write(src) + n, err := zw.Write(p) releaseGzipWriter(zw) - return w.b + return n, err +} + +// WriteGzipLevel writes gzipped p to w and returns the number of compressed +// bytes written to w. +func WriteGzip(w io.Writer, p []byte) (int, error) { + return WriteGzipLevel(w, p, CompressDefaultCompression) } // AppendGzipBytes appends gzipped src to dst and returns the resulting dst. @@ -130,16 +150,27 @@ func AppendGzipBytes(dst, src []byte) []byte { return AppendGzipBytesLevel(dst, src, CompressDefaultCompression) } -// AppendGunzipBytes append gunzipped src to dst and returns the resulting dst. -func AppendGunzipBytes(dst, src []byte) ([]byte, error) { - r := &byteSliceReader{src} +// WriteGunzip writes ungzipped p to w and returns the number of uncompressed +// bytes written to w. +func WriteGunzip(w io.Writer, p []byte) (int, error) { + r := &byteSliceReader{p} zr, err := acquireGzipReader(r) if err != nil { - return dst, err + return 0, err } - w := &byteSliceWriter{dst} - _, err = io.Copy(w, zr) + n, err := io.Copy(w, zr) releaseGzipReader(zr) + nn := int(n) + if int64(nn) != n { + return 0, fmt.Errorf("too much data gunzipped: %d", n) + } + return nn, err +} + +// AppendGunzipBytes append gunzipped src to dst and returns the resulting dst. +func AppendGunzipBytes(dst, src []byte) ([]byte, error) { + w := &byteSliceWriter{dst} + _, err := WriteGunzip(w, src) return w.b, err } |