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.

134 lines
3.4 KiB

10 years ago
11 years ago
11 years ago
  1. package wire
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "reflect"
  7. . "github.com/tendermint/tendermint/common"
  8. )
  9. // TODO document and maybe make it configurable.
  10. const MaxBinaryReadSize = 21 * 1024 * 1024
  11. var ErrBinaryReadSizeOverflow = errors.New("Error: binary read size overflow")
  12. var ErrBinaryReadSizeUnderflow = errors.New("Error: binary read size underflow")
  13. func ReadBinary(o interface{}, r io.Reader, n *int64, err *error) interface{} {
  14. rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
  15. if rv.Kind() == reflect.Ptr {
  16. if rv.IsNil() {
  17. // This allows ReadBinaryObject() to return a nil pointer,
  18. // if the value read is nil.
  19. rvPtr := reflect.New(rt)
  20. ReadBinaryPtr(rvPtr.Interface(), r, n, err)
  21. return rvPtr.Elem().Interface()
  22. } else {
  23. readReflectBinary(rv, rt, Options{}, r, n, err)
  24. return o
  25. }
  26. } else {
  27. ptrRv := reflect.New(rt)
  28. readReflectBinary(ptrRv.Elem(), rt, Options{}, r, n, err)
  29. return ptrRv.Elem().Interface()
  30. }
  31. }
  32. func ReadBinaryPtr(o interface{}, r io.Reader, n *int64, err *error) interface{} {
  33. rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
  34. if rv.Kind() == reflect.Ptr {
  35. readReflectBinary(rv.Elem(), rt.Elem(), Options{}, r, n, err)
  36. } else {
  37. PanicSanity("ReadBinaryPtr expects o to be a pointer")
  38. }
  39. return o
  40. }
  41. func WriteBinary(o interface{}, w io.Writer, n *int64, err *error) {
  42. rv := reflect.ValueOf(o)
  43. rt := reflect.TypeOf(o)
  44. writeReflectBinary(rv, rt, Options{}, w, n, err)
  45. }
  46. func ReadJSON(o interface{}, bytes []byte, err *error) interface{} {
  47. var object interface{}
  48. *err = json.Unmarshal(bytes, &object)
  49. if *err != nil {
  50. return o
  51. }
  52. return ReadJSONObject(o, object, err)
  53. }
  54. func ReadJSONPtr(o interface{}, bytes []byte, err *error) interface{} {
  55. var object interface{}
  56. *err = json.Unmarshal(bytes, &object)
  57. if *err != nil {
  58. return o
  59. }
  60. return ReadJSONObjectPtr(o, object, err)
  61. }
  62. // o is the ultimate destination, object is the result of json unmarshal
  63. func ReadJSONObject(o interface{}, object interface{}, err *error) interface{} {
  64. rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
  65. if rv.Kind() == reflect.Ptr {
  66. if rv.IsNil() {
  67. // This allows ReadJSONObject() to return a nil pointer
  68. // if the value read is nil.
  69. rvPtr := reflect.New(rt)
  70. ReadJSONObjectPtr(rvPtr.Interface(), object, err)
  71. return rvPtr.Elem().Interface()
  72. } else {
  73. readReflectJSON(rv, rt, object, err)
  74. return o
  75. }
  76. } else {
  77. ptrRv := reflect.New(rt)
  78. readReflectJSON(ptrRv.Elem(), rt, object, err)
  79. return ptrRv.Elem().Interface()
  80. }
  81. }
  82. func ReadJSONObjectPtr(o interface{}, object interface{}, err *error) interface{} {
  83. rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
  84. if rv.Kind() == reflect.Ptr {
  85. readReflectJSON(rv.Elem(), rt.Elem(), object, err)
  86. } else {
  87. PanicSanity("ReadJSON(Object)Ptr expects o to be a pointer")
  88. }
  89. return o
  90. }
  91. func WriteJSON(o interface{}, w io.Writer, n *int64, err *error) {
  92. rv := reflect.ValueOf(o)
  93. rt := reflect.TypeOf(o)
  94. if rv.Kind() == reflect.Ptr {
  95. rv, rt = rv.Elem(), rt.Elem()
  96. }
  97. writeReflectJSON(rv, rt, w, n, err)
  98. }
  99. // Write all of bz to w
  100. // Increment n and set err accordingly.
  101. func WriteTo(bz []byte, w io.Writer, n *int64, err *error) {
  102. if *err != nil {
  103. return
  104. }
  105. n_, err_ := w.Write(bz)
  106. *n += int64(n_)
  107. *err = err_
  108. }
  109. // Read len(buf) from r
  110. // Increment n and set err accordingly.
  111. func ReadFull(buf []byte, r io.Reader, n *int64, err *error) {
  112. if *err != nil {
  113. return
  114. }
  115. n_, err_ := io.ReadFull(r, buf)
  116. *n += int64(n_)
  117. *err = err_
  118. }