real_encoder.go 1.6 KB

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