packet_decoder.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. getVarint() (int64, error)
  12. getArrayLength() (int, error)
  13. // Collections
  14. getBytes() ([]byte, error)
  15. getVarintBytes() ([]byte, error)
  16. getRawBytes(length int) ([]byte, error)
  17. getString() (string, error)
  18. getNullableString() (*string, error)
  19. getInt32Array() ([]int32, error)
  20. getInt64Array() ([]int64, error)
  21. getStringArray() ([]string, error)
  22. // Subsets
  23. remaining() int
  24. getSubset(length int) (packetDecoder, error)
  25. peek(offset, length int) (packetDecoder, error) // similar to getSubset, but it doesn't advance the offset
  26. // Stacks, see PushDecoder
  27. push(in pushDecoder) error
  28. pop() error
  29. }
  30. // PushDecoder is the interface for decoding fields like CRCs and lengths where the validity
  31. // of the field depends on what is after it in the packet. Start them with PacketDecoder.Push() where
  32. // the actual value is located in the packet, then PacketDecoder.Pop() them when all the bytes they
  33. // depend upon have been decoded.
  34. type pushDecoder interface {
  35. // Saves the offset into the input buffer as the location to actually read the calculated value when able.
  36. saveOffset(in int)
  37. // Returns the length of data to reserve for the input of this encoder (eg 4 bytes for a CRC32).
  38. reserveLength() int
  39. // Indicates that all required data is now available to calculate and check the field.
  40. // SaveOffset is guaranteed to have been called first. The implementation should read ReserveLength() bytes
  41. // of data from the saved offset, and verify it based on the data between the saved offset and curOffset.
  42. check(curOffset int, buf []byte) error
  43. }
  44. // dynamicPushDecoder extends the interface of pushDecoder for uses cases where the length of the
  45. // fields itself is unknown until its value was decoded (for instance varint encoded length
  46. // fields).
  47. // During push, dynamicPushDecoder.decode() method will be called instead of reserveLength()
  48. type dynamicPushDecoder interface {
  49. pushDecoder
  50. decoder
  51. }