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.

96 lines
2.6 KiB

  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Modified to return number of bytes written by Writer.WriteMsg(), and added byteReader.
  30. package protoio
  31. import (
  32. "io"
  33. "github.com/gogo/protobuf/proto"
  34. )
  35. type Writer interface {
  36. WriteMsg(proto.Message) (int, error)
  37. }
  38. type WriteCloser interface {
  39. Writer
  40. io.Closer
  41. }
  42. type Reader interface {
  43. ReadMsg(msg proto.Message) error
  44. }
  45. type ReadCloser interface {
  46. Reader
  47. io.Closer
  48. }
  49. type marshaler interface {
  50. MarshalTo(data []byte) (n int, err error)
  51. }
  52. func getSize(v interface{}) (int, bool) {
  53. if sz, ok := v.(interface {
  54. Size() (n int)
  55. }); ok {
  56. return sz.Size(), true
  57. } else if sz, ok := v.(interface {
  58. ProtoSize() (n int)
  59. }); ok {
  60. return sz.ProtoSize(), true
  61. } else {
  62. return 0, false
  63. }
  64. }
  65. // byteReader wraps an io.Reader and implements io.ByteReader. Reading one byte at a
  66. // time is extremely slow, but this is what Amino did already, and the caller can
  67. // wrap the reader in bufio.Reader if appropriate.
  68. type byteReader struct {
  69. io.Reader
  70. bytes []byte
  71. }
  72. func newByteReader(r io.Reader) *byteReader {
  73. return &byteReader{
  74. Reader: r,
  75. bytes: make([]byte, 1),
  76. }
  77. }
  78. func (r *byteReader) ReadByte() (byte, error) {
  79. _, err := r.Read(r.bytes)
  80. if err != nil {
  81. return 0, err
  82. }
  83. return r.bytes[0], nil
  84. }