packet_encoder.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package sarama
  2. // PacketEncoder is the interface providing helpers for writing with Kafka's encoding rules.
  3. // Types implementing Encoder only need to worry about calling methods like PutString,
  4. // not about how a string is represented in Kafka.
  5. type packetEncoder interface {
  6. // Primitives
  7. putInt8(in int8)
  8. putInt16(in int16)
  9. putInt32(in int32)
  10. putInt64(in int64)
  11. putArrayLength(in int) error
  12. // Collections
  13. putBytes(in []byte) error
  14. putRawBytes(in []byte) error
  15. putString(in string) error
  16. putStringArray(in []string) error
  17. putInt32Array(in []int32) error
  18. putInt64Array(in []int64) error
  19. // Stacks, see PushEncoder
  20. push(in pushEncoder)
  21. pop() error
  22. }
  23. // PushEncoder is the interface for encoding fields like CRCs and lengths where the value
  24. // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where
  25. // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they
  26. // depend upon have been written.
  27. type pushEncoder interface {
  28. // Saves the offset into the input buffer as the location to actually write the calculated value when able.
  29. saveOffset(in int)
  30. // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
  31. reserveLength() int
  32. // Indicates that all required data is now available to calculate and write the field.
  33. // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
  34. // of data to the saved offset, based on the data between the saved offset and curOffset.
  35. run(curOffset int, buf []byte) error
  36. }