packet_decoder.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package encoding
  2. // PacketDecoder is the interface providing helpers for reading with Kafka's encoding rules.
  3. // Types implementing Decoder only need to worry about calling methods like GetString,
  4. // not about how a string is represented in Kafka.
  5. type PacketDecoder interface {
  6. // Primitives
  7. GetInt8() (int8, error)
  8. GetInt16() (int16, error)
  9. GetInt32() (int32, error)
  10. GetInt64() (int64, error)
  11. GetArrayLength() (int, error)
  12. // Collections
  13. GetBytes() ([]byte, error)
  14. GetString() (string, error)
  15. GetInt32Array() ([]int32, error)
  16. GetInt64Array() ([]int64, error)
  17. // Subsets
  18. Remaining() int
  19. GetSubset(length int) (PacketDecoder, error)
  20. // Stacks, see PushDecoder
  21. Push(in PushDecoder) error
  22. Pop() error
  23. }
  24. // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
  25. // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
  26. // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
  27. // depend upon have been decoded.
  28. type PushDecoder interface {
  29. // Saves the offset into the input buffer as the location to actually read the calculated value when able.
  30. SaveOffset(in int)
  31. // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32).
  32. ReserveLength() int
  33. // Indicates that all required data is now available to calculate and check the field.
  34. // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
  35. // of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
  36. Check(curOffset int, buf []byte) error
  37. }