Initial import to Git
[profile/ivi/common-api-dbus-runtime.git] / CommonAPI-DBus / src / CommonAPI / DBus / DBusOutputStream.cpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include "DBusOutputStream.h"
5
6 namespace CommonAPI {
7 namespace DBus {
8
9 DBusOutputStream::DBusOutputStream(DBusMessage dbusMessage) :
10                 dbusMessage_(dbusMessage) {
11 }
12
13 DBusOutputStream::~DBusOutputStream() {
14 }
15
16 void DBusOutputStream::flush() {
17     const int toWrite = payload_.size();
18     const bool success = dbusMessage_.setBodyLength(toWrite);
19     char* destinationDataPtr = dbusMessage_.getBodyData();
20
21     memcpy(destinationDataPtr, payload_.c_str(), toWrite);
22 }
23
24 void DBusOutputStream::reserveMemory(size_t numOfBytes) {
25     assert(numOfBytes >= 0);
26
27     payload_.reserve(numOfBytes);
28 }
29
30 DBusOutputStream& DBusOutputStream::writeString(const char* cString, const uint32_t& length) {
31     assert(cString != NULL);
32     assert(cString[length] == '\0');
33
34     *this << length;
35
36     writeRawData(cString, length + 1);
37
38     return *this;
39 }
40
41 } // namespace DBus
42 } // namespace CommonAPI