From 1b255f6ba90847240b92126deb5ea0174ad269b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 30 Sep 2011 20:46:42 +0200
Subject: Avoid a copy when reading from a byte array.


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());
 		}
-- 
cgit v0.10.2-6-g49f6