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.

138 lines
3.3 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import (
  3. "errors"
  4. "io"
  5. "time"
  6. )
  7. type Codec interface {
  8. WriteTo(io.Writer, interface{}, *int64, *error)
  9. ReadFrom(io.Reader, *int64, *error) interface{}
  10. }
  11. //-----------------------------------------------------------------------------
  12. const (
  13. typeNil = byte(0x00)
  14. typeByte = byte(0x01)
  15. typeInt8 = byte(0x02)
  16. // typeUInt8 = byte(0x03)
  17. typeInt16 = byte(0x04)
  18. typeUInt16 = byte(0x05)
  19. typeInt32 = byte(0x06)
  20. typeUInt32 = byte(0x07)
  21. typeInt64 = byte(0x08)
  22. typeUInt64 = byte(0x09)
  23. typeString = byte(0x10)
  24. typeByteSlice = byte(0x11)
  25. typeTime = byte(0x20)
  26. )
  27. var BasicCodec = basicCodec{}
  28. type basicCodec struct{}
  29. func (bc basicCodec) WriteTo(w io.Writer, o interface{}, n *int64, err *error) {
  30. switch o.(type) {
  31. case nil:
  32. WriteByte(w, typeNil, n, err)
  33. case byte:
  34. WriteByte(w, typeByte, n, err)
  35. WriteByte(w, o.(byte), n, err)
  36. case int8:
  37. WriteByte(w, typeInt8, n, err)
  38. WriteInt8(w, o.(int8), n, err)
  39. //case uint8:
  40. // WriteByte(w, typeUInt8, n, err)
  41. // WriteUInt8(w, o.(uint8), n, err)
  42. case int16:
  43. WriteByte(w, typeInt16, n, err)
  44. WriteInt16(w, o.(int16), n, err)
  45. case uint16:
  46. WriteByte(w, typeUInt16, n, err)
  47. WriteUInt16(w, o.(uint16), n, err)
  48. case int32:
  49. WriteByte(w, typeInt32, n, err)
  50. WriteInt32(w, o.(int32), n, err)
  51. case uint32:
  52. WriteByte(w, typeUInt32, n, err)
  53. WriteUInt32(w, o.(uint32), n, err)
  54. case int64:
  55. WriteByte(w, typeInt64, n, err)
  56. WriteInt64(w, o.(int64), n, err)
  57. case uint64:
  58. WriteByte(w, typeUInt64, n, err)
  59. WriteUInt64(w, o.(uint64), n, err)
  60. case string:
  61. WriteByte(w, typeString, n, err)
  62. WriteString(w, o.(string), n, err)
  63. case []byte:
  64. WriteByte(w, typeByteSlice, n, err)
  65. WriteByteSlice(w, o.([]byte), n, err)
  66. case time.Time:
  67. WriteByte(w, typeTime, n, err)
  68. WriteTime(w, o.(time.Time), n, err)
  69. default:
  70. panic("Unsupported type")
  71. }
  72. return
  73. }
  74. func (bc basicCodec) ReadFrom(r io.Reader, n *int64, err *error) interface{} {
  75. type_ := ReadByte(r, n, err)
  76. switch type_ {
  77. case typeNil:
  78. return nil
  79. case typeByte:
  80. return ReadByte(r, n, err)
  81. case typeInt8:
  82. return ReadInt8(r, n, err)
  83. //case typeUInt8:
  84. // return ReadUInt8(r, n, err)
  85. case typeInt16:
  86. return ReadInt16(r, n, err)
  87. case typeUInt16:
  88. return ReadUInt16(r, n, err)
  89. case typeInt32:
  90. return ReadInt32(r, n, err)
  91. case typeUInt32:
  92. return ReadUInt32(r, n, err)
  93. case typeInt64:
  94. return ReadInt64(r, n, err)
  95. case typeUInt64:
  96. return ReadUInt64(r, n, err)
  97. case typeString:
  98. return ReadString(r, n, err)
  99. case typeByteSlice:
  100. return ReadByteSlice(r, n, err)
  101. case typeTime:
  102. return ReadTime(r, n, err)
  103. default:
  104. panic("Unsupported type")
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Creates an adapter codec for Binary things.
  109. // Resulting Codec can be used with merkle/*.
  110. type BinaryCodec struct {
  111. decoder func(io.Reader, *int64, *error) interface{}
  112. }
  113. func NewBinaryCodec(decoder func(io.Reader, *int64, *error) interface{}) *BinaryCodec {
  114. return &BinaryCodec{decoder}
  115. }
  116. func (ca *BinaryCodec) WriteTo(w io.Writer, o interface{}, n *int64, err *error) {
  117. if bo, ok := o.(Binary); ok {
  118. WriteTo(w, BinaryBytes(bo), n, err)
  119. } else {
  120. *err = errors.New("BinaryCodec expected Binary object")
  121. }
  122. }
  123. func (ca *BinaryCodec) ReadFrom(r io.Reader, n *int64, err *error) interface{} {
  124. return ca.decoder(r, n, err)
  125. }