sources/network/bytestream.cpp

changeset 180
2e7225dbd9b2
parent 173
248fba6d1e76
child 182
20ca0a6be175
equal deleted inserted replaced
179:7fc34735178e 180:2e7225dbd9b2
64 } 64 }
65 65
66 /*! 66 /*!
67 * \returns an iterator to the current data position. 67 * \returns an iterator to the current data position.
68 */ 68 */
69 ByteArray::Iterator Bytestream::getCurrentIterator() 69 ByteArray::iterator Bytestream::getCurrentIterator()
70 { 70 {
71 return m_data.begin() + m_position; 71 return m_data.begin() + m_position;
72 } 72 }
73 73
74 /*! 74 /*!
158 * \brief Writes an integer to the end of the data as one byte. 158 * \brief Writes an integer to the end of the data as one byte.
159 * \param value Value to write 159 * \param value Value to write
160 */ 160 */
161 void Bytestream::writeByte(int8_t value) 161 void Bytestream::writeByte(int8_t value)
162 { 162 {
163 m_data.append(value); 163 m_data.push_back(value);
164 } 164 }
165 165
166 /*! 166 /*!
167 * \brief Writes an integer to the end of the data as 2 bytes. 167 * \brief Writes an integer to the end of the data as 2 bytes.
168 * \param value Value to write 168 * \param value Value to write
169 */ 169 */
170 void Bytestream::writeShort(int16_t value) 170 void Bytestream::writeShort(int16_t value)
171 { 171 {
172 m_data.reserve(m_data.size() + 2);
172 for (int i : range(2)) 173 for (int i : range(2))
173 m_data.append((value >> (i * 8)) & 0xFF); 174 m_data.push_back((value >> (i * 8)) & 0xFF);
174 } 175 }
175 176
176 /*! 177 /*!
177 * \brief Writes an integer to the end of the data as 4 bytes. 178 * \brief Writes an integer to the end of the data as 4 bytes.
178 * \param value Value to write 179 * \param value Value to write
179 */ 180 */
180 void Bytestream::writeLong(int32_t value) 181 void Bytestream::writeLong(int32_t value)
181 { 182 {
183 m_data.reserve(m_data.size() + 4);
182 for (int i : range(4)) 184 for (int i : range(4))
183 m_data.append((value >> (i * 8)) & 0xFF); 185 m_data.push_back((value >> (i * 8)) & 0xFF);
184 } 186 }
185 187
186 /*! 188 /*!
187 * \brief Writes a floating-point number to the end of the data. 189 * \brief Writes a floating-point number to the end of the data.
188 * \param value Value to write. 190 * \param value Value to write.
199 * \brief Writes the given string to the end of the data. 201 * \brief Writes the given string to the end of the data.
200 * \param text String to write. 202 * \param text String to write.
201 */ 203 */
202 void Bytestream::writeString(const String& string) 204 void Bytestream::writeString(const String& string)
203 { 205 {
204 m_data.append(string.toBytes(), string.length()); 206 const int oldSize = m_data.size();
205 m_data.append(0); 207 m_data.reserve(m_data.size() + string.length() + 1);
208 m_data.resize(m_data.size() + string.length());
209 std::copy(string.begin(), string.end(), m_data.begin() + oldSize);
210 m_data.push_back(0);
206 } 211 }
207 212
208 /*! 213 /*!
209 * \returns the current position the stream cursor in the data. 214 * \returns the current position the stream cursor in the data.
210 */ 215 */

mercurial