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.

114 lines
2.6 KiB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
  1. package binary
  2. import (
  3. "bytes"
  4. "time"
  5. )
  6. type Codec interface {
  7. Write(interface{}) ([]byte, error)
  8. Read([]byte) (interface{}, error)
  9. }
  10. const (
  11. typeNil = byte(0x00)
  12. typeByte = byte(0x01)
  13. typeInt8 = byte(0x02)
  14. // typeUInt8 = byte(0x03)
  15. typeInt16 = byte(0x04)
  16. typeUInt16 = byte(0x05)
  17. typeInt32 = byte(0x06)
  18. typeUInt32 = byte(0x07)
  19. typeInt64 = byte(0x08)
  20. typeUInt64 = byte(0x09)
  21. typeString = byte(0x10)
  22. typeByteSlice = byte(0x11)
  23. typeTime = byte(0x20)
  24. )
  25. var BasicCodec = basicCodec{}
  26. type basicCodec struct{}
  27. func (bc basicCodec) Write(o interface{}) ([]byte, error) {
  28. n, err, w := new(int64), new(error), new(bytes.Buffer)
  29. switch o.(type) {
  30. case nil:
  31. WriteByte(w, typeNil, n, err)
  32. case byte:
  33. WriteByte(w, typeByte, n, err)
  34. WriteByte(w, o.(byte), n, err)
  35. case int8:
  36. WriteByte(w, typeInt8, n, err)
  37. WriteInt8(w, o.(int8), n, err)
  38. //case uint8:
  39. // WriteByte(w, typeUInt8, n, err)
  40. // WriteUInt8(w, o.(uint8), n, err)
  41. case int16:
  42. WriteByte(w, typeInt16, n, err)
  43. WriteInt16(w, o.(int16), n, err)
  44. case uint16:
  45. WriteByte(w, typeUInt16, n, err)
  46. WriteUInt16(w, o.(uint16), n, err)
  47. case int32:
  48. WriteByte(w, typeInt32, n, err)
  49. WriteInt32(w, o.(int32), n, err)
  50. case uint32:
  51. WriteByte(w, typeUInt32, n, err)
  52. WriteUInt32(w, o.(uint32), n, err)
  53. case int64:
  54. WriteByte(w, typeInt64, n, err)
  55. WriteInt64(w, o.(int64), n, err)
  56. case uint64:
  57. WriteByte(w, typeUInt64, n, err)
  58. WriteUInt64(w, o.(uint64), n, err)
  59. case string:
  60. WriteByte(w, typeString, n, err)
  61. WriteString(w, o.(string), n, err)
  62. case []byte:
  63. WriteByte(w, typeByteSlice, n, err)
  64. WriteByteSlice(w, o.([]byte), n, err)
  65. case time.Time:
  66. WriteByte(w, typeTime, n, err)
  67. WriteTime(w, o.(time.Time), n, err)
  68. default:
  69. panic("Unsupported type")
  70. }
  71. return w.Bytes(), *err
  72. }
  73. func (bc basicCodec) Read(bz []byte) (interface{}, error) {
  74. n, err, r, o := new(int64), new(error), bytes.NewBuffer(bz), interface{}(nil)
  75. type_ := ReadByte(r, n, err)
  76. switch type_ {
  77. case typeNil:
  78. o = nil
  79. case typeByte:
  80. o = ReadByte(r, n, err)
  81. case typeInt8:
  82. o = ReadInt8(r, n, err)
  83. //case typeUInt8:
  84. // o = ReadUInt8(r, n, err)
  85. case typeInt16:
  86. o = ReadInt16(r, n, err)
  87. case typeUInt16:
  88. o = ReadUInt16(r, n, err)
  89. case typeInt32:
  90. o = ReadInt32(r, n, err)
  91. case typeUInt32:
  92. o = ReadUInt32(r, n, err)
  93. case typeInt64:
  94. o = ReadInt64(r, n, err)
  95. case typeUInt64:
  96. o = ReadUInt64(r, n, err)
  97. case typeString:
  98. o = ReadString(r, n, err)
  99. case typeByteSlice:
  100. o = ReadByteSlice(r, n, err)
  101. case typeTime:
  102. o = ReadTime(r, n, err)
  103. default:
  104. panic("Unsupported type")
  105. }
  106. return o, *err
  107. }