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.

123 lines
3.0 KiB

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