stream.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package bson
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "io"
  7. )
  8. const (
  9. // MinDocumentSize is the size of the smallest possible valid BSON document:
  10. // an int32 size header + 0x00 (end of document).
  11. MinDocumentSize = 5
  12. // MaxDocumentSize is the largest possible size for a BSON document allowed by MongoDB,
  13. // that is, 16 MiB (see https://docs.mongodb.com/manual/reference/limits/).
  14. MaxDocumentSize = 16777216
  15. )
  16. // ErrInvalidDocumentSize is an error returned when a BSON document's header
  17. // contains a size smaller than MinDocumentSize or greater than MaxDocumentSize.
  18. type ErrInvalidDocumentSize struct {
  19. DocumentSize int32
  20. }
  21. func (e ErrInvalidDocumentSize) Error() string {
  22. return fmt.Sprintf("invalid document size %d", e.DocumentSize)
  23. }
  24. // A Decoder reads and decodes BSON values from an input stream.
  25. type Decoder struct {
  26. source io.Reader
  27. }
  28. // NewDecoder returns a new Decoder that reads from source.
  29. // It does not add any extra buffering, and may not read data from source beyond the BSON values requested.
  30. func NewDecoder(source io.Reader) *Decoder {
  31. return &Decoder{source: source}
  32. }
  33. // Decode reads the next BSON-encoded value from its input and stores it in the value pointed to by v.
  34. // See the documentation for Unmarshal for details about the conversion of BSON into a Go value.
  35. func (dec *Decoder) Decode(v interface{}) (err error) {
  36. // BSON documents start with their size as a *signed* int32.
  37. var docSize int32
  38. if err = binary.Read(dec.source, binary.LittleEndian, &docSize); err != nil {
  39. return
  40. }
  41. if docSize < MinDocumentSize || docSize > MaxDocumentSize {
  42. return ErrInvalidDocumentSize{DocumentSize: docSize}
  43. }
  44. docBuffer := bytes.NewBuffer(make([]byte, 0, docSize))
  45. if err = binary.Write(docBuffer, binary.LittleEndian, docSize); err != nil {
  46. return
  47. }
  48. // docSize is the *full* document's size (including the 4-byte size header,
  49. // which has already been read).
  50. if _, err = io.CopyN(docBuffer, dec.source, int64(docSize-4)); err != nil {
  51. return
  52. }
  53. // Let Unmarshal handle the rest.
  54. defer handleErr(&err)
  55. return Unmarshal(docBuffer.Bytes(), v)
  56. }
  57. // An Encoder encodes and writes BSON values to an output stream.
  58. type Encoder struct {
  59. target io.Writer
  60. }
  61. // NewEncoder returns a new Encoder that writes to target.
  62. func NewEncoder(target io.Writer) *Encoder {
  63. return &Encoder{target: target}
  64. }
  65. // Encode encodes v to BSON, and if successful writes it to the Encoder's output stream.
  66. // See the documentation for Marshal for details about the conversion of Go values to BSON.
  67. func (enc *Encoder) Encode(v interface{}) error {
  68. data, err := Marshal(v)
  69. if err != nil {
  70. return err
  71. }
  72. _, err = enc.target.Write(data)
  73. return err
  74. }