packet_decoder.go 2.6 KB

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