IBR-DTNSuite  0.10
BLOB.h
Go to the documentation of this file.
1 /*
2  * BLOB.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 BLOB_H_
23 #define BLOB_H_
24 
25 #include "ibrcommon/thread/Mutex.h"
27 #include "ibrcommon/data/File.h"
29 #include "ibrcommon/refcnt_ptr.h"
30 #include <iostream>
31 #include <sstream>
32 #include <fstream>
33 
34 namespace ibrcommon
35 {
37  {
38  public:
39  CanNotOpenFileException(ibrcommon::File f) throw() : IOException("Could not open file " + f.getPath() + ".")
40  {
41  };
42  };
43 
44  class BLOB : public Mutex
45  {
46  public:
51 
55  static std::ostream& copy(std::ostream &output, std::istream &input, const std::streamsize size, const size_t buffer_size = 0x1000);
56 
57  virtual ~BLOB();
58 
63  virtual void clear() = 0;
64 
65  virtual void open() = 0;
66  virtual void close() = 0;
67 
68  class iostream
69  {
70  private:
71  BLOB &_blob;
73  std::iostream &_stream;
74 
75  public:
76  iostream(BLOB &blob)
77  : _blob(blob), lock(blob), _stream(blob.__get_stream())
78  {
79  _blob.open();
80  }
81 
82  virtual ~iostream()
83  {
84  _blob.close();
85  };
86 
87  std::iostream* operator->() { return &(_blob.__get_stream()); };
88  std::iostream& operator*() { return _blob.__get_stream(); };
89 
90  std::streamsize size()
91  {
92  return _blob.__get_size();
93  };
94 
95  void clear()
96  {
97  _blob.clear();
98  }
99  };
100 
101  class Reference
102  {
103  public:
104  Reference(BLOB *blob);
105  Reference(const Reference &ref);
106  virtual ~Reference();
107 
113 
118  const BLOB& operator*() const;
119 
120  private:
121  refcnt_ptr<BLOB> _blob;
122  };
123 
130  class Provider
131  {
132  public:
136  virtual ~Provider() = 0;
137 
142  virtual BLOB::Reference create() = 0;
143  };
144 
150 
156 
160  static void changeProvider(BLOB::Provider *p, bool auto_delete = false);
161 
162  protected:
164  {
165  public:
166  ProviderRef(Provider *provider, bool auto_delete);
167  virtual ~ProviderRef();
168 
169  void change(Provider *p, bool auto_delete = true);
170 
172 
173  private:
174  Provider *_provider;
175  bool _auto_delete;
176  };
177 
182 
183  BLOB();
184 
185  virtual std::streamsize __get_size() = 0;
186  virtual std::iostream &__get_stream() = 0;
187 
188  private:
189  BLOB(const BLOB &ref); // forbidden copy constructor
190  };
191 
196  class FileBLOB : public ibrcommon::BLOB
197  {
198  public:
199  FileBLOB(const ibrcommon::File &f);
200  virtual ~FileBLOB();
201 
202  virtual void clear();
203 
204  virtual void open();
205  virtual void close();
206 
207  protected:
208  std::iostream &__get_stream()
209  {
210  return _filestream;
211  }
212 
213  std::streamsize __get_size();
214 
215  private:
216  std::fstream _filestream;
217  File _file;
218  };
219 
221  {
222  public:
224  virtual ~MemoryBLOBProvider();
226 
227  private:
232  class StringBLOB : public BLOB
233  {
234  public:
235  static BLOB::Reference create();
236  virtual ~StringBLOB();
237 
238  virtual void clear();
239 
240  virtual void open();
241  virtual void close();
242 
243  protected:
244  std::iostream &__get_stream()
245  {
246  return _stringstream;
247  }
248 
249  std::streamsize __get_size();
250 
251  private:
252  StringBLOB();
253  std::stringstream _stringstream;
254  };
255  };
256 
258  {
259  public:
260  FileBLOBProvider(const File &path);
261  virtual ~FileBLOBProvider();
263 
264  private:
265  ibrcommon::File _tmppath;
266 
272  class TmpFileBLOB : public BLOB
273  {
274  public:
275  TmpFileBLOB(const File &path);
276  virtual ~TmpFileBLOB();
277 
278  virtual void clear();
279 
280  virtual void open();
281  virtual void close();
282 
283  protected:
284  std::iostream &__get_stream()
285  {
286  return _filestream;
287  }
288 
289  std::streamsize __get_size();
290 
291  private:
292  std::fstream _filestream;
293  int _fd;
294  TemporaryFile _tmpfile;
295  };
296  };
297 }
298 
299 #endif /* BLOB_H_ */