mirror of
https://github.com/chrislusf/seaweedfs
synced 2025-06-30 16:52:46 +02:00
Compare commits
No commits in common. "1b1ab331f60c7ee4bb127d237620e0152606187c" and "05fc7db755c1beeb5188a65c85288a870e34c16e" have entirely different histories.
1b1ab331f6
...
05fc7db755
2 changed files with 21 additions and 64 deletions
|
@ -2,7 +2,10 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/flate"
|
||||||
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
|
@ -39,21 +42,17 @@ func MaybeDecompressData(input []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GzipData(input []byte) ([]byte, error) {
|
func GzipData(input []byte) ([]byte, error) {
|
||||||
w := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
_, err := GzipStream(w, bytes.NewReader(input))
|
w, _ := gzip.NewWriterLevel(buf, flate.BestSpeed)
|
||||||
if err != nil {
|
if _, err := w.Write(input); err != nil {
|
||||||
|
glog.V(2).Infof("error gzip data: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return w.Bytes(), nil
|
if err := w.Close(); err != nil {
|
||||||
}
|
glog.V(2).Infof("error closing gzipped data: %v", err)
|
||||||
|
|
||||||
func ungzipData(input []byte) ([]byte, error) {
|
|
||||||
w := new(bytes.Buffer)
|
|
||||||
_, err := GunzipStream(w, bytes.NewReader(input))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return w.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecompressData(input []byte) ([]byte, error) {
|
func DecompressData(input []byte) ([]byte, error) {
|
||||||
|
@ -68,6 +67,17 @@ func DecompressData(input []byte) ([]byte, error) {
|
||||||
return input, UnsupportedCompression
|
return input, UnsupportedCompression
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ungzipData(input []byte) ([]byte, error) {
|
||||||
|
buf := bytes.NewBuffer(input)
|
||||||
|
r, _ := gzip.NewReader(buf)
|
||||||
|
defer r.Close()
|
||||||
|
output, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
glog.V(2).Infof("error ungzip data: %v", err)
|
||||||
|
}
|
||||||
|
return output, err
|
||||||
|
}
|
||||||
|
|
||||||
func IsGzippedContent(data []byte) bool {
|
func IsGzippedContent(data []byte) bool {
|
||||||
if len(data) < 2 {
|
if len(data) < 2 {
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"compress/gzip"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
gzipReaderPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return new(gzip.Reader)
|
|
||||||
//return gzip.NewReader()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
gzipWriterPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
w, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed)
|
|
||||||
return w
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func GzipStream(w io.Writer, r io.Reader) (int64, error) {
|
|
||||||
gw, ok := gzipWriterPool.Get().(*gzip.Writer)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("gzip: new writer error")
|
|
||||||
}
|
|
||||||
gw.Reset(w)
|
|
||||||
defer func() {
|
|
||||||
gw.Close()
|
|
||||||
gzipWriterPool.Put(gw)
|
|
||||||
}()
|
|
||||||
return io.Copy(gw, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GunzipStream(w io.Writer, r io.Reader) (int64, error) {
|
|
||||||
gr, ok := gzipReaderPool.Get().(*gzip.Reader)
|
|
||||||
if !ok {
|
|
||||||
return 0, fmt.Errorf("gzip: new reader error")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := gr.Reset(r); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
gr.Close()
|
|
||||||
gzipReaderPool.Put(gr)
|
|
||||||
}()
|
|
||||||
return io.Copy(w, gr)
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue