bytes.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package util
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "unicode"
  13. )
  14. // BytesToHumanReadable returns the converted human readable representation of the bytes.
  15. func BytesToHumanReadable(b uint64) string {
  16. const unit = 1024
  17. if b < unit {
  18. return fmt.Sprintf("%d B", b)
  19. }
  20. div, exp := uint64(unit), 0
  21. for n := b / unit; n >= unit; n /= unit {
  22. div *= unit
  23. exp++
  24. }
  25. return fmt.Sprintf("%.2f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
  26. }
  27. // big endian
  28. func BytesToUint64(b []byte) (v uint64) {
  29. length := uint(len(b))
  30. for i := uint(0); i < length-1; i++ {
  31. v += uint64(b[i])
  32. v <<= 8
  33. }
  34. v += uint64(b[length-1])
  35. return
  36. }
  37. func BytesToUint32(b []byte) (v uint32) {
  38. length := uint(len(b))
  39. for i := uint(0); i < length-1; i++ {
  40. v += uint32(b[i])
  41. v <<= 8
  42. }
  43. v += uint32(b[length-1])
  44. return
  45. }
  46. func BytesToUint16(b []byte) (v uint16) {
  47. v += uint16(b[0])
  48. v <<= 8
  49. v += uint16(b[1])
  50. return
  51. }
  52. func Uint64toBytes(b []byte, v uint64) {
  53. for i := uint(0); i < 8; i++ {
  54. b[7-i] = byte(v >> (i * 8))
  55. }
  56. }
  57. func Uint32toBytes(b []byte, v uint32) {
  58. for i := uint(0); i < 4; i++ {
  59. b[3-i] = byte(v >> (i * 8))
  60. }
  61. }
  62. func Uint16toBytes(b []byte, v uint16) {
  63. b[0] = byte(v >> 8)
  64. b[1] = byte(v)
  65. }
  66. func Uint8toBytes(b []byte, v uint8) {
  67. b[0] = byte(v)
  68. }
  69. // returns a 64 bit big int
  70. func HashStringToLong(dir string) (v int64) {
  71. h := md5.New()
  72. io.WriteString(h, dir)
  73. b := h.Sum(nil)
  74. v += int64(b[0])
  75. v <<= 8
  76. v += int64(b[1])
  77. v <<= 8
  78. v += int64(b[2])
  79. v <<= 8
  80. v += int64(b[3])
  81. v <<= 8
  82. v += int64(b[4])
  83. v <<= 8
  84. v += int64(b[5])
  85. v <<= 8
  86. v += int64(b[6])
  87. v <<= 8
  88. v += int64(b[7])
  89. return
  90. }
  91. func HashToInt32(data []byte) (v int32) {
  92. h := md5.New()
  93. h.Write(data)
  94. b := h.Sum(nil)
  95. v += int32(b[0])
  96. v <<= 8
  97. v += int32(b[1])
  98. v <<= 8
  99. v += int32(b[2])
  100. v <<= 8
  101. v += int32(b[3])
  102. return
  103. }
  104. func Base64Encode(data []byte) string {
  105. return base64.StdEncoding.EncodeToString(data)
  106. }
  107. func Base64Md5(data []byte) string {
  108. return Base64Encode(Md5(data))
  109. }
  110. func Md5(data []byte) []byte {
  111. hash := md5.New()
  112. hash.Write(data)
  113. return hash.Sum(nil)
  114. }
  115. func Md5String(data []byte) string {
  116. return fmt.Sprintf("%x", Md5(data))
  117. }
  118. func Base64Md5ToBytes(contentMd5 string) []byte {
  119. data, err := base64.StdEncoding.DecodeString(contentMd5)
  120. if err != nil {
  121. return nil
  122. }
  123. return data
  124. }
  125. func RandomInt32() int32 {
  126. buf := make([]byte, 4)
  127. rand.Read(buf)
  128. return int32(BytesToUint32(buf))
  129. }
  130. func RandomUint64() int32 {
  131. buf := make([]byte, 8)
  132. rand.Read(buf)
  133. return int32(BytesToUint64(buf))
  134. }
  135. func RandomBytes(byteCount int) []byte {
  136. buf := make([]byte, byteCount)
  137. rand.Read(buf)
  138. return buf
  139. }
  140. type BytesReader struct {
  141. Bytes []byte
  142. *bytes.Reader
  143. }
  144. func NewBytesReader(b []byte) *BytesReader {
  145. return &BytesReader{
  146. Bytes: b,
  147. Reader: bytes.NewReader(b),
  148. }
  149. }
  150. // EmptyTo returns to if s is empty.
  151. func EmptyTo(s, to string) string {
  152. if s == "" {
  153. return to
  154. }
  155. return s
  156. }
  157. // IfElse works like b ? this : that.
  158. func IfElse(b bool, this, that string) string {
  159. if b {
  160. return this
  161. }
  162. return that
  163. }
  164. // ParseBytes parses a string representation of bytes into the number
  165. // of bytes it represents.
  166. //
  167. // See Also: Bytes, IBytes.
  168. //
  169. // ParseBytes("42MB") -> 42000000, nil
  170. // ParseBytes("42 MB") -> 42000000, nil
  171. // ParseBytes("42 mib") -> 44040192, nil
  172. func ParseBytes(s string) (uint64, error) {
  173. lastDigit := 0
  174. hasComma := false
  175. for _, r := range s {
  176. if !(unicode.IsDigit(r) || r == '.' || r == ',') {
  177. break
  178. }
  179. if r == ',' {
  180. hasComma = true
  181. }
  182. lastDigit++
  183. }
  184. num := s[:lastDigit]
  185. if hasComma {
  186. num = strings.Replace(num, ",", "", -1)
  187. }
  188. f, err := strconv.ParseFloat(num, 64)
  189. if err != nil {
  190. return 0, err
  191. }
  192. extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
  193. if m, ok := bytesSizeTable[extra]; ok {
  194. f *= float64(m)
  195. if f >= math.MaxUint64 {
  196. return 0, fmt.Errorf("too large: %v", s)
  197. }
  198. return uint64(f), nil
  199. }
  200. return 0, fmt.Errorf("unhandled size name: %v", extra)
  201. }
  202. var bytesSizeTable = map[string]uint64{
  203. "b": Byte, "kib": KiByte, "kb": KByte, "mib": MiByte, "mb": MByte, "gib": GiByte, "gb": GByte,
  204. "tib": TiByte, "tb": TByte, "pib": PiByte, "pb": PByte, "eib": EiByte, "eb": EByte,
  205. // Without suffix
  206. "": Byte, "ki": KiByte, "k": KByte, "mi": MiByte, "m": MByte, "gi": GiByte, "g": GByte,
  207. "ti": TiByte, "t": TByte, "pi": PiByte, "p": PByte, "ei": EiByte, "e": EByte,
  208. }
  209. // IEC Sizes.
  210. // kibis of bits
  211. const (
  212. Byte = 1 << (iota * 10)
  213. KiByte
  214. MiByte
  215. GiByte
  216. TiByte
  217. PiByte
  218. EiByte
  219. )
  220. // SI Sizes.
  221. const (
  222. IByte = 1
  223. KByte = IByte * 1000
  224. MByte = KByte * 1000
  225. GByte = MByte * 1000
  226. TByte = GByte * 1000
  227. PByte = TByte * 1000
  228. EByte = PByte * 1000
  229. )