length_field.go 694 B

1234567891011121314151617181920212223242526272829
  1. package sarama
  2. import "encoding/binary"
  3. // LengthField implements the PushEncoder and PushDecoder interfaces for calculating 4-byte lengths.
  4. type lengthField struct {
  5. startOffset int
  6. }
  7. func (l *lengthField) saveOffset(in int) {
  8. l.startOffset = in
  9. }
  10. func (l *lengthField) reserveLength() int {
  11. return 4
  12. }
  13. func (l *lengthField) run(curOffset int, buf []byte) error {
  14. binary.BigEndian.PutUint32(buf[l.startOffset:], uint32(curOffset-l.startOffset-4))
  15. return nil
  16. }
  17. func (l *lengthField) check(curOffset int, buf []byte) error {
  18. if uint32(curOffset-l.startOffset-4) != binary.BigEndian.Uint32(buf[l.startOffset:]) {
  19. return PacketDecodingError{"length field invalid"}
  20. }
  21. return nil
  22. }