BWAPI
Undermind/proxy/cpp/include/google/protobuf/io/gzip_stream.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: brianolson@google.com (Brian Olson)
00032 //
00033 // This file contains the definition for classes GzipInputStream and
00034 // GzipOutputStream.
00035 //
00036 // GzipInputStream decompresses data from an underlying
00037 // ZeroCopyInputStream and provides the decompressed data as a
00038 // ZeroCopyInputStream.
00039 //
00040 // GzipOutputStream is an ZeroCopyOutputStream that compresses data to
00041 // an underlying ZeroCopyOutputStream.
00042 
00043 #ifndef GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
00044 #define GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
00045 
00046 #include <zlib.h>
00047 
00048 #include <google/protobuf/io/zero_copy_stream.h>
00049 
00050 namespace google {
00051 namespace protobuf {
00052 namespace io {
00053 
00054 // A ZeroCopyInputStream that reads compressed data through zlib
00055 class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {
00056  public:
00057   // Format key for constructor
00058   enum Format {
00059     // zlib will autodetect gzip header or deflate stream
00060     AUTO = 0,
00061 
00062     // GZIP streams have some extra header data for file attributes.
00063     GZIP = 1,
00064 
00065     // Simpler zlib stream format.
00066     ZLIB = 2,
00067   };
00068 
00069   // buffer_size and format may be -1 for default of 64kB and GZIP format
00070   explicit GzipInputStream(
00071       ZeroCopyInputStream* sub_stream,
00072       Format format = AUTO,
00073       int buffer_size = -1);
00074   virtual ~GzipInputStream();
00075 
00076   // Return last error message or NULL if no error.
00077   inline const char* ZlibErrorMessage() const {
00078     return zcontext_.msg;
00079   }
00080   inline int ZlibErrorCode() const {
00081     return zerror_;
00082   }
00083 
00084   // implements ZeroCopyInputStream ----------------------------------
00085   bool Next(const void** data, int* size);
00086   void BackUp(int count);
00087   bool Skip(int count);
00088   int64 ByteCount() const;
00089 
00090  private:
00091   Format format_;
00092 
00093   ZeroCopyInputStream* sub_stream_;
00094 
00095   z_stream zcontext_;
00096   int zerror_;
00097 
00098   void* output_buffer_;
00099   void* output_position_;
00100   size_t output_buffer_length_;
00101 
00102   int Inflate(int flush);
00103   void DoNextOutput(const void** data, int* size);
00104 
00105   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipInputStream);
00106 };
00107 
00108 
00109 class LIBPROTOBUF_EXPORT GzipOutputStream : public ZeroCopyOutputStream {
00110  public:
00111   // Format key for constructor
00112   enum Format {
00113     // GZIP streams have some extra header data for file attributes.
00114     GZIP = 1,
00115 
00116     // Simpler zlib stream format.
00117     ZLIB = 2,
00118   };
00119 
00120   struct Options {
00121     // Defaults to GZIP.
00122     Format format;
00123 
00124     // What size buffer to use internally.  Defaults to 64kB.
00125     int buffer_size;
00126 
00127     // A number between 0 and 9, where 0 is no compression and 9 is best
00128     // compression.  Defaults to Z_DEFAULT_COMPRESSION (see zlib.h).
00129     int compression_level;
00130 
00131     // Defaults to Z_DEFAULT_STRATEGY.  Can also be set to Z_FILTERED,
00132     // Z_HUFFMAN_ONLY, or Z_RLE.  See the documentation for deflateInit2 in
00133     // zlib.h for definitions of these constants.
00134     int compression_strategy;
00135 
00136     Options();  // Initializes with default values.
00137   };
00138 
00139   // Create a GzipOutputStream with default options.
00140   explicit GzipOutputStream(ZeroCopyOutputStream* sub_stream);
00141 
00142   // Create a GzipOutputStream with the given options.
00143   GzipOutputStream(
00144       ZeroCopyOutputStream* sub_stream,
00145       const Options& options);
00146 
00147   // DEPRECATED:  Use one of the above constructors instead.
00148   GzipOutputStream(
00149       ZeroCopyOutputStream* sub_stream,
00150       Format format,
00151       int buffer_size = -1) GOOGLE_ATTRIBUTE_DEPRECATED;
00152 
00153   virtual ~GzipOutputStream();
00154 
00155   // Return last error message or NULL if no error.
00156   inline const char* ZlibErrorMessage() const {
00157     return zcontext_.msg;
00158   }
00159   inline int ZlibErrorCode() const {
00160     return zerror_;
00161   }
00162 
00163   // Flushes data written so far to zipped data in the underlying stream.
00164   // It is the caller's responsibility to flush the underlying stream if
00165   // necessary.
00166   // Compression may be less efficient stopping and starting around flushes.
00167   // Returns true if no error.
00168   bool Flush();
00169 
00170   // Writes out all data and closes the gzip stream.
00171   // It is the caller's responsibility to close the underlying stream if
00172   // necessary.
00173   // Returns true if no error.
00174   bool Close();
00175 
00176   // implements ZeroCopyOutputStream ---------------------------------
00177   bool Next(void** data, int* size);
00178   void BackUp(int count);
00179   int64 ByteCount() const;
00180 
00181  private:
00182   ZeroCopyOutputStream* sub_stream_;
00183   // Result from calling Next() on sub_stream_
00184   void* sub_data_;
00185   int sub_data_size_;
00186 
00187   z_stream zcontext_;
00188   int zerror_;
00189   void* input_buffer_;
00190   size_t input_buffer_length_;
00191 
00192   // Shared constructor code.
00193   void Init(ZeroCopyOutputStream* sub_stream, const Options& options);
00194 
00195   // Do some compression.
00196   // Takes zlib flush mode.
00197   // Returns zlib error code.
00198   int Deflate(int flush);
00199 
00200   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GzipOutputStream);
00201 };
00202 
00203 }  // namespace io
00204 }  // namespace protobuf
00205 
00206 }  // namespace google
00207 #endif  // GOOGLE_PROTOBUF_IO_GZIP_STREAM_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines