41#ifndef _PWP_BYTECOMPR_H_
42#define _PWP_BYTECOMPR_H_
76 void write (
const char *buf,
size_t n);
132 int code = BZ2_bzCompressInit (
bzStream, 9, 0, 0);
135 case BZ_CONFIG_ERROR:
136 throw "BZ2_bzCompressInit config error.";
138 throw "BZ2_bzCompressInit wrong parameters.";
140 throw "BZ2_bzCompressInit memory error.";
144 throw "BZ2_bzCompressInit returned an unknown code.";
157 bool finished (
false);
158 while ((finishing && !finished) || (startPos <
inPos))
171 int code = BZ2_bzCompress (
bzStream,
172 finishing ? BZ_FINISH : BZ_RUN);
175 case BZ_SEQUENCE_ERROR:
176 throw "BZ2_bzCompress sequence error.";
181 throw "BZ2_bzCompress unexpected stream end.";
188 throw "BZ2_bzCompress returned an unknown code.";
200 if (finishing && finished)
214 throw "Compression buffer size too small. Fix the code!";
218 throw "Not enough buffer space to compress data.";
219 for (
size_t i = 0; i < n; ++ i)
234 int code = BZ2_bzCompressEnd (
bzStream);
238 throw "BZ2_bzCompressEnd: wrong bz stream data structure";
242 throw "BZ2_bzCompressEnd returned an unknown code.";
268#define DEFBYTEWRITE(var_type) \
269inline ByteCompressor &operator << (ByteCompressor &c, const var_type &n) \
271 c. write (reinterpret_cast<const char *> (&n), sizeof (var_type)); \
312 void read (
char *buf,
size_t n);
386 int code = BZ2_bzDecompressInit (
bzStream, 0, 0);
389 case BZ_CONFIG_ERROR:
390 throw "BZ2_bzDecompressInit config error.";
392 throw "BZ2_bzDecompressInit wrong parameters.";
394 throw "BZ2_bzDecompressInit memory error.";
398 throw "BZ2_bzDecompressInit returned an unknown code.";
425 *(outBegin ++) = *(outCurrent ++);
452 *(inBegin ++) = *(inCurrent ++);
472 int code = BZ2_bzDecompress (
bzStream);
476 throw "BZ2_bzDecompress parameter error.";
478 throw "BZ2_bzDecompress data integrity error.";
479 case BZ_DATA_ERROR_MAGIC:
480 throw "BZ2_bzDecompress magic data error.";
482 throw "BZ2_bzDecompress not enough memory available.";
490 throw "BZ2_bzDecompress returned an unknown code.";
507 throw "Compression buffer size too small. Fix the code!";
511 throw "Not enough data read from the compressed stream.";
512 for (
size_t i = 0; i < n; ++ i)
524 int code = BZ2_bzDecompressEnd (
bzStream);
528 throw "BZ2_bzDecompressEnd: wrong bz stream data structure";
532 throw "BZ2_bzDecompressEnd returned an unknown code.";
558#define DEFBYTEREAD(var_type) \
559inline ByteDecompressor &operator >> (ByteDecompressor &c, var_type &n) \
561 c. read (reinterpret_cast<char *> (&n), sizeof (var_type)); \
#define DEFBYTEWRITE(var_type)
Writes a variable to a byte compressor stream.
#define DEFBYTEREAD(var_type)
Reads a variable from a byte decompressor stream.
A simple wrapper for the bzip2 data compression to an output stream.
ByteCompressor(const ByteCompressor &)
ByteCompressor & operator=(const ByteCompressor &)
std::ostream & outStream
A reference to the output stream to which the data should be written.
void close()
Ends the compression and writes the remaining data to the stream.
char * inBuffer
An internal buffer for caching data for compression.
size_t inPos
The current position in the internal buffer for compression.
char * outBuffer
An internal buffer for caching compressed data.
ByteCompressor(std::ostream &out)
The constructor of an output stream for storing binary data.
bz_stream * bzStream
The bzip2 data stream information.
void write(const char *buf, size_t n)
Writes the given number of bytes to the compression stream.
void compress(bool finishing)
Compresses the input buffer and writes the resulting data to the output stream.
~ByteCompressor()
The destructor that ends the compression procedure and releases the internal memory buffers.
size_t bufSize
The size of each of the internal buffers.
A simple wrapper for the bzip2 data decompression from an input stream.
void close()
Ends the decompression and deletes the bzip2 structure.
bz_stream * bzStream
The bzip2 data stream information.
size_t inPos
The current position in the internal buffer with compressed data.
void read(char *buf, size_t n)
Reads the given number of bytes from the decompressed stream.
ByteDecompressor(std::istream &in)
The constructor of an input stream for retrieving binary data.
std::istream & inStream
A reference to the input stream from which the compressed data should be read.
char * inBuffer
An internal buffer for caching data read from the input stream.
~ByteDecompressor()
The destructor that ends the decompression procedure and releases the internal memory buffers.
ByteDecompressor & operator=(const ByteDecompressor &)
size_t bufSize
The size of each of the internal buffers.
size_t outPos
The current position in the internal buffer with decompressed data.
char * outBuffer
An internal buffer for caching decompressed data.
size_t inPosEnd
The end of the compressed data in the internal buffer.
bool finished
Is the decompression procedure completed?
ByteDecompressor(const ByteDecompressor &)
size_t outPosEnd
The position of the end of the decompressed data in the internal buffer.
void decompress()
Decompresses data from the input buffer (and the input stream) to provide more data for reading if po...