IBR-DTNSuite  0.12
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  std::streamsize size() const;
69 
70  // updates the const size of the BLOB
71  void update();
72 
73  class iostream
74  {
75  private:
76  BLOB &_blob;
78  std::iostream &_stream;
79 
80  public:
81  iostream(BLOB &blob)
82  : _blob(blob), lock(blob), _stream(blob.__get_stream())
83  {
84  _blob.open();
85  }
86 
87  virtual ~iostream()
88  {
89  _blob.close();
90  _blob.update();
91  };
92 
93  std::iostream* operator->() { return &(_blob.__get_stream()); };
94  std::iostream& operator*() { return _blob.__get_stream(); };
95 
96  std::streamsize size()
97  {
98  return _blob.__get_size();
99  };
100 
101  void clear()
102  {
103  _blob.clear();
104  }
105  };
106 
107  class Reference
108  {
109  public:
110  Reference(BLOB *blob);
111  Reference(const Reference &ref);
112  virtual ~Reference();
113 
119 
124  const BLOB& operator*() const;
125 
129  std::streamsize size() const;
130 
131  private:
132  refcnt_ptr<BLOB> _blob;
133  };
134 
141  class Provider
142  {
143  public:
147  virtual ~Provider() = 0;
148 
153  virtual BLOB::Reference create() = 0;
154  };
155 
161 
167 
171  static void changeProvider(BLOB::Provider *p, bool auto_delete = false);
172 
173  protected:
175  {
176  public:
177  ProviderRef(Provider *provider, bool auto_delete);
178  virtual ~ProviderRef();
179 
180  void change(Provider *p, bool auto_delete = true);
181 
183 
184  private:
185  Provider *_provider;
186  bool _auto_delete;
187  };
188 
193 
194  BLOB(const std::streamsize intitial_size = 0);
195 
196  virtual std::streamsize __get_size() = 0;
197  virtual std::iostream &__get_stream() = 0;
198 
199  private:
200  BLOB(const BLOB &ref); // forbidden copy constructor
201  std::streamsize _const_size;
202  };
203 
208  class FileBLOB : public ibrcommon::BLOB
209  {
210  public:
211  FileBLOB(const ibrcommon::File &f);
212  virtual ~FileBLOB();
213 
214  virtual void clear();
215 
216  virtual void open();
217  virtual void close();
218 
219  protected:
220  std::iostream &__get_stream()
221  {
222  return _filestream;
223  }
224 
225  std::streamsize __get_size();
226 
227  private:
228  std::fstream _filestream;
229  File _file;
230  };
231 
233  {
234  public:
236  virtual ~MemoryBLOBProvider();
238 
239  private:
244  class StringBLOB : public BLOB
245  {
246  public:
247  static BLOB::Reference create();
248  virtual ~StringBLOB();
249 
250  virtual void clear();
251 
252  virtual void open();
253  virtual void close();
254 
255  protected:
256  std::iostream &__get_stream()
257  {
258  return _stringstream;
259  }
260 
261  std::streamsize __get_size();
262 
263  private:
264  StringBLOB();
265  std::stringstream _stringstream;
266  };
267  };
268 
270  {
271  public:
272  FileBLOBProvider(const File &path);
273  virtual ~FileBLOBProvider();
275 
276  private:
277  ibrcommon::File _tmppath;
278 
284  class TmpFileBLOB : public BLOB
285  {
286  public:
287  TmpFileBLOB(const File &path);
288  virtual ~TmpFileBLOB();
289 
290  virtual void clear();
291 
292  virtual void open();
293  virtual void close();
294 
295  protected:
296  std::iostream &__get_stream()
297  {
298  return _filestream;
299  }
300 
301  std::streamsize __get_size();
302 
303  private:
304  std::fstream _filestream;
305  int _fd;
306  TemporaryFile _tmpfile;
307  };
308  };
309 }
310 
311 #endif /* BLOB_H_ */