From: Christian P. <cp...@us...> - 2005-01-07 13:43:08
|
Update of /cvsroot/pclasses/pclasses2/src/IO In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5188/src/IO Added Files: BZip2IOFilter.cpp Log Message: Added BZip2Filter.[h|cpp] --- NEW FILE: BZip2IOFilter.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include <pclasses/IO/BZip2IOFilter.h> namespace P { namespace IO { BZip2IOFilter::BZip2IOFilter() : _strmOut(0), _strmIn(0) { } BZip2IOFilter::~BZip2IOFilter() { if(_strmOut) delete _strmOut; if(_strmIn) delete _strmIn; } IOFilter* BZip2IOFilter::create() { return new BZip2IOFilter(); } void BZip2IOFilter::sync(bool close) { // sync output stream ... if(_strmOut) { if(close) { while(!_strmOut->finish()) flushAvailBytes(); flushAvailBytes(); _strmOut->reset(); } else { _strmOut->sync(); flushAvailBytes(); } } if(_strmIn) { if(close) _strmIn->reset(); } IOFilter::sync(close); } bool BZip2IOFilter::isSeekable() { // seeking is too complex (for now ;-) return false; } bool BZip2IOFilter::eof() const throw() { if(_strmIn) return IOFilter::eof() && _strmIn->eof(); return IOFilter::eof(); } size_t BZip2IOFilter::read(char* buffer, size_t count) throw(IO::IOError) { if(!_strmIn) _strmIn = new P::IO::BZip2InputStream(); if(_strmIn->bytesAvail() < count) { // refill output buffer ... char tmp[1024]; size_t got = IOFilter::read(tmp, sizeof(tmp)); size_t consumed = _strmIn->decompress(tmp, got); if(consumed < sizeof(tmp)) { //@fixme: we need to buffer rememaining bytes we could not inflate } count = _strmIn->bytesAvail(); } // dequeue bytes available in output buffer ... memcpy(buffer, _strmIn->buffer(), count); _strmIn->dequeue(count); return count; } size_t BZip2IOFilter::write(const char* buffer, size_t count) throw(IO::IOError) { if(!_strmOut) _strmOut = new P::IO::BZip2OutputStream(9); size_t consumed = _strmOut->compress(buffer, count); flushAvailBytes(); return consumed; } size_t BZip2IOFilter::peek(char* buffer, size_t count) throw(IO::IOError) { return 0; } offset_t BZip2IOFilter::seek(offset_t offset, IO::IODevice::SeekMode mode) throw(IO::IOError) { throw IO::IOError(0, "Could not seek on device", P_SOURCEINFO); return -1; } offset_t BZip2IOFilter::size() const throw(IO::IOError) { //@fixme!!! return IOFilter::size(); } void BZip2IOFilter::flushAvailBytes() { size_t avail = _strmOut->bytesAvail(); if(avail) _strmOut->dequeue(IOFilter::write(_strmOut->buffer(), avail)); } } } |