packet_decoder.go 2.7 KB

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