-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
88 lines (74 loc) · 2.08 KB
/
base.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
86
87
88
package encoding
import (
"github.com/chinaDL/whTools/os/gfile"
"github.com/chinaDL/whTools/utils"
"io"
)
const (
MaxFileBlock = 1 * 1024 * 1024
)
type BaseStruct struct {
src []byte
dst []byte
Err error
isBigFile bool
filePath string
}
// String implements the interface Stringer for encode struct.
// 实现 Stringer 接口
func (e BaseStruct) String() string {
return e.ToString()
}
// ToString outputs as string.
// 输出字符串
func (e BaseStruct) ToString() string {
return utils.Bytes2String(e.dst)
}
// ToBytes outputs as byte slice.
// 输出字节切片
func (e BaseStruct) ToBytes() []byte {
if len(e.dst) == 0 {
return []byte("")
}
return e.dst
}
func (e BaseStruct) sum(w io.Writer) {
if e.isBigFile {
_ = gfile.ReadDo(e.filePath, func(cBuf []byte) error {
_, err := w.Write(cBuf)
return err
})
} else {
_, _ = w.Write(e.src)
}
}
// ToHexString outputs as string with hex encoding.
// 输出经过 hex 编码的字符串
func (e BaseStruct) ToHexString() string {
return NewEncode().FromBytes(e.dst).ByHex().ToString()
}
// ToBase32String outputs as string with base32 encoding.
// 输出经过 base32 编码的字符串
func (e BaseStruct) ToBase32String() string {
return NewEncode().FromBytes(e.dst).ByBase32().ToString()
}
// ToBase64String outputs as string with base64 encoding.
// 输出经过 base64 编码的字符串
func (e BaseStruct) ToBase64String() string {
return NewEncode().FromBytes(e.dst).ByBase64().ToString()
}
// ToHexBytes outputs as byte with hex encoding.
// 输出经过 hex 编码的字节切片
func (e BaseStruct) ToHexBytes() []byte {
return NewEncode().FromBytes(e.dst).ByHex().ToBytes()
}
// ToBase32Bytes outputs as byte slice with base32 encoding.
// 输出经过 base32 编码的字节切片
func (e BaseStruct) ToBase32Bytes() []byte {
return NewEncode().FromBytes(e.dst).ByBase32().ToBytes()
}
// ToBase64Bytes outputs as byte slice with base64 encoding.
// 输出经过 base64 编码的字节切片
func (e BaseStruct) ToBase64Bytes() []byte {
return NewEncode().FromBytes(e.dst).ByBase64().ToBytes()
}