real_encoder.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package kafka
  2. import "encoding/binary"
  3. type realEncoder struct {
  4. raw []byte
  5. off int
  6. stack []pushEncoder
  7. }
  8. func (re *realEncoder) putInt8(in int8) {
  9. re.raw[re.off] = byte(in)
  10. re.off += 1
  11. }
  12. func (re *realEncoder) putInt16(in int16) {
  13. binary.BigEndian.PutUint16(re.raw[re.off:], uint16(in))
  14. re.off += 2
  15. }
  16. func (re *realEncoder) putInt32(in int32) {
  17. binary.BigEndian.PutUint32(re.raw[re.off:], uint32(in))
  18. re.off += 4
  19. }
  20. func (re *realEncoder) putInt64(in int64) {
  21. binary.BigEndian.PutUint64(re.raw[re.off:], uint64(in))
  22. re.off += 8
  23. }
  24. func (re *realEncoder) putError(in KError) {
  25. re.putInt16(int16(in))
  26. }
  27. func (re *realEncoder) putString(in *string) {
  28. if in == nil {
  29. re.putInt16(-1)
  30. return
  31. }
  32. re.putInt16(int16(len(*in)))
  33. copy(re.raw[re.off:], *in)
  34. re.off += len(*in)
  35. }
  36. func (re *realEncoder) putBytes(in *[]byte) {
  37. if in == nil {
  38. re.putInt32(-1)
  39. return
  40. }
  41. re.putInt32(int32(len(*in)))
  42. copy(re.raw[re.off:], *in)
  43. re.off += len(*in)
  44. }
  45. func (re *realEncoder) putArrayCount(in int) {
  46. re.putInt32(int32(in))
  47. }
  48. func (re *realEncoder) push(in pushEncoder) {
  49. in.saveOffset(re.off)
  50. re.off += in.reserveLength()
  51. re.stack = append(re.stack, in)
  52. }
  53. func (re *realEncoder) pushLength32() {
  54. re.push(&length32Encoder{})
  55. }
  56. func (re *realEncoder) pushCRC32() {
  57. re.push(&crc32Encoder{})
  58. }
  59. func (re *realEncoder) pop() {
  60. // this is go's ugly pop pattern (the inverse of append)
  61. in := re.stack[len(re.stack)-1]
  62. re.stack = re.stack[:len(re.stack)-1]
  63. in.run(re.off, re.raw)
  64. }