packet_decoder.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package sarama
  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. getStringArray() ([]string, error)
  18. // Subsets
  19. remaining() int
  20. getSubset(length int) (packetDecoder, error)
  21. // Stacks, see PushDecoder
  22. push(in pushDecoder) error
  23. pop() error
  24. }
  25. // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
  26. // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
  27. // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
  28. // depend upon have been decoded.
  29. type pushDecoder interface {
  30. // Saves the offset into the input buffer as the location to actually read the calculated value when able.
  31. saveOffset(in int)
  32. // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32).
  33. reserveLength() int
  34. // Indicates that all required data is now available to calculate and check the field.
  35. // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
  36. // of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
  37. check(curOffset int, buf []byte) error
  38. }