real_encoder.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. re.putRaw(in)
  55. }
  56. func (re *realEncoder) putRaw(in []byte) {
  57. copy(re.raw[re.off:], in)
  58. re.off += len(in)
  59. }
  60. // stackable
  61. func (re *realEncoder) push(in pushEncoder) {
  62. in.saveOffset(re.off)
  63. re.off += in.reserveLength()
  64. re.stack = append(re.stack, in)
  65. }
  66. func (re *realEncoder) pushLength32() {
  67. re.push(&length32Encoder{})
  68. }
  69. func (re *realEncoder) pushCRC32() {
  70. re.push(&crc32Encoder{})
  71. }
  72. func (re *realEncoder) pop() {
  73. // this is go's ugly pop pattern (the inverse of append)
  74. in := re.stack[len(re.stack)-1]
  75. re.stack = re.stack[:len(re.stack)-1]
  76. in.run(re.off, re.raw)
  77. }