6 #include "Alignment/Geners/interface/BZ2Handle.hh"
7 #include "Alignment/Geners/interface/CStringStream.hh"
8 #include "Alignment/Geners/interface/IOException.hh"
11 const unsigned long long len,
15 const unsigned long long bufLen,
21 strm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(
data));
24 strm.next_out = reinterpret_cast<Bytef *>(
buffer);
25 strm.avail_out = bufLen;
26 status = defl ? deflate(&strm, Z_FINISH) : inflate(&strm, Z_NO_FLUSH);
28 const unsigned have = bufLen - strm.avail_out;
31 throw gs::IOWriteFailure(
"In gs::doZlibCompression: sink stream failure");
32 }
while (strm.avail_out == 0);
35 assert(strm.avail_in == 0);
37 assert(deflateReset(&strm) == Z_OK);
39 assert(inflateReset(&strm) == Z_OK);
43 const unsigned long long len,
47 const unsigned long long bufLen,
53 strm.next_in = const_cast<char *>(
data);
57 strm.avail_out = bufLen;
58 status = defl ? BZ2_bzCompress(&strm, BZ_FINISH) : BZ2_bzDecompress(&strm);
60 const unsigned have = bufLen - strm.avail_out;
63 throw gs::IOWriteFailure(
"In gs::doBZ2Compression: sink stream failure");
64 }
while (
status != BZ_STREAM_END);
68 CStringStream::CStringStream(
const CompressionMode
m,
70 const unsigned minSizeToCompress,
71 const unsigned bufSize)
74 minSizeToCompress_(minSizeToCompress),
78 comprBuf_(bufSize > 1024
U ? bufSize : 1024
U),
83 void CStringStream::setCompressionMode(
const CompressionMode newmode) {
94 void CStringStream::readCompressed(std::istream &
in,
const unsigned compressionCode,
const unsigned long long len) {
100 if (len > readBuf_.size())
101 readBuf_.resize(len);
102 in.read(&readBuf_[0], len);
104 switch (static_cast<CompressionMode>(compressionCode)) {
106 this->
write(&readBuf_[0], len);
110 if (!inflator_.get())
111 inflator_ = CPP11_auto_ptr<ZlibInflateHandle>(
new ZlibInflateHandle());
112 doZlibCompression(&readBuf_[0], len,
false, inflator_->strm(), &comprBuf_[0], comprBuf_.size(), *
this);
119 BZ2InflateHandle
h(strm);
120 doBZ2Compression(&readBuf_[0], len,
false, strm, &comprBuf_[0], comprBuf_.size(), *
this);
124 assert(!
"Unhandled switch case in "
125 "CStringStream::readCompressed. "
126 "This is a bug. Please report.");
130 CStringStream::CompressionMode CStringStream::writeCompressed() {
134 unsigned long long len = 0;
135 const char *
data = buf_.getPutBuffer(&len);
137 return NOT_COMPRESSED;
139 if (mode_ == NOT_COMPRESSED || len < minSizeToCompress_) {
140 sink_->write(
data, len);
141 return NOT_COMPRESSED;
146 if (!deflator_.get())
147 deflator_ = CPP11_auto_ptr<ZlibDeflateHandle>(
new ZlibDeflateHandle(compressionLevel_));
155 BZ2DeflateHandle
h(strm);
160 assert(!
"Unhandled switch case in "
161 "CStringStream::writeCompressed. "
162 "This is a bug. Please report.");
169 bool CStringStream::getCompressionModeByName(
const char *
name, CompressionMode *
m) {
170 static const char *
names[] = {
"n",
"z",
"b"};
173 for (
unsigned i = 0;
i <
sizeof(
names) /
sizeof(
names[0]); ++
i)
175 *
m = static_cast<CompressionMode>(
i);
181 std::string CStringStream::compressionModeName(
const CompressionMode
m,
const bool useShortName) {
185 mode = useShortName ?
"n" :
"not compressed";
188 mode = useShortName ?
"z" :
"zlib";
191 mode = useShortName ?
"b" :
"bzip2";
194 assert(!
"Unhandled switch case in "
195 "CStringStream::compressionModeName. "
196 "This is a bug. Please report.");