client-cpp  0.10.0
AvroByteArrayConverter.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 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 {
44 public:
49 
50 
51  /*
52  * Copy operator
53 
54  */
55 
63  T fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize);
64 
72  void fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize, T& datum);
73 
79  SharedDataBuffer toByteArray(const T& datum);
80 
87  void toByteArray(const T& datum, std::vector<std::uint8_t>& dest);
88 
94  void toByteArray(const T& datum, std::ostream& stream);
95 
99  void switchToJson(const avro::ValidSchema &schema) {
100  encoder_ = avro::jsonEncoder(schema);
101  decoder_ = avro::jsonDecoder(schema);
102  }
103 
104  void switchToBinary() {
105  encoder_ = avro::binaryEncoder();
106  decoder_ = avro::binaryDecoder();
107  }
108 
109 private:
110  avro::EncoderPtr encoder_;
111  avro::DecoderPtr decoder_;
112 };
113 
114 template<typename T>
116 {
117  switchToBinary();
118 }
119 
120 template<typename T>
121 T AvroByteArrayConverter<T>::fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize)
122 {
123  if ((data && !dataSize) || (dataSize && !data)) {
124  throw KaaException("invalid data to decode");
125  }
126 
127  T datum;
128  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
129 
130  decoder_->init(*in);
131  avro::decode(*decoder_, datum);
132 
133  return datum;
134 }
135 
136 template<typename T>
137 void AvroByteArrayConverter<T>::fromByteArray(const std::uint8_t* data, const std::uint32_t& dataSize, T& datum)
138 {
139  if ((data && !dataSize) || (dataSize && !data)) {
140  throw KaaException("invalid data to decode");
141  }
142 
143  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
144 
145  decoder_->init(*in);
146  avro::decode(*decoder_, datum);
147 }
148 
149 template<typename T>
151 {
152  std::stringstream ostream;
153  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
154 
155  encoder_->init(*out);
156  avro::encode(*encoder_, datum);
157  encoder_->flush();
158 
159  SharedDataBuffer buffer;
160 
161  std::streampos beg = ostream.tellg();
162  ostream.seekg(0, std::ios_base::end);
163 
164  std::streampos end = ostream.tellg();
165  ostream.seekg(0, std::ios_base::beg);
166 
167  buffer.second = end - beg;
168  buffer.first.reset(new uint8_t[buffer.second]);
169  std::copy(std::istreambuf_iterator<char>(ostream), std::istreambuf_iterator<char>(), buffer.first.get());
170 
171  return buffer;
172 }
173 
174 template<typename T>
175 void AvroByteArrayConverter<T>::toByteArray(const T& datum, std::vector<std::uint8_t>& dest)
176 {
177  std::stringstream ostream;
178  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
179 
180  encoder_->init(*out);
181  avro::encode(*encoder_, datum);
182  encoder_->flush();
183 
184  std::streampos beg = ostream.tellg();
185  ostream.seekg(0, std::ios_base::end);
186 
187  std::streampos end = ostream.tellg();
188  ostream.seekg(0, std::ios_base::beg);
189 
190  dest.reserve(end - beg);
191  dest.assign(std::istreambuf_iterator<char>(ostream), std::istreambuf_iterator<char>());
192 }
193 
194 template<typename T>
195 void AvroByteArrayConverter<T>::toByteArray(const T& datum, std::ostream& stream)
196 {
197  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(stream);
198 
199  encoder_->init(*out);
200  avro::encode(*encoder_, datum);
201  encoder_->flush();
202 }
203 
204 } /* namespace kaa */
205 
206 #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