$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 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 } // namespace rosbag 00151 00152 #endif