BinaryEncoder.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "Encoder.hh"
  19. #include "Zigzag.hh"
  20. #include <array>
  21. namespace avro {
  22. using std::make_shared;
  23. class BinaryEncoder : public Encoder {
  24. StreamWriter out_;
  25. void init(OutputStream &os) final;
  26. void flush() final;
  27. int64_t byteCount() const final;
  28. void encodeNull() final;
  29. void encodeBool(bool b) final;
  30. void encodeInt(int32_t i) final;
  31. void encodeLong(int64_t l) final;
  32. void encodeFloat(float f) final;
  33. void encodeDouble(double d) final;
  34. void encodeString(const std::string &s) final;
  35. void encodeBytes(const uint8_t *bytes, size_t len) final;
  36. void encodeFixed(const uint8_t *bytes, size_t len) final;
  37. void encodeEnum(size_t e) final;
  38. void arrayStart() final;
  39. void arrayEnd() final;
  40. void mapStart() final;
  41. void mapEnd() final;
  42. void setItemCount(size_t count) final;
  43. void startItem() final;
  44. void encodeUnionIndex(size_t e) final;
  45. void doEncodeLong(int64_t l);
  46. };
  47. EncoderPtr binaryEncoder() {
  48. return make_shared<BinaryEncoder>();
  49. }
  50. void BinaryEncoder::init(OutputStream &os) {
  51. out_.reset(os);
  52. }
  53. void BinaryEncoder::flush() {
  54. out_.flush();
  55. }
  56. void BinaryEncoder::encodeNull() {
  57. }
  58. void BinaryEncoder::encodeBool(bool b) {
  59. out_.write(b ? 1 : 0);
  60. }
  61. void BinaryEncoder::encodeInt(int32_t i) {
  62. doEncodeLong(i);
  63. }
  64. void BinaryEncoder::encodeLong(int64_t l) {
  65. doEncodeLong(l);
  66. }
  67. void BinaryEncoder::encodeFloat(float f) {
  68. const auto *p = reinterpret_cast<const uint8_t *>(&f);
  69. out_.writeBytes(p, sizeof(float));
  70. }
  71. void BinaryEncoder::encodeDouble(double d) {
  72. const auto *p = reinterpret_cast<const uint8_t *>(&d);
  73. out_.writeBytes(p, sizeof(double));
  74. }
  75. void BinaryEncoder::encodeString(const std::string &s) {
  76. doEncodeLong(s.size());
  77. out_.writeBytes(reinterpret_cast<const uint8_t *>(s.c_str()), s.size());
  78. }
  79. void BinaryEncoder::encodeBytes(const uint8_t *bytes, size_t len) {
  80. doEncodeLong(len);
  81. out_.writeBytes(bytes, len);
  82. }
  83. void BinaryEncoder::encodeFixed(const uint8_t *bytes, size_t len) {
  84. out_.writeBytes(bytes, len);
  85. }
  86. void BinaryEncoder::encodeEnum(size_t e) {
  87. doEncodeLong(e);
  88. }
  89. void BinaryEncoder::arrayStart() {
  90. }
  91. void BinaryEncoder::arrayEnd() {
  92. doEncodeLong(0);
  93. }
  94. void BinaryEncoder::mapStart() {
  95. }
  96. void BinaryEncoder::mapEnd() {
  97. doEncodeLong(0);
  98. }
  99. void BinaryEncoder::setItemCount(size_t count) {
  100. if (count == 0) {
  101. throw Exception("Count cannot be zero");
  102. }
  103. doEncodeLong(count);
  104. }
  105. void BinaryEncoder::startItem() {
  106. }
  107. void BinaryEncoder::encodeUnionIndex(size_t e) {
  108. doEncodeLong(e);
  109. }
  110. int64_t BinaryEncoder::byteCount() const {
  111. return out_.byteCount();
  112. }
  113. void BinaryEncoder::doEncodeLong(int64_t l) {
  114. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
  115. std::array<uint8_t, 10> bytes;
  116. auto size = encodeInt64(l, bytes);
  117. out_.writeBytes(bytes.data(), size);
  118. }
  119. } // namespace avro