Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
GZipCodec.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef GZipCodec_INCLUDE_ONCE
33 #define GZipCodec_INCLUDE_ONCE
34 
35 #include <vlCore/VirtualFile.hpp>
36 struct z_stream_s;
37 
38 namespace vl
39 {
44  {
46 
47  // Lower this if you need to limit the amount of data allocated to the stack, for example to 16K.
48  static const int CHUNK_SIZE = 128*1024;
49 
50  public:
52  GZipCodec(VirtualFile* stream=NULL);
53 
55  GZipCodec(const String& gz_path);
56 
58  ~GZipCodec();
59 
63  virtual bool open(EOpenMode mode);
64 
66  virtual bool isOpen() const { return mReadBytes != -1; }
67 
69  virtual bool exists() const { return mStream ? mStream->exists() : false; }
70 
72  virtual void close();
73 
75  virtual long long size() const;
76 
78  virtual ref<VirtualFile> clone() const;
79 
80  GZipCodec& operator=(const GZipCodec& other);
81 
84  void setCompressionLevel(int level) { mCompressionLevel = level; }
85 
86  int compressionLevel() const { return mCompressionLevel; }
87 
89  void setStream(VirtualFile* stream);
90 
92  const VirtualFile* stream() const { return mStream.get(); }
93 
95  VirtualFile* stream() { return mStream.get(); }
96 
101  long long uncompressedSize();
102 
104  long long compressedSize() const { return stream() ? stream()->size() : -1; }
105 
107  float compressionRatio() const;
108 
109  bool warnOnSeek() const { return mWarnOnSeek; }
110 
111  void setWarnOnSeek(bool warn_on) { mWarnOnSeek = warn_on; }
112 
113  protected:
114  virtual long long read_Implementation(void* buffer, long long bytes_to_read);
115  virtual long long write_Implementation(const void* buffer, long long byte_count);
116  virtual long long position_Implementation() const;
117  void resetStream();
118  bool seekSet_Implementation(long long pos);
119  bool fillUncompressedBuffer();
120 
121  protected:
124  long long mReadBytes;
125  long long mWrittenBytes;
127 
128  z_stream_s* mZStream;
129  unsigned char mZipBufferIn[CHUNK_SIZE];
130  unsigned char mZipBufferOut[CHUNK_SIZE];
131  std::vector<char> mUncompressedBuffer;
133  long long mStreamSize;
134  long long mUncompressedSize;
135  enum { ZNone, ZCompress, ZDecompress } mMode;
136  };
137 }
138 
139 #endif
int mUncompressedBufferPtr
Definition: GZipCodec.hpp:132
long long compressedSize() const
Returns the size of the compressed stream as returned by stream()->size().
Definition: GZipCodec.hpp:104
An abstract class representing a file.
Definition: VirtualFile.hpp:60
void setWarnOnSeek(bool warn_on)
Definition: GZipCodec.hpp:111
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
long long mUncompressedSize
Definition: GZipCodec.hpp:134
long long mWrittenBytes
Definition: GZipCodec.hpp:125
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
int mCompressionLevel
Definition: GZipCodec.hpp:122
void setCompressionLevel(int level)
Sets the compression level used during write operations.
Definition: GZipCodec.hpp:84
Visualization Library main namespace.
long long mStreamSize
Definition: GZipCodec.hpp:133
std::vector< char > mUncompressedBuffer
Definition: GZipCodec.hpp:131
virtual bool exists() const
Returns true if the input or output stream file exists.
Definition: GZipCodec.hpp:69
The GZipCodec class is a VirtualFile that transparently encodes and decodes a stream of data using th...
Definition: GZipCodec.hpp:43
#define NULL
Definition: OpenGLDefs.hpp:81
virtual long long size() const =0
Returns the size of the file in bytes.
VirtualFile * stream()
Returns the VirtualFile representing the GZip file to be read or to be written.
Definition: GZipCodec.hpp:95
long long mReadBytes
Definition: GZipCodec.hpp:124
virtual bool isOpen() const
Returns true if the file is open for read or writing.
Definition: GZipCodec.hpp:66
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
ref< VirtualFile > mStream
Definition: GZipCodec.hpp:123
bool warnOnSeek() const
Definition: GZipCodec.hpp:109
int compressionLevel() const
Definition: GZipCodec.hpp:86
const VirtualFile * stream() const
Returns the VirtualFile representing the GZip file to be read or to be written.
Definition: GZipCodec.hpp:92
z_stream_s * mZStream
Definition: GZipCodec.hpp:128