IBR-DTNSuite  0.10
Base64Stream.h
Go to the documentation of this file.
1 /*
2  * Base64Stream.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef BASE64STREAM_H_
23 #define BASE64STREAM_H_
24 
25 #include "ibrcommon/data/Base64.h"
26 #include <iostream>
27 #include <streambuf>
28 #include <vector>
29 #include <stdint.h>
30 
31 namespace ibrcommon
32 {
33  class Base64Stream : public std::basic_streambuf<char, std::char_traits<char> >, public std::ostream
34  {
35  public:
36  Base64Stream(std::ostream &stream, bool decode = false, const size_t linebreak = 0, const size_t buffer = 2048);
37  virtual ~Base64Stream();
38 
39  protected:
40  virtual int sync();
41  virtual std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type = std::char_traits<char>::eof());
42 
43  private:
44  void set_b64(char val);
45  void set_byte(char val);
46 
47  void __flush_encoder__();
48 
49  bool _decode;
50 
51  std::ostream &_stream;
52 
53  // Output buffer
54  std::vector<char> data_buf_;
55 
56  // length of the data buffer
57  size_t data_size_;
58 
59  uint8_t _base64_state;
60 
61  size_t _char_counter;
62 
63  Base64::Group _group;
64  char _base64_padding;
65 
66  const size_t _linebreak;
67  };
68 }
69 
70 #endif /* BASE64STREAM_H_ */