fixed GH #1828: DeflatingStreamBuf::sync() should also flush underlying stream.

This commit is contained in:
Günter Obiltschnig 2017-11-07 17:54:08 +01:00
parent b96b07a9ad
commit a8ab414ea8

View File

@ -19,7 +19,7 @@
namespace Poco {
DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, StreamType type, int level):
DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, StreamType type, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
_pIstr(&istr),
_pOstr(0),
@ -43,15 +43,15 @@ DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, StreamType type, int
_buffer = new char[DEFLATE_BUFFER_SIZE];
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK)
if (rc != Z_OK)
{
delete [] _buffer;
throw IOException(zError(rc));
throw IOException(zError(rc));
}
}
DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, int windowBits, int level):
DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, int windowBits, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
_pIstr(&istr),
_pOstr(0),
@ -68,15 +68,15 @@ DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, int windowBits, int l
_buffer = new char[DEFLATE_BUFFER_SIZE];
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK)
if (rc != Z_OK)
{
delete [] _buffer;
throw IOException(zError(rc));
throw IOException(zError(rc));
}
}
DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level):
DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
_pIstr(0),
_pOstr(&ostr),
@ -93,15 +93,15 @@ DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, StreamType type, int
_buffer = new char[DEFLATE_BUFFER_SIZE];
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK)
if (rc != Z_OK)
{
delete [] _buffer;
throw IOException(zError(rc));
throw IOException(zError(rc));
}
}
DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, int windowBits, int level):
DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, int windowBits, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
_pIstr(0),
_pOstr(&ostr),
@ -118,10 +118,10 @@ DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, int windowBits, int l
_buffer = new char[DEFLATE_BUFFER_SIZE];
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK)
if (rc != Z_OK)
{
delete [] _buffer;
throw IOException(zError(rc));
throw IOException(zError(rc));
}
}
@ -149,17 +149,17 @@ int DeflatingStreamBuf::close()
if (_zstr.next_out)
{
int rc = deflate(&_zstr, Z_FINISH);
if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
if (!_pOstr->good()) throw IOException(zError(rc));
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
while (rc != Z_STREAM_END)
{
rc = deflate(&_zstr, Z_FINISH);
if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
if (rc != Z_OK && rc != Z_STREAM_END) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
if (!_pOstr->good()) throw IOException(zError(rc));
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
}
@ -176,23 +176,27 @@ int DeflatingStreamBuf::sync()
if (BufferedStreamBuf::sync())
return -1;
if (_pOstr && _zstr.next_out)
if (_pOstr)
{
int rc = deflate(&_zstr, Z_SYNC_FLUSH);
if (rc != Z_OK) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
while (_zstr.avail_out == 0)
if (_zstr.next_out)
{
int rc = deflate(&_zstr, Z_SYNC_FLUSH);
if (rc != Z_OK) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
while (_zstr.avail_out == 0)
{
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
rc = deflate(&_zstr, Z_SYNC_FLUSH);
if (rc != Z_OK) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
};
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
rc = deflate(&_zstr, Z_SYNC_FLUSH);
if (rc != Z_OK) throw IOException(zError(rc));
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
};
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
}
_pOstr->flush();
}
return 0;
}
@ -226,12 +230,12 @@ int DeflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length)
for (;;)
{
int rc = deflate(&_zstr, _eof ? Z_FINISH : Z_NO_FLUSH);
if (_eof && rc == Z_STREAM_END)
if (_eof && rc == Z_STREAM_END)
{
_pIstr = 0;
return static_cast<int>(length) - _zstr.avail_out;
}
if (rc != Z_OK) throw IOException(zError(rc));
if (rc != Z_OK) throw IOException(zError(rc));
if (_zstr.avail_out == 0)
{
return static_cast<int>(length);
@ -271,18 +275,18 @@ int DeflatingStreamBuf::writeToDevice(const char* buffer, std::streamsize length
for (;;)
{
int rc = deflate(&_zstr, Z_NO_FLUSH);
if (rc != Z_OK) throw IOException(zError(rc));
if (rc != Z_OK) throw IOException(zError(rc));
if (_zstr.avail_out == 0)
{
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE);
if (!_pOstr->good()) throw IOException(zError(rc));
if (!_pOstr->good()) throw IOException(zError(rc));
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
}
if (_zstr.avail_in == 0)
{
_pOstr->write(_buffer, DEFLATE_BUFFER_SIZE - _zstr.avail_out);
if (!_pOstr->good()) throw IOException(zError(rc));
if (!_pOstr->good()) throw IOException(zError(rc));
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
break;