full.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. package io
  29. import (
  30. "github.com/gogo/protobuf/proto"
  31. "io"
  32. )
  33. func NewFullWriter(w io.Writer) WriteCloser {
  34. return &fullWriter{w, nil}
  35. }
  36. type fullWriter struct {
  37. w io.Writer
  38. buffer []byte
  39. }
  40. func (this *fullWriter) WriteMsg(msg proto.Message) (err error) {
  41. var data []byte
  42. if m, ok := msg.(marshaler); ok {
  43. n, ok := getSize(m)
  44. if !ok {
  45. data, err = proto.Marshal(msg)
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. if n >= len(this.buffer) {
  51. this.buffer = make([]byte, n)
  52. }
  53. _, err = m.MarshalTo(this.buffer)
  54. if err != nil {
  55. return err
  56. }
  57. data = this.buffer[:n]
  58. } else {
  59. data, err = proto.Marshal(msg)
  60. if err != nil {
  61. return err
  62. }
  63. }
  64. _, err = this.w.Write(data)
  65. return err
  66. }
  67. func (this *fullWriter) Close() error {
  68. if closer, ok := this.w.(io.Closer); ok {
  69. return closer.Close()
  70. }
  71. return nil
  72. }
  73. type fullReader struct {
  74. r io.Reader
  75. buf []byte
  76. }
  77. func NewFullReader(r io.Reader, maxSize int) ReadCloser {
  78. return &fullReader{r, make([]byte, maxSize)}
  79. }
  80. func (this *fullReader) ReadMsg(msg proto.Message) error {
  81. length, err := this.r.Read(this.buf)
  82. if err != nil {
  83. return err
  84. }
  85. return proto.Unmarshal(this.buf[:length], msg)
  86. }
  87. func (this *fullReader) Close() error {
  88. if closer, ok := this.r.(io.Closer); ok {
  89. return closer.Close()
  90. }
  91. return nil
  92. }