Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
cheprep::DeflateOutputStreamBuffer Class Reference

#include <DeflateOutputStreamBuffer.h>

+ Inheritance diagram for cheprep::DeflateOutputStreamBuffer:

Public Member Functions

 DeflateOutputStreamBuffer (std::streambuf *buffer)
 
void init (bool compress)
 
void finish ()
 
virtual ~DeflateOutputStreamBuffer ()
 

Protected Member Functions

int overflow (int c=EOF)
 
bool flushOut ()
 
void putUI (unsigned int ui)
 
void putUS (unsigned short us)
 
void putUB (unsigned char ub)
 
void putS (const std::string s)
 
std::streampos pos ()
 
unsigned int getSize ()
 
unsigned int getCRC ()
 

Detailed Description

Definition at line 20 of file DeflateOutputStreamBuffer.h.

Constructor & Destructor Documentation

◆ DeflateOutputStreamBuffer()

cheprep::DeflateOutputStreamBuffer::DeflateOutputStreamBuffer ( std::streambuf *  buffer)

Definition at line 80 of file DeflateOutputStreamBuffer.cc.

81 : buffer(aBuffer)
82 , crc(0)
83 , size(0)
84#ifndef CHEPREP_NO_ZLIB
85 , zStreamOpen(false)
86 , in(inSize)
87 , out(outSize)
88#endif // CHEPREP_NO_ZLIB
89 {
90
91#ifndef CHEPREP_NO_ZLIB
92 zStream.zalloc = Z_NULL;
93 zStream.zfree = Z_NULL;
94 zStream.opaque = Z_NULL;
95#endif // CHEPREP_NO_ZLIB
96 }
alloc_func zalloc
Definition: zlib.h:94
free_func zfree
Definition: zlib.h:95
voidpf opaque
Definition: zlib.h:96
#define Z_NULL
Definition: zlib.h:180

◆ ~DeflateOutputStreamBuffer()

cheprep::DeflateOutputStreamBuffer::~DeflateOutputStreamBuffer ( )
virtual

Definition at line 158 of file DeflateOutputStreamBuffer.cc.

158 {
159 }

Member Function Documentation

◆ finish()

void cheprep::DeflateOutputStreamBuffer::finish ( )

Definition at line 125 of file DeflateOutputStreamBuffer.cc.

125 {
126
127#ifndef CHEPREP_NO_ZLIB
128 if (zStreamOpen) {
129
130 overflow() ;
131
132 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
133 zStream.avail_out = outSize;
134
135 int err = Z_OK ;
136 while ( err == Z_OK ) {
137 if (zStream.avail_out == 0) {
138 flushOut();
139 }
140 err = deflate(&zStream, Z_FINISH);
141 }
142
143 flushOut() ;
144
145 if (err != Z_STREAM_END) {
146 cerr << "ERROR: deflation failed" << endl;
147 }
148
149 if (deflateEnd(&zStream) != Z_OK) {
150 cerr << "ERROR: deflateEnd failed" << endl;
151 }
152
153 zStreamOpen = false ;
154 }
155#endif // CHEPREP_NO_ZLIB
156 }
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.cc:670
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.cc:496
uInt avail_out
Definition: zlib.h:88
Bytef * next_out
Definition: zlib.h:87
#define Z_STREAM_END
Definition: zlib.h:148
#define Z_FINISH
Definition: zlib.h:143
#define Z_OK
Definition: zlib.h:147

Referenced by cheprep::GZIPOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::closeEntry().

◆ flushOut()

bool cheprep::DeflateOutputStreamBuffer::flushOut ( )
protected

Definition at line 230 of file DeflateOutputStreamBuffer.cc.

230 {
231 int deflatedCount = outSize - zStream.avail_out;
232 int byteCount = buffer->sputn(&(out[0]), deflatedCount);
233
234 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
235 zStream.avail_out = outSize ;
236
237 return deflatedCount == byteCount ;
238 }

Referenced by finish(), and overflow().

◆ getCRC()

unsigned int cheprep::DeflateOutputStreamBuffer::getCRC ( )
inlineprotected

Definition at line 72 of file DeflateOutputStreamBuffer.h.

72 {
73 return crc;
74 }

Referenced by cheprep::ZipOutputStreamBuffer::closeEntry().

◆ getSize()

unsigned int cheprep::DeflateOutputStreamBuffer::getSize ( )
inlineprotected

Definition at line 68 of file DeflateOutputStreamBuffer.h.

68 {
69 return size;
70 }

Referenced by cheprep::ZipOutputStreamBuffer::closeEntry().

◆ init()

void cheprep::DeflateOutputStreamBuffer::init ( bool  compress)

Definition at line 99 of file DeflateOutputStreamBuffer.cc.

99 {
100
101 if (compress) {
102 if (zStreamOpen) return;
103
104 zStream.next_in = reinterpret_cast<unsigned char *>(&(in[0]));
105 zStream.avail_in = 0 ;
106
107 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
108 zStream.avail_out = out.size();
109
110 if (deflateInit2(&zStream, 6, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY ) != Z_OK) {
111 cerr << "ERROR: deflateInit2 failed" << endl;
112 } else {
113 zStreamOpen = true;
114 setp(&(in[0]), &(in[0])+inSize);
115 }
116 }
117#else
118 void DeflateOutputStreamBuffer::init(bool /*compress*/) {
119#endif // CHEPREP_NO_ZLIB
120
121 crc = 0;
122 size = 0;
123 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.cc:57
uInt avail_in
Definition: zlib.h:84
Bytef * next_in
Definition: zlib.h:83
#define MAX_WBITS
Definition: zconf.h:142
#define Z_DEFLATED
Definition: zlib.h:177
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:169
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1178

Referenced by cheprep::GZIPOutputStreamBuffer::GZIPOutputStreamBuffer(), init(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ overflow()

int cheprep::DeflateOutputStreamBuffer::overflow ( int  c = EOF)
protected

Definition at line 168 of file DeflateOutputStreamBuffer.cc.

168 {
169
170#ifndef CHEPREP_NO_ZLIB
171 if (zStreamOpen) {
172 zStream.avail_in = pptr() - pbase() ;
173 zStream.next_in = reinterpret_cast<unsigned char *>(&(in[0]));
174
175 int len = zStream.avail_in;
176 unsigned char* buf = zStream.next_in;
177
178 crc = crc ^ 0xffffffffUL;
179 while (len >= 8) {
180 DO8;
181 len -= 8;
182 }
183 if (len) do {
184 DO1;
185 } while (--len);
186 crc = crc ^ 0xffffffffUL;
187
188 size += zStream.avail_in;
189
190 zStream.next_out = reinterpret_cast<unsigned char *>(&(out[0]));
191 zStream.avail_out = outSize ;
192
193 int err = Z_OK ;
194 while ((zStream.avail_in > 0 || zStream.avail_out == 0) && err == Z_OK) {
195 if (zStream.avail_out == 0 ) {
196 flushOut();
197 }
198 err = deflate(&zStream, Z_NO_FLUSH);
199 }
200
201 flushOut();
202
203 setp(&(in[0]), &(in[0]) + inSize);
204
205 if ((err != Z_OK) && (err != Z_STREAM_END)) {
206 cerr << "ERROR: deflation failed" << endl;
207 return EOF ;
208 }
209
210 if ( c != EOF ) {
211 *pptr() = c ;
212 pbump(1);
213 }
214
215 return 0 ;
216 } else {
217#endif // CHEPREP_NO_ZLIB
218 crc = crc ^ 0xffffffffUL;
219 crc = crctable[(crc ^ c) & 0xff] ^ (crc >> 8);
220 crc = crc ^ 0xffffffffUL;
221 size++;
222 return buffer->sputc((char)c);
223#ifndef CHEPREP_NO_ZLIB
224 }
225#endif // CHEPREP_NO_ZLIB
226 }
#define DO1(buf, i)
Definition: adler32.cc:15
#define DO8(buf, i)
Definition: adler32.cc:18
#define Z_NO_FLUSH
Definition: zlib.h:139

Referenced by finish(), cheprep::ZipOutputStreamBuffer::overflow(), and cheprep::GZIPOutputStreamBuffer::overflow().

◆ pos()

std::streampos cheprep::DeflateOutputStreamBuffer::pos ( )
inlineprotected

Definition at line 63 of file DeflateOutputStreamBuffer.h.

63 {
64 std::ostream os(buffer);
65 return os.tellp();
66 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), cheprep::ZipOutputStreamBuffer::closeEntry(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putS()

void cheprep::DeflateOutputStreamBuffer::putS ( const std::string  s)
inlineprotected

Definition at line 59 of file DeflateOutputStreamBuffer.h.

59 {
60 buffer->sputn(s.c_str(), s.length());
61 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putUB()

void cheprep::DeflateOutputStreamBuffer::putUB ( unsigned char  ub)
inlineprotected

Definition at line 55 of file DeflateOutputStreamBuffer.h.

55 {
56 buffer->sputc(ub);
57 }

◆ putUI()

void cheprep::DeflateOutputStreamBuffer::putUI ( unsigned int  ui)
inlineprotected

Definition at line 39 of file DeflateOutputStreamBuffer.h.

39 {
40 unsigned char* ucp = reinterpret_cast<unsigned char *>(&ui);
41 unsigned int i = (static_cast<unsigned int>(ucp[ 3 ]) << 24) +
42 (static_cast<unsigned int>(ucp[ 2 ]) << 16) +
43 (static_cast<unsigned int>(ucp[ 1 ]) << 8 ) +
44 (static_cast<unsigned int>(ucp[ 0 ]));
45 buffer->sputn(reinterpret_cast<char *>(&i), sizeof(unsigned int));
46 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), cheprep::ZipOutputStreamBuffer::closeEntry(), and cheprep::ZipOutputStreamBuffer::putNextEntry().

◆ putUS()

void cheprep::DeflateOutputStreamBuffer::putUS ( unsigned short  us)
inlineprotected

Definition at line 48 of file DeflateOutputStreamBuffer.h.

48 {
49 unsigned char* ucp = reinterpret_cast<unsigned char *>(&us);
50 unsigned short s = (static_cast<unsigned short>(ucp[ 1 ]) << 8 ) +
51 (static_cast<unsigned short>(ucp[ 0 ]));
52 buffer->sputn(reinterpret_cast<char *>(&s), sizeof(unsigned short));
53 }

Referenced by cheprep::ZipOutputStreamBuffer::close(), and cheprep::ZipOutputStreamBuffer::putNextEntry().


The documentation for this class was generated from the following files: