stream.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #ifndef ROSBAG_STREAM_H
36 #define ROSBAG_STREAM_H
37 
38 #include <ios>
39 #include <stdint.h>
40 #include <string>
41 
42 #include <boost/shared_ptr.hpp>
43 
44 #include <bzlib.h>
45 
46 #include <roslz4/lz4s.h>
47 
48 #include "rosbag/exceptions.h"
49 #include "rosbag/macros.h"
50 
51 namespace rosbag {
52 
53 namespace compression
54 {
55  enum CompressionType
56  {
57  Uncompressed = 0,
58  BZ2 = 1,
59  LZ4 = 2,
60  };
61 }
63 
64 class ChunkedFile;
65 
66 class FileAccessor;
67 
68 class ROSBAG_STORAGE_DECL Stream
69 {
70  friend class FileAccessor;
71 public:
72  Stream(ChunkedFile* file);
73  virtual ~Stream();
74 
75  virtual CompressionType getCompressionType() const = 0;
76 
77  virtual void write(void* ptr, size_t size) = 0;
78  virtual void read (void* ptr, size_t size) = 0;
79 
80  virtual void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) = 0;
81 
82  virtual void startWrite();
83  virtual void stopWrite();
84 
85  virtual void startRead();
86  virtual void stopRead();
87 
88 protected:
89  FILE* getFilePointer();
90  uint64_t getCompressedIn();
91  void setCompressedIn(uint64_t nbytes);
92  void advanceOffset(uint64_t nbytes);
93  char* getUnused();
94  int getUnusedLength();
95  void setUnused(char* unused);
96  void setUnusedLength(int nUnused);
97  void clearUnused();
98 
99 protected:
100  ChunkedFile* file_;
101 };
102 
104 {
105 public:
106  StreamFactory(ChunkedFile* file);
107 
108  boost::shared_ptr<Stream> getStream(CompressionType type) const;
109 
110 private:
111  boost::shared_ptr<Stream> uncompressed_stream_;
112  boost::shared_ptr<Stream> bz2_stream_;
113  boost::shared_ptr<Stream> lz4_stream_;
114 };
115 
116 class FileAccessor {
117  friend class ChunkedFile;
118  static void setFile(Stream& a, ChunkedFile* file) {
119  a.file_ = file;
120  }
121 };
122 
123 class ROSBAG_STORAGE_DECL UncompressedStream : public Stream
124 {
125 public:
126  UncompressedStream(ChunkedFile* file);
127 
128  CompressionType getCompressionType() const;
129 
130  void write(void* ptr, size_t size);
131  void read(void* ptr, size_t size);
132 
133  void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
134 };
135 
139 class ROSBAG_STORAGE_DECL BZ2Stream : public Stream
140 {
141 public:
142  BZ2Stream(ChunkedFile* file);
143 
144  CompressionType getCompressionType() const;
145 
146  void startWrite();
147  void write(void* ptr, size_t size);
148  void stopWrite();
149 
150  void startRead();
151  void read(void* ptr, size_t size);
152  void stopRead();
153 
154  void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
155 
156 private:
157  int verbosity_;
158  int block_size_100k_;
159  int work_factor_;
160 
161  BZFILE* bzfile_;
162  int bzerror_;
163 };
164 
165 // LZ4Stream reads/writes compressed datat in the LZ4 format
166 // https://code.google.com/p/lz4/
167 class ROSBAG_STORAGE_DECL LZ4Stream : public Stream
168 {
169 public:
170  LZ4Stream(ChunkedFile* file);
172 
173  CompressionType getCompressionType() const;
174 
175  void startWrite();
176  void write(void* ptr, size_t size);
177  void stopWrite();
178 
179  void startRead();
180  void read(void* ptr, size_t size);
181  void stopRead();
182 
183  void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
184 
185 private:
186  LZ4Stream(const LZ4Stream&);
187  LZ4Stream operator=(const LZ4Stream&);
188  void writeStream(int action);
189 
190  char *buff_;
191  int buff_size_;
192  int block_size_id_;
194 };
195 
196 
197 
198 } // namespace rosbag
199 
200 #endif
rosbag::ChunkedFile
ChunkedFile reads and writes files which contain interleaved chunks of compressed and uncompressed da...
Definition: chunked_file.h:83
rosbag::FileAccessor::setFile
static void setFile(Stream &a, ChunkedFile *file)
Definition: stream.h:150
exceptions.h
boost::shared_ptr
rosbag::Stream::file_
ChunkedFile * file_
Definition: stream.h:132
decompress
def decompress(data)
rosbag::compression::LZ4
@ LZ4
Definition: stream.h:155
macros.h
rosbag::compression::BZ2
@ BZ2
Definition: stream.h:154
rosbag::FileAccessor
Definition: stream.h:148
rosbag::LZ4Stream
Definition: stream.h:199
roslz4_stream
ROSBAG_STORAGE_DECL
#define ROSBAG_STORAGE_DECL
Definition: macros.h:50
rosbag::StreamFactory
Definition: stream.h:135
rosbag::compression::CompressionType
CompressionType
Definition: stream.h:119
rosbag::CompressionType
compression::CompressionType CompressionType
Definition: stream.h:94
rosbag::Stream
Definition: stream.h:100
lz4s.h
rosbag::compression::Uncompressed
@ Uncompressed
Definition: stream.h:153
rosbag
Definition: aes_encryptor.h:43
rosbag::BZ2Stream
Definition: stream.h:171


rosbag_storage
Author(s): Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:58