-
Notifications
You must be signed in to change notification settings - Fork 0
/
f.go
47 lines (42 loc) · 1.1 KB
/
f.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"bytes"
"compress/zlib"
"github.com/golang/snappy"
)
func Compress(srcBuffer *Dm_build_0, offset int, length int, compressID int) ([]byte, error) {
if compressID == Dm_build_682 {
return snappy.Encode(nil, srcBuffer.Dm_build_290(offset, length)), nil
}
return GzlibCompress(srcBuffer, offset, length)
}
func UnCompress(srcBytes []byte, compressID int) ([]byte, error) {
if compressID == Dm_build_682 {
return snappy.Decode(nil, srcBytes)
}
return GzlibUncompress(srcBytes)
}
func GzlibCompress(srcBuffer *Dm_build_0, offset int, length int) ([]byte, error) {
var ret bytes.Buffer
var w = zlib.NewWriter(&ret)
w.Write(srcBuffer.Dm_build_290(offset, length))
w.Close()
return ret.Bytes(), nil
}
func GzlibUncompress(srcBytes []byte) ([]byte, error) {
var bytesBuf = new(bytes.Buffer)
r, err := zlib.NewReader(bytes.NewReader(srcBytes))
if err != nil {
return nil, err
}
defer r.Close()
_, err = bytesBuf.ReadFrom(r)
if err != nil {
return nil, err
}
return bytesBuf.Bytes(), nil
}