diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-09-30 18:46:42 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-09-30 18:46:42 (GMT) |
commit | 1b255f6ba90847240b92126deb5ea0174ad269b0 (patch) | |
tree | 9d56e647ebc35c0f0941d19a302b888c1185ce1c | |
parent | af976a75b5e1b504a340602659ef930109cd2c48 (diff) | |
download | swift-1b255f6ba90847240b92126deb5ea0174ad269b0.zip swift-1b255f6ba90847240b92126deb5ea0174ad269b0.tar.bz2 |
Avoid a copy when reading from a byte array.
-rw-r--r-- | Swiften/FileTransfer/ByteArrayReadBytestream.h | 8 | ||||
-rw-r--r-- | Swiften/FileTransfer/FileReadBytestream.cpp | 14 | ||||
-rw-r--r-- | Swiften/FileTransfer/FileReadBytestream.h | 2 | ||||
-rw-r--r-- | Swiften/FileTransfer/IBBSendSession.cpp | 9 | ||||
-rw-r--r-- | Swiften/FileTransfer/ReadBytestream.h | 3 | ||||
-rw-r--r-- | Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp | 6 | ||||
-rw-r--r-- | Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp | 2 |
7 files changed, 25 insertions, 19 deletions
diff --git a/Swiften/FileTransfer/ByteArrayReadBytestream.h b/Swiften/FileTransfer/ByteArrayReadBytestream.h index 6cbdef0..9311099 100644 --- a/Swiften/FileTransfer/ByteArrayReadBytestream.h +++ b/Swiften/FileTransfer/ByteArrayReadBytestream.h @@ -7,9 +7,11 @@ #pragma once #include <vector> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Base/Algorithm.h> #include <Swiften/FileTransfer/ReadBytestream.h> +#include <Swiften/Base/ByteArray.h> namespace Swift { class ByteArrayReadBytestream : public ReadBytestream { @@ -17,14 +19,14 @@ namespace Swift { ByteArrayReadBytestream(const std::vector<unsigned char>& data) : data(data), position(0), dataComplete(true) { } - virtual std::vector<unsigned char> read(size_t size) { + virtual boost::shared_ptr<ByteArray> read(size_t size) { size_t readSize = size; if (position + readSize > data.size()) { readSize = data.size() - position; } - std::vector<unsigned char> result(data.begin() + position, data.begin() + position + readSize); + boost::shared_ptr<ByteArray> result = boost::make_shared<ByteArray>(data.begin() + position, data.begin() + position + readSize); - onRead(result); + onRead(*result); position += readSize; return result; } diff --git a/Swiften/FileTransfer/FileReadBytestream.cpp b/Swiften/FileTransfer/FileReadBytestream.cpp index f0139b8..a8946a0 100644 --- a/Swiften/FileTransfer/FileReadBytestream.cpp +++ b/Swiften/FileTransfer/FileReadBytestream.cpp @@ -6,8 +6,10 @@ #include <boost/filesystem/fstream.hpp> #include <cassert> +#include <boost/smart_ptr/make_shared.hpp> #include <Swiften/FileTransfer/FileReadBytestream.h> +#include <Swiften/Base/ByteArray.h> namespace Swift { @@ -21,16 +23,16 @@ FileReadBytestream::~FileReadBytestream() { } } -std::vector<unsigned char> FileReadBytestream::read(size_t size) { +boost::shared_ptr<ByteArray> FileReadBytestream::read(size_t size) { if (!stream) { stream = new boost::filesystem::ifstream(file, std::ios_base::in|std::ios_base::binary); } - std::vector<unsigned char> result; - result.resize(size); + boost::shared_ptr<ByteArray> result = boost::make_shared<ByteArray>(); + result->resize(size); assert(stream->good()); - stream->read(reinterpret_cast<char*>(&result[0]), size); - result.resize(stream->gcount()); - onRead(result); + stream->read(reinterpret_cast<char*>(vecptr(*result)), size); + result->resize(stream->gcount()); + onRead(*result); return result; } diff --git a/Swiften/FileTransfer/FileReadBytestream.h b/Swiften/FileTransfer/FileReadBytestream.h index bb24879..e9db2a4 100644 --- a/Swiften/FileTransfer/FileReadBytestream.h +++ b/Swiften/FileTransfer/FileReadBytestream.h @@ -17,7 +17,7 @@ namespace Swift { FileReadBytestream(const boost::filesystem::path& file); ~FileReadBytestream(); - virtual std::vector<unsigned char> read(size_t size); + virtual boost::shared_ptr< std::vector<unsigned char> > read(size_t size); virtual bool isFinished() const; private: diff --git a/Swiften/FileTransfer/IBBSendSession.cpp b/Swiften/FileTransfer/IBBSendSession.cpp index 3a1390c..c24cc0a 100644 --- a/Swiften/FileTransfer/IBBSendSession.cpp +++ b/Swiften/FileTransfer/IBBSendSession.cpp @@ -8,6 +8,7 @@ #include <boost/bind.hpp> +#include <Swiften/Base/ByteArray.h> #include <Swiften/Queries/IQRouter.h> #include <Swiften/FileTransfer/IBBRequest.h> #include <Swiften/FileTransfer/BytestreamException.h> @@ -52,14 +53,14 @@ void IBBSendSession::handleIBBResponse(IBB::ref, ErrorPayload::ref error) { void IBBSendSession::sendMoreData() { try { - std::vector<unsigned char> data = bytestream->read(blockSize); - if (!data.empty()) { + boost::shared_ptr<ByteArray> data = bytestream->read(blockSize); + if (!data->empty()) { waitingForData = false; - IBBRequest::ref request = IBBRequest::create(from, to, IBB::createIBBData(id, sequenceNumber, data), router); + IBBRequest::ref request = IBBRequest::create(from, to, IBB::createIBBData(id, sequenceNumber, *data), router); sequenceNumber++; request->onResponse.connect(boost::bind(&IBBSendSession::handleIBBResponse, this, _1, _2)); request->send(); - onBytesSent(data.size()); + onBytesSent(data->size()); } else { waitingForData = true; diff --git a/Swiften/FileTransfer/ReadBytestream.h b/Swiften/FileTransfer/ReadBytestream.h index 0e95f7b..c94e4d3 100644 --- a/Swiften/FileTransfer/ReadBytestream.h +++ b/Swiften/FileTransfer/ReadBytestream.h @@ -6,6 +6,7 @@ #pragma once +#include <boost/shared_ptr.hpp> #include <vector> #include <Swiften/Base/boost_bsignals.h> @@ -19,7 +20,7 @@ namespace Swift { * Return an empty vector if no more data is available. * Use onDataAvailable signal for signaling there is data available again. */ - virtual std::vector<unsigned char> read(size_t size) = 0; + virtual boost::shared_ptr< std::vector<unsigned char> > read(size_t size) = 0; virtual bool isFinished() const = 0; diff --git a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp index db3d83f..cd555e5 100644 --- a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp +++ b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp @@ -165,9 +165,9 @@ HostAddressPort SOCKS5BytestreamClientSession::getAddressPort() const { void SOCKS5BytestreamClientSession::sendData() { if (!readBytestream->isFinished()) { try { - SafeByteArray dataToSend = createSafeByteArray(readBytestream->read(chunkSize)); - connection->write(dataToSend); - onBytesSent(dataToSend.size()); + boost::shared_ptr<ByteArray> dataToSend = readBytestream->read(chunkSize); + connection->write(createSafeByteArray(*dataToSend)); + onBytesSent(dataToSend->size()); } catch (const BytestreamException&) { finish(true); diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp index def9e33..f660fda 100644 --- a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp +++ b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp @@ -145,7 +145,7 @@ void SOCKS5BytestreamServerSession::process() { void SOCKS5BytestreamServerSession::sendData() { if (!readBytestream->isFinished()) { try { - SafeByteArray dataToSend = createSafeByteArray(readBytestream->read(chunkSize)); + SafeByteArray dataToSend = createSafeByteArray(*readBytestream->read(chunkSize)); connection->write(dataToSend); onBytesSent(dataToSend.size()); } |