client-cpp  0.6.1
AvroByteArrayConverter.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AVROBYTEARRAYCONVERTER_HPP_
18 #define AVROBYTEARRAYCONVERTER_HPP_
19 
20 #include <string>
21 #include <memory>
22 #include <sstream>
23 #include <cstdint>
24 
25 #include <avro/Compiler.hh>
26 #include <avro/Specific.hh>
27 #include <avro/Stream.hh>
28 #include <avro/Encoder.hh>
29 #include <avro/Decoder.hh>
30 
33 
34 namespace kaa {
35 
41 template<typename T>
43 public:
48 
49 
50  /*
51  * Copy operator
52 
53  */
54 
62  T fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize);
63 
71  void fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize, T& datum);
72 
78  SharedDataBuffer toByteArray(const T& datum);
79 
85  void toByteArray(const T& datum, std::ostream& stream);
86 
90  void switchToJson(const avro::ValidSchema &schema) {
91  encoder_ = avro::jsonEncoder(schema);
92  decoder_ = avro::jsonDecoder(schema);
93  }
94 
95  void switchToBinary() {
96  encoder_ = avro::binaryEncoder();
97  decoder_ = avro::binaryDecoder();
98  }
99 
100 private:
101  avro::EncoderPtr encoder_;
102  avro::DecoderPtr decoder_;
103 };
104 
105 template<typename T>
107 {
108  switchToBinary();
109 }
110 
111 template<typename T>
112 T AvroByteArrayConverter<T>::fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize)
113 {
114  if (!data || dataSize == 0) {
115  throw KaaException("null data to decode");
116  }
117 
118  T datum;
119  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
120 
121  decoder_->init(*in);
122  avro::decode(*decoder_, datum);
123 
124  return datum;
125 }
126 
127 template<typename T>
128 void AvroByteArrayConverter<T>::fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize, T& datum)
129 {
130  if (!data || dataSize == 0) {
131  throw KaaException("null data to decode");
132  }
133 
134  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
135 
136  decoder_->init(*in);
137  avro::decode(*decoder_, datum);
138 }
139 
140 template<typename T>
142 {
143  std::ostringstream ostream;
144  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
145 
146  encoder_->init(*out);
147  avro::encode(*encoder_, datum);
148  encoder_->flush();
149 
150  SharedDataBuffer buffer;
151  const std::string& encodedData = ostream.str();
152  const size_t encodedDataSize = encodedData.size();
153 
154  buffer.second = encodedDataSize;
155  buffer.first.reset(new uint8_t[encodedDataSize]);
156  memcpy(buffer.first.get(), encodedData.data(), encodedDataSize);
157 
158  return buffer;
159 }
160 
161 template<typename T>
162 void AvroByteArrayConverter<T>::toByteArray(const T& datum, std::ostream& stream)
163 {
164  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(stream);
165 
166  encoder_->init(*out);
167  avro::encode(*encoder_, datum);
168  encoder_->flush();
169 }
170 
171 } /* namespace kaa */
172 
173 #endif /* AVROBYTEARRAYCONVERTER_HPP_ */
void switchToJson(const avro::ValidSchema &schema)
SharedDataBuffer toByteArray(const T &datum)
T fromByteArray(const std::uint8_t *data, const std::uint32_t &dataSize)
std::pair< boost::shared_array< std::uint8_t >, std::uint32_t > SharedDataBuffer