Panda3D
zStreamBuf.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file zStreamBuf.h
10  * @author drose
11  * @date 2002-08-05
12  */
13 
14 #ifndef ZSTREAMBUF_H
15 #define ZSTREAMBUF_H
16 
17 #include "pandabase.h"
18 
19 // This module is not compiled if zlib is not available.
20 #ifdef HAVE_ZLIB
21 
22 #include <zlib.h>
23 
24 /**
25  * The streambuf object that implements IDecompressStream and OCompressStream.
26  */
27 class EXPCL_PANDA_EXPRESS ZStreamBuf : public std::streambuf {
28 public:
29  ZStreamBuf();
30  virtual ~ZStreamBuf();
31 
32  void open_read(std::istream *source, bool owns_source);
33  void close_read();
34 
35  void open_write(std::ostream *dest, bool owns_dest, int compression_level);
36  void close_write();
37 
38  virtual std::streampos seekoff(std::streamoff off, ios_seekdir dir, ios_openmode which);
39  virtual std::streampos seekpos(std::streampos pos, ios_openmode which);
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  std::istream *_source;
53  bool _owns_source;
54 
55  std::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, because
64  // zlib might not consume all of the input characters at each call to
65  // inflate(). This isn't a problem on output because in that case we can
66  // afford to wait until it does consume all of the characters we give it.
67  enum {
68  // It's not clear how large or small this buffer ought to be. It doesn't
69  // seem to matter much, especially since this is just a temporary holding
70  // area before getting copied into zlib's own internal buffers.
71  decompress_buffer_size = 128
72  };
73  char decompress_buffer[decompress_buffer_size];
74 };
75 
76 #endif // HAVE_ZLIB
77 
78 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.