packet_encoder.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package encoding
  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) error
  8. PutInt16(in int16) error
  9. PutInt32(in int32) error
  10. PutInt64(in int64) error
  11. // Collections
  12. PutBytes(in []byte) error
  13. PutString(in string) error
  14. PutInt32Array(in []int32) error
  15. // Stacks, see PushEncoder
  16. Push(in PushEncoder) error
  17. Pop() error
  18. }
  19. // PushEncoder is the interface for encoding fields like CRCs and lengths where the value
  20. // of the field depends on what is encoded after it in the packet. Start them with PacketEncoder.Push() where
  21. // the actual value is located in the packet, then PacketEncoder.Pop() them when all the bytes they
  22. // depend upon have been written.
  23. type PushEncoder interface {
  24. // Saves the offset into the input buffer as the location to actually write the calculated value when able.
  25. SaveOffset(in int)
  26. // Returns the length of data to reserve for the output of this encoder (eg 4 bytes for a CRC32).
  27. ReserveLength() int
  28. // Indicates that all required data is now available to calculate and write the field.
  29. // SaveOffset is guaranteed to have been called first. The implementation should write ReserveLength() bytes
  30. // of data to the saved offset, based on the data between the saved offset and curOffset.
  31. Run(curOffset int, buf []byte) error
  32. }