Panda3D
zStreamBuf.h
1 // Filename: zStreamBuf.h
2 // Created by: drose (05Aug02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef ZSTREAMBUF_H
16 #define ZSTREAMBUF_H
17 
18 #include "pandabase.h"
19 
20 // This module is not compiled if zlib is not available.
21 #ifdef HAVE_ZLIB
22 
23 #include <zlib.h>
24 
25 ////////////////////////////////////////////////////////////////////
26 // Class : ZStreamBuf
27 // Description : The streambuf object that implements
28 // IDecompressStream and OCompressStream.
29 ////////////////////////////////////////////////////////////////////
30 class EXPCL_PANDAEXPRESS ZStreamBuf : public streambuf {
31 public:
32  ZStreamBuf();
33  virtual ~ZStreamBuf();
34 
35  void open_read(istream *source, bool owns_source);
36  void close_read();
37 
38  void open_write(ostream *dest, bool owns_dest, int compression_level);
39  void close_write();
40 
41 protected:
42  virtual int overflow(int c);
43  virtual int sync();
44  virtual int underflow();
45 
46 private:
47  size_t read_chars(char *start, size_t length);
48  void write_chars(const char *start, size_t length, int flush);
49  void show_zlib_error(const char *function, int error_code, z_stream &z);
50 
51 private:
52  istream *_source;
53  bool _owns_source;
54 
55  ostream *_dest;
56  bool _owns_dest;
57 
58  z_stream _z_source;
59  z_stream _z_dest;
60 
61  char *_buffer;
62 
63  // We need to store the decompression buffer on the class object,
64  // because zlib might not consume all of the input characters at
65  // each call to inflate(). This isn't a problem on output because
66  // in that case we can afford to wait until it does consume all of
67  // the characters we give it.
68  enum {
69  // It's not clear how large or small this buffer ought to be. It
70  // doesn't seem to matter much, especially since this is just a
71  // temporary holding area before getting copied into zlib's own
72  // internal buffers.
73  decompress_buffer_size = 128
74  };
75  char decompress_buffer[decompress_buffer_size];
76 };
77 
78 #endif // HAVE_ZLIB
79 
80 #endif