-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
64 lines (56 loc) · 1.11 KB
/
utils.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
package p2p
import (
"fmt"
"io"
)
func checkEqual(ref, current []byte) bool {
for i := 0; i < len(current); i++ {
if ref[i] != current[i] {
return false
}
}
return true
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func uint32ToBytes(buf []byte, n uint32) {
buf[0] = byte(n >> 24)
buf[1] = byte(n >> 16)
buf[2] = byte(n >> 8)
buf[3] = byte(n)
}
func bytesToUint32(buf []byte) uint32 {
return (uint32(buf[0]) << 24) |
(uint32(buf[1]) << 16) |
(uint32(buf[2]) << 8) | uint32(buf[3])
}
func writeNBOUint32(w io.Writer, n uint32) (err error) {
buf := make([]byte, 4)
uint32ToBytes(buf, n)
_, err = w.Write(buf[0:])
return
}
func readNBOUint32(r io.Reader) (n uint32, err error) {
var buf [4]byte
_, err = r.Read(buf[0:])
if err != nil {
return
}
n = bytesToUint32(buf[0:])
return
}
func humanSize(value float64) string {
switch {
case value > 1<<30:
return fmt.Sprintf("%.2f GB", value/(1<<30))
case value > 1<<20:
return fmt.Sprintf("%.2f MB", value/(1<<20))
case value > 1<<10:
return fmt.Sprintf("%.2f kB", value/(1<<10))
}
return fmt.Sprintf("%.2f B", value)
}