You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
1.6 KiB

11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
  1. package binary
  2. import (
  3. "io"
  4. )
  5. const (
  6. TYPE_NIL = Byte(0x00)
  7. TYPE_BYTE = Byte(0x01)
  8. TYPE_INT8 = Byte(0x02)
  9. TYPE_UINT8 = Byte(0x03)
  10. TYPE_INT16 = Byte(0x04)
  11. TYPE_UINT16 = Byte(0x05)
  12. TYPE_INT32 = Byte(0x06)
  13. TYPE_UINT32 = Byte(0x07)
  14. TYPE_INT64 = Byte(0x08)
  15. TYPE_UINT64 = Byte(0x09)
  16. TYPE_STRING = Byte(0x10)
  17. TYPE_BYTESLICE = Byte(0x11)
  18. TYPE_TIME = Byte(0x20)
  19. )
  20. func GetBinaryType(o Binary) Byte {
  21. switch o.(type) {
  22. case nil:
  23. return TYPE_NIL
  24. case Byte:
  25. return TYPE_BYTE
  26. case Int8:
  27. return TYPE_INT8
  28. case UInt8:
  29. return TYPE_UINT8
  30. case Int16:
  31. return TYPE_INT16
  32. case UInt16:
  33. return TYPE_UINT16
  34. case Int32:
  35. return TYPE_INT32
  36. case UInt32:
  37. return TYPE_UINT32
  38. case Int64:
  39. return TYPE_INT64
  40. case UInt64:
  41. return TYPE_UINT64
  42. case Int:
  43. panic("Int not supported")
  44. case UInt:
  45. panic("UInt not supported")
  46. case String:
  47. return TYPE_STRING
  48. case ByteSlice:
  49. return TYPE_BYTESLICE
  50. case Time:
  51. return TYPE_TIME
  52. default:
  53. panic("Unsupported type")
  54. }
  55. }
  56. func ReadBinary(r io.Reader) Binary {
  57. type_ := ReadByte(r)
  58. switch type_ {
  59. case TYPE_NIL:
  60. return nil
  61. case TYPE_BYTE:
  62. return ReadByte(r)
  63. case TYPE_INT8:
  64. return ReadInt8(r)
  65. case TYPE_UINT8:
  66. return ReadUInt8(r)
  67. case TYPE_INT16:
  68. return ReadInt16(r)
  69. case TYPE_UINT16:
  70. return ReadUInt16(r)
  71. case TYPE_INT32:
  72. return ReadInt32(r)
  73. case TYPE_UINT32:
  74. return ReadUInt32(r)
  75. case TYPE_INT64:
  76. return ReadInt64(r)
  77. case TYPE_UINT64:
  78. return ReadUInt64(r)
  79. case TYPE_STRING:
  80. return ReadString(r)
  81. case TYPE_BYTESLICE:
  82. return ReadByteSlice(r)
  83. case TYPE_TIME:
  84. return ReadTime(r)
  85. default:
  86. panic("Unsupported type")
  87. }
  88. }