stream.h
Go to the documentation of this file.
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 <roslz4/lz4s.h>
00047 
00048 #include "rosbag/exceptions.h"
00049 #include "rosbag/macros.h"
00050 
00051 namespace rosbag {
00052 
00053 namespace compression
00054 {
00055     enum CompressionType
00056     {
00057         Uncompressed = 0,
00058         BZ2          = 1,
00059         LZ4          = 2,
00060     };
00061 }
00062 typedef compression::CompressionType CompressionType;
00063 
00064 class ChunkedFile;
00065 
00066 class ROSBAG_DECL Stream
00067 {
00068 public:
00069     Stream(ChunkedFile* file);
00070     virtual ~Stream();
00071 
00072     virtual CompressionType getCompressionType() const = 0;
00073 
00074     virtual void write(void* ptr, size_t size) = 0;
00075     virtual void read (void* ptr, size_t size) = 0;
00076 
00077     virtual void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) = 0;
00078 
00079     virtual void startWrite();
00080     virtual void stopWrite();
00081 
00082     virtual void startRead();
00083     virtual void stopRead();
00084 
00085 protected:
00086     FILE*    getFilePointer();
00087     uint64_t getCompressedIn();
00088     void     setCompressedIn(uint64_t nbytes);
00089     void     advanceOffset(uint64_t nbytes);
00090     char*    getUnused();
00091     int      getUnusedLength();
00092     void     setUnused(char* unused);
00093     void     setUnusedLength(int nUnused);
00094     void     clearUnused();
00095 
00096 protected:
00097     ChunkedFile* file_;
00098 };
00099 
00100 class ROSBAG_DECL StreamFactory
00101 {
00102 public:
00103     StreamFactory(ChunkedFile* file);
00104 
00105     boost::shared_ptr<Stream> getStream(CompressionType type) const;
00106 
00107 private:
00108     boost::shared_ptr<Stream> uncompressed_stream_;
00109     boost::shared_ptr<Stream> bz2_stream_;
00110     boost::shared_ptr<Stream> lz4_stream_;
00111 };
00112 
00113 class ROSBAG_DECL UncompressedStream : public Stream
00114 {
00115 public:
00116     UncompressedStream(ChunkedFile* file);
00117 
00118     CompressionType getCompressionType() const;
00119 
00120     void write(void* ptr, size_t size);
00121     void read(void* ptr, size_t size);
00122 
00123     void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
00124 };
00125 
00129 class ROSBAG_DECL BZ2Stream : public Stream
00130 {
00131 public:
00132     BZ2Stream(ChunkedFile* file);
00133 
00134     CompressionType getCompressionType() const;
00135 
00136     void startWrite();
00137     void write(void* ptr, size_t size);
00138     void stopWrite();
00139 
00140     void startRead();
00141     void read(void* ptr, size_t size);
00142     void stopRead();
00143 
00144     void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
00145 
00146 private:
00147     int     verbosity_;        
00148     int     block_size_100k_;  
00149     int     work_factor_;      
00150 
00151     BZFILE* bzfile_;           
00152     int     bzerror_;          
00153 };
00154 
00155 // LZ4Stream reads/writes compressed datat in the LZ4 format
00156 // https://code.google.com/p/lz4/
00157 class ROSBAG_DECL LZ4Stream : public Stream
00158 {
00159 public:
00160     LZ4Stream(ChunkedFile* file);
00161     ~LZ4Stream();
00162 
00163     CompressionType getCompressionType() const;
00164 
00165     void startWrite();
00166     void write(void* ptr, size_t size);
00167     void stopWrite();
00168 
00169     void startRead();
00170     void read(void* ptr, size_t size);
00171     void stopRead();
00172 
00173     void decompress(uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len);
00174 
00175 private:
00176     void writeStream(int action);
00177 
00178     char *buff_;
00179     int buff_size_;
00180     int block_size_id_;
00181     roslz4_stream lz4s_;
00182 };
00183 
00184 
00185 
00186 } // namespace rosbag
00187 
00188 #endif


rosbag_storage
Author(s):
autogenerated on Thu Jun 6 2019 21:09:52