📄 deflatingstream.cpp
字号:
//
// DeflatingStream.cpp
//
// $Id: //poco/Main/Foundation/src/DeflatingStream.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/DeflatingStream.h"
#include "Foundation/Exception.h"
Foundation_BEGIN
DeflatingStreamBuf::DeflatingStreamBuf(std::istream& istr, StreamType type, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
_pIstr(&istr),
_pOstr(0),
_eof(false)
{
_zstr.zalloc = Z_NULL;
_zstr.zfree = Z_NULL;
_zstr.opaque = Z_NULL;
_zstr.next_in = 0;
_zstr.avail_in = 0;
_zstr.next_out = 0;
_zstr.avail_out = 0;
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK) throw IOException(zError(rc));
_buffer = new char[DEFLATE_BUFFER_SIZE];
}
DeflatingStreamBuf::DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level):
BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
_pIstr(0),
_pOstr(&ostr),
_eof(false)
{
_zstr.zalloc = Z_NULL;
_zstr.zfree = Z_NULL;
_zstr.opaque = Z_NULL;
_zstr.next_in = 0;
_zstr.avail_in = 0;
_zstr.next_out = 0;
_zstr.avail_out = 0;
int rc = deflateInit2(&_zstr, level, Z_DEFLATED, 15 + (type == STREAM_GZIP ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
if (rc != Z_OK) throw IOException(zError(rc));
_buffer = new char[DEFLATE_BUFFER_SIZE];
}
DeflatingStreamBuf::~DeflatingStreamBuf()
{
close();
delete [] _buffer;
}
int DeflatingStreamBuf::close()
{
sync();
if (_pIstr)
{
int rc = deflateEnd(&_zstr);
if (rc != Z_OK) throw IOException(zError(rc));
_pIstr = 0;
}
else if (_pOstr)
{
if (_zstr.next_out)
{
int rc = deflate(&_zstr, Z_FINISH);
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));
_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));
_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 = deflateEnd(&_zstr);
if (rc != Z_OK) throw IOException(zError(rc));
}
_pOstr = 0;
}
return 0;
}
int DeflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length)
{
if (!_pIstr) return 0;
if (!_zstr.next_in && !_eof)
{
int n = 0;
if (_pIstr->good())
{
_pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
n = _pIstr->gcount();
}
if (n > 0)
{
_zstr.next_in = (unsigned char*) _buffer;
_zstr.avail_in = n;
}
else
{
_zstr.next_in = 0;
_zstr.avail_in = 0;
_eof = true;
}
_zstr.next_out = (unsigned char*) buffer;
_zstr.avail_out = length;
}
else if (_zstr.avail_out == 0)
{
_zstr.next_out = (unsigned char*) buffer;
_zstr.avail_out = length;
}
for (;;)
{
int rc = deflate(&_zstr, _eof ? Z_FINISH : Z_NO_FLUSH);
if (_eof && rc == Z_STREAM_END)
{
rc = deflateEnd(&_zstr);
if (rc != Z_OK) throw IOException(zError(rc));
_pIstr = 0;
return length - _zstr.avail_out;
}
if (rc != Z_OK) throw IOException(zError(rc));
if (_zstr.avail_out == 0)
{
return length;
}
if (_zstr.avail_in == 0)
{
int n = 0;
if (_pIstr->good())
{
_pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
n = _pIstr->gcount();
}
if (n > 0)
{
_zstr.next_in = (unsigned char*) _buffer;
_zstr.avail_in = n;
}
else
{
_zstr.next_in = 0;
_zstr.avail_in = 0;
_eof = true;
}
}
}
}
int DeflatingStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
{
if (length == 0 || !_pOstr) return 0;
_zstr.next_in = (unsigned char*) buffer;
_zstr.avail_in = length;
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
for (;;)
{
int rc = deflate(&_zstr, Z_NO_FLUSH);
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));
_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));
_zstr.next_out = (unsigned char*) _buffer;
_zstr.avail_out = DEFLATE_BUFFER_SIZE;
break;
}
}
return length;
}
DeflatingIOS::DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type, int level):
_buf(ostr, type, level)
{
init(&_buf);
}
DeflatingIOS::DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type, int level):
_buf(istr, type, level)
{
init(&_buf);
}
DeflatingIOS::~DeflatingIOS()
{
}
DeflatingStreamBuf* DeflatingIOS::rdbuf()
{
return &_buf;
}
DeflatingOutputStream::DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type, int level):
DeflatingIOS(ostr, type, level),
std::ostream(&_buf)
{
}
DeflatingOutputStream::~DeflatingOutputStream()
{
}
int DeflatingOutputStream::close()
{
return _buf.close();
}
DeflatingInputStream::DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type, int level):
DeflatingIOS(istr, type, level),
std::istream(&_buf)
{
}
DeflatingInputStream::~DeflatingInputStream()
{
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -