diff --git a/src/memoryreadbytestream.cpp b/src/memoryreadbytestream.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c3f0588e3bf426e2aba37bd0b8883ee4b7cad2a --- /dev/null +++ b/src/memoryreadbytestream.cpp @@ -0,0 +1,71 @@ +/** + * libtransport -- C++ library for easy XMPP Transports development + * + * Copyright (C) 2011, Jan Kaluza + * + * 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 "log4cxx/logger.h" +#include "memoryusage.h" +#include + +using namespace log4cxx; + +namespace Transport { + +MemoryReadBytestream::MemoryReadBytestream() { + neededData = false; + m_finished = false; +} + +MemoryReadBytestream::~MemoryReadBytestream() { + +} + +unsigned long MemoryReadBytestream::appendData(const std::string &data) { + m_data += data; + onDataAvailable(); + neededData = false; + return m_data.size(); +} + +boost::shared_ptr > MemoryReadBytestream::read(size_t size) { + if (m_data.empty()) { + onDataNeeded(); + return boost::shared_ptr >(new std::vector()); + } + + if (m_data.size() < size) { + boost::shared_ptr > ptr(new std::vector(m_data.begin(), m_data.end())); + m_data.clear(); + onDataNeeded(); + return ptr; + } + boost::shared_ptr > ptr(new std::vector(m_data.begin(), m_data.begin() + size)); + m_data.erase(m_data.begin(), m_data.begin() + size); + if (m_data.size() < 500000 && !neededData) { + neededData = true; + onDataNeeded(); + } + return ptr; +} + +bool MemoryReadBytestream::isFinished() const { + return m_finished; +} + +}