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