prep_encoder.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package sarama
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type prepEncoder struct {
  7. length int
  8. }
  9. // primitives
  10. func (pe *prepEncoder) putInt8(in int8) {
  11. pe.length += 1
  12. }
  13. func (pe *prepEncoder) putInt16(in int16) {
  14. pe.length += 2
  15. }
  16. func (pe *prepEncoder) putInt32(in int32) {
  17. pe.length += 4
  18. }
  19. func (pe *prepEncoder) putInt64(in int64) {
  20. pe.length += 8
  21. }
  22. func (pe *prepEncoder) putArrayLength(in int) error {
  23. if in > math.MaxInt32 {
  24. return PacketEncodingError{fmt.Sprintf("array too long (%d)", in)}
  25. }
  26. pe.length += 4
  27. return nil
  28. }
  29. // arrays
  30. func (pe *prepEncoder) putBytes(in []byte) error {
  31. pe.length += 4
  32. if in == nil {
  33. return nil
  34. }
  35. if len(in) > math.MaxInt32 {
  36. return PacketEncodingError{fmt.Sprintf("byteslice too long (%d)", len(in))}
  37. }
  38. pe.length += len(in)
  39. return nil
  40. }
  41. func (pe *prepEncoder) putRawBytes(in []byte) error {
  42. if len(in) > math.MaxInt32 {
  43. return PacketEncodingError{fmt.Sprintf("byteslice too long (%d)", len(in))}
  44. }
  45. pe.length += len(in)
  46. return nil
  47. }
  48. func (pe *prepEncoder) putString(in string) error {
  49. pe.length += 2
  50. if len(in) > math.MaxInt16 {
  51. return PacketEncodingError{fmt.Sprintf("string too long (%d)", len(in))}
  52. }
  53. pe.length += len(in)
  54. return nil
  55. }
  56. func (pe *prepEncoder) putInt32Array(in []int32) error {
  57. err := pe.putArrayLength(len(in))
  58. if err != nil {
  59. return err
  60. }
  61. pe.length += 4 * len(in)
  62. return nil
  63. }
  64. func (pe *prepEncoder) putInt64Array(in []int64) error {
  65. err := pe.putArrayLength(len(in))
  66. if err != nil {
  67. return err
  68. }
  69. pe.length += 8 * len(in)
  70. return nil
  71. }
  72. // stackable
  73. func (pe *prepEncoder) push(in pushEncoder) {
  74. pe.length += in.reserveLength()
  75. }
  76. func (pe *prepEncoder) pop() error {
  77. return nil
  78. }