tools.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package rest
  2. import (
  3. "io"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. )
  6. // конверторы
  7. // Int64ToBytes упаковывает int64 в срез байтов заданной длины
  8. func Int64ToBytes(num int64, byteCount int) []byte {
  9. bytes := make([]byte, byteCount)
  10. for i := 0; i < byteCount; i++ {
  11. shift := uint((byteCount - 1 - i) * 8)
  12. bytes[i] = byte(num >> shift)
  13. }
  14. return bytes
  15. }
  16. // BytesToInt64 конвертирует срез байтов в int64
  17. func BytesToInt64(bytes []byte) int64 {
  18. var num int64
  19. for _, b := range bytes {
  20. num = (num << 8) | int64(b)
  21. }
  22. return num
  23. }
  24. func ioError(field string, err error) IErrorArgs {
  25. return NewError(
  26. "ErrIO",
  27. json.Map{
  28. "field": field,
  29. "error": err.Error(),
  30. },
  31. )
  32. }
  33. // чтение
  34. func ReadBuf(r io.Reader, size int, field string) ([]byte, IErrorArgs) {
  35. buf := make([]byte, size)
  36. if _, err := r.Read(buf); err != nil {
  37. return nil, ioError(field, err)
  38. }
  39. return buf, nil
  40. }
  41. func ReadBufSize(r io.Reader, lenSize int, field string) ([]byte, IErrorArgs) {
  42. var size int64
  43. if err := ReadInt64(r, lenSize, field+"_size", &size); err != nil {
  44. return nil, err
  45. }
  46. buf, err := ReadBuf(r, int(size), field)
  47. return buf, err
  48. }
  49. func ReadInt64(r io.Reader, size int, field string, result *int64) IErrorArgs {
  50. buf := make([]byte, size)
  51. if _, err := r.Read(buf); err != nil {
  52. return ioError(field, err)
  53. }
  54. *result = BytesToInt64(buf)
  55. return nil
  56. }
  57. func ReadString(r io.Reader, lenSize int, field string, result *string) IErrorArgs {
  58. strBuf, err := ReadBufSize(r, lenSize, field)
  59. if err != nil {
  60. return err
  61. }
  62. *result = string(strBuf)
  63. return nil
  64. }
  65. func ReadByte(r io.Reader, field string, result *byte) IErrorArgs {
  66. buf, err := ReadBuf(r, 1, field)
  67. if err != nil {
  68. return err
  69. }
  70. *result = buf[0]
  71. return nil
  72. }
  73. // запись
  74. func WriteBuf(w io.Writer, buf []byte, field string) IErrorArgs {
  75. if _, err := w.Write(buf); err != nil {
  76. return ioError(field, err)
  77. }
  78. return nil
  79. }
  80. func WriteInt64(w io.Writer, val int64, size int, field string) IErrorArgs {
  81. buf := Int64ToBytes(val, size)
  82. return WriteBuf(w, buf, field)
  83. }
  84. func WriteString(w io.Writer, val, field string, lenSize int) IErrorArgs {
  85. // длина
  86. if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
  87. return err
  88. }
  89. // строка
  90. if len(val) == 0 {
  91. return nil
  92. }
  93. return WriteBuf(w, []byte(val), field)
  94. }
  95. func WriteByte(w io.Writer, val byte, field string) IErrorArgs {
  96. return WriteBuf(w, []byte{val}, field)
  97. }
  98. func WriteBufSize(w io.Writer, val []byte, lenSize int, field string) IErrorArgs {
  99. // длина
  100. if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
  101. return err
  102. }
  103. // буфер
  104. return WriteBuf(w, val, field)
  105. }