Fetch the repository succeeded.
package compress
import (
"bytes"
"compress/zlib"
"io"
"io/ioutil"
"github.com/xiaonanln/goworld/engine/gwioutil"
)
func NewZlibCompressor() Compressor {
fc := &zlibCompressor{}
var err error
fc.reader = newZlibReader()
if err != nil {
panic(err)
}
fc.writer, err = zlib.NewWriterLevel(ioutil.Discard, zlib.BestSpeed)
if err != nil {
panic(err)
}
return fc
}
func newZlibReader() (reader io.ReadCloser) {
w := bytes.NewBuffer(nil)
cw := zlib.NewWriter(w)
cw.Write(nil)
cw.Flush()
reader, _ = zlib.NewReader(bytes.NewReader(w.Bytes()))
return
}
type zlibCompressor struct {
writer *zlib.Writer
reader io.ReadCloser
}
func (fc *zlibCompressor) Compress(b []byte, c []byte) ([]byte, error) {
wb := bytes.NewBuffer(c)
fc.writer.Reset(wb)
n, err := fc.writer.Write(b)
if err != nil {
return nil, err
}
if n != len(b) {
return nil, errNotFullyCompressed
}
fc.writer.Flush()
return wb.Bytes(), nil
}
func (fc *zlibCompressor) Decompress(c []byte, b []byte) error {
fc.reader.(zlib.Resetter).Reset(bytes.NewReader(c), nil)
return gwioutil.ReadAll(fc.reader, b)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。