/** * libtransport -- C++ library for easy XMPP Transports development * * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */#include"transport/memoryreadbytestream.h"#include<boost/foreach.hpp>namespaceTransport{MemoryReadBytestream::MemoryReadBytestream(unsignedlongsize){neededData=false;m_finished=false;m_sent=0;m_size=size;}MemoryReadBytestream::~MemoryReadBytestream(){}unsignedlongMemoryReadBytestream::appendData(conststd::string&data){m_data+=data;onDataAvailable();neededData=false;returnm_data.size();}boost::shared_ptr<std::vector<unsignedchar>>MemoryReadBytestream::read(size_tsize){if(m_data.empty()){onDataNeeded();returnboost::shared_ptr<std::vector<unsignedchar>>(newstd::vector<unsignedchar>());}if(m_data.size()<size){boost::shared_ptr<std::vector<unsignedchar>>ptr(newstd::vector<unsignedchar>(m_data.begin(),m_data.end()));m_sent+=m_data.size();m_data.clear();if(m_sent==m_size)m_finished=true;onDataNeeded();returnptr;}boost::shared_ptr<std::vector<unsignedchar>>ptr(newstd::vector<unsignedchar>(m_data.begin(),m_data.begin()+size));m_data.erase(m_data.begin(),m_data.begin()+size);m_sent+=size;if(m_sent==m_size)m_finished=true;if(m_data.size()<500000&&!neededData){neededData=true;onDataNeeded();}returnptr;}boolMemoryReadBytestream::isFinished()const{// std::cout << "finished? " << m_finished << "\n";returnm_finished;}}