/
bytes.go
85 lines (76 loc) · 1.84 KB
/
bytes.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package utils
import (
"bytes"
"compress/gzip"
"compress/zlib"
"encoding/binary"
"io"
)
func ZlibCompress(data []byte) []byte {
var in bytes.Buffer
w := zlib.NewWriter(&in)
w.Write(data)
w.Close()
return in.Bytes()
}
func ZlibUnCompress(data []byte) ([]byte, error) {
b := bytes.NewReader(data)
r, _ := zlib.NewReader(b)
defer r.Close()
undatas, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return undatas, nil
}
func GZipCompress(data []byte) []byte {
var in bytes.Buffer
w := gzip.NewWriter(&in)
w.Write(data)
w.Close()
return in.Bytes()
}
func GZipUnCompress(data []byte) ([]byte, error) {
b := bytes.NewReader(data)
r, _ := gzip.NewReader(b)
defer r.Close()
undatas, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return undatas, nil
}
//IntToBytes 整形转换成字节
func IntToBytes(n interface{}) ([]byte, error) {
var err error
bytesBuffer := bytes.NewBuffer([]byte{})
if v, ok := n.(int); ok {
err = binary.Write(bytesBuffer, binary.BigEndian, int32(v))
} else if v, ok := n.(float64); ok {
err = binary.Write(bytesBuffer, binary.BigEndian, v)
} else {
err = binary.Write(bytesBuffer, binary.BigEndian, n)
}
if err != nil {
return nil, err
} else {
return bytesBuffer.Bytes(), nil
}
}
//IntToBuffer 将数字写入BUFFER, buffer := bytes.NewBuffer([]byte{})
func IntToBuffer(buffer *bytes.Buffer, n interface{}) error {
if v, ok := n.(int); ok {
return binary.Write(buffer, binary.BigEndian, int32(v))
} else if v, ok := n.(float64); ok {
return binary.Write(buffer, binary.BigEndian, v)
} else {
return binary.Write(buffer, binary.BigEndian, n)
}
}
//BytesToInt 字节转换成整形,n 必须是指针
// var a int32
// BytesToInt([]byte{1},&a)
func BytesToInt(b []byte, n interface{}) error {
bytesBuffer := bytes.NewBuffer(b)
return binary.Read(bytesBuffer, binary.BigEndian, n)
}