client-cpp  0.0.1-SNAPSHOT
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 
24 #include <avro/Compiler.hh>
25 #include <avro/Specific.hh>
26 #include <avro/Stream.hh>
27 #include <avro/Encoder.hh>
28 #include <avro/Decoder.hh>
29 
30 #include <boost/cstdint.hpp>
31 
34 
35 namespace kaa {
36 
42 template<typename T>
44 public:
49 
50 
51  /*
52  * Copy operator
53 
54  */
55 
63  T fromByteArray(const boost::uint8_t* data, const boost::uint32_t& dataSize);
64 
72  void fromByteArray(const boost::uint8_t* data, const boost::uint32_t& dataSize, T& datum);
73 
79  SharedDataBuffer toByteArray(const T& datum);
80 
86  void toByteArray(const T& datum, std::ostream& stream);
87 
91  void switchToJson(const avro::ValidSchema &schema) {
92  encoder_ = avro::jsonEncoder(schema);
93  decoder_ = avro::jsonDecoder(schema);
94  }
95 
96  void switchToBinary() {
97  encoder_ = avro::binaryEncoder();
98  decoder_ = avro::binaryDecoder();
99  }
100 
101 private:
102  avro::EncoderPtr encoder_;
103  avro::DecoderPtr decoder_;
104 };
105 
106 template<typename T>
108 {
109  switchToBinary();
110 }
111 
112 template<typename T>
113 T AvroByteArrayConverter<T>::fromByteArray(const boost::uint8_t* data, const boost::uint32_t& dataSize)
114 {
115  if (!data || dataSize == 0) {
116  throw KaaException("null data to decode");
117  }
118 
119  T datum;
120  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
121 
122  decoder_->init(*in);
123  avro::decode(*decoder_, datum);
124 
125  return datum;
126 }
127 
128 template<typename T>
129 void AvroByteArrayConverter<T>::fromByteArray(const boost::uint8_t* data, const boost::uint32_t& dataSize, T& datum)
130 {
131  if (!data || dataSize == 0) {
132  throw KaaException("null data to decode");
133  }
134 
135  std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
136 
137  decoder_->init(*in);
138  avro::decode(*decoder_, datum);
139 }
140 
141 template<typename T>
143 {
144  std::ostringstream ostream;
145  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
146 
147  encoder_->init(*out);
148  avro::encode(*encoder_, datum);
149  encoder_->flush();
150 
151  SharedDataBuffer buffer;
152  const std::string& encodedData = ostream.str();
153  const size_t encodedDataSize = encodedData.size();
154 
155  buffer.second = encodedDataSize;
156  buffer.first.reset(new uint8_t[encodedDataSize]);
157  memcpy(buffer.first.get(), encodedData.data(), encodedDataSize);
158 
159  return buffer;
160 }
161 
162 template<typename T>
163 void AvroByteArrayConverter<T>::toByteArray(const T& datum, std::ostream& stream)
164 {
165  std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(stream);
166 
167  encoder_->init(*out);
168  avro::encode(*encoder_, datum);
169  encoder_->flush();
170 }
171 
172 } /* namespace kaa */
173 
174 #endif /* AVROBYTEARRAYCONVERTER_HPP_ */
void switchToJson(const avro::ValidSchema &schema)
T fromByteArray(const boost::uint8_t *data, const boost::uint32_t &dataSize)
std::pair< boost::shared_array< boost::uint8_t >, boost::uint32_t > SharedDataBuffer
SharedDataBuffer toByteArray(const T &datum)