00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef ROSBAG_STREAM_H
00036 #define ROSBAG_STREAM_H
00037
00038 #include <ios>
00039 #include <stdint.h>
00040 #include <string>
00041
00042 #include <boost/shared_ptr.hpp>
00043
00044 #include <bzlib.h>
00045
00046 #include "rosbag/exceptions.h"
00047
00048 namespace rosbag {
00049
00050 namespace compression
00051 {
00052 enum CompressionType
00053 {
00054 Uncompressed = 0,
00055 BZ2 = 1,
00056 };
00057 }
00058 typedef compression::CompressionType CompressionType;
00059
00060 class ChunkedFile;
00061
00062 class Stream
00063 {
00064 public:
00065 Stream(ChunkedFile* file);
00066 virtual ~Stream();
00067
00068 virtual CompressionType getCompressionType() const = 0;
00069
00070 virtual void write(void* ptr, size_t size) = 0;
00071 virtual void read (void* ptr, size_t size) = 0;
00072
00073 virtual void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) = 0;
00074
00075 virtual void startWrite();
00076 virtual void stopWrite();
00077
00078 virtual void startRead();
00079 virtual void stopRead();
00080
00081 protected:
00082 FILE* getFilePointer();
00083 uint64_t getCompressedIn();
00084 void setCompressedIn(uint64_t nbytes);
00085 void advanceOffset(uint64_t nbytes);
00086 char* getUnused();
00087 int getUnusedLength();
00088 void setUnused(char* unused);
00089 void setUnusedLength(int nUnused);
00090 void clearUnused();
00091
00092 protected:
00093 ChunkedFile* file_;
00094 };
00095
00096 class StreamFactory
00097 {
00098 public:
00099 StreamFactory(ChunkedFile* file);
00100
00101 boost::shared_ptr<Stream> getStream(CompressionType type) const;
00102
00103 private:
00104 boost::shared_ptr<Stream> uncompressed_stream_;
00105 boost::shared_ptr<Stream> bz2_stream_;
00106 };
00107
00108 class UncompressedStream : public Stream
00109 {
00110 public:
00111 UncompressedStream(ChunkedFile* file);
00112
00113 CompressionType getCompressionType() const;
00114
00115 void write(void* ptr, size_t size);
00116 void read(void* ptr, size_t size);
00117
00118 void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
00119 };
00120
00124 class BZ2Stream : public Stream
00125 {
00126 public:
00127 BZ2Stream(ChunkedFile* file);
00128
00129 CompressionType getCompressionType() const;
00130
00131 void startWrite();
00132 void write(void* ptr, size_t size);
00133 void stopWrite();
00134
00135 void startRead();
00136 void read(void* ptr, size_t size);
00137 void stopRead();
00138
00139 void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
00140
00141 private:
00142 int verbosity_;
00143 int block_size_100k_;
00144 int work_factor_;
00145
00146 BZFILE* bzfile_;
00147 int bzerror_;
00148 };
00149
00150 }
00151
00152 #endif