3 #include "../conversion/conversionexception.h"
23 BinaryWriter::BinaryWriter(ostream *stream,
ByteOrder byteOrder) :
26 m_byteOrder(byteOrder)
35 m_stream(other.m_stream),
37 m_byteOrder(other.m_byteOrder)
45 if(m_stream && m_ownership) {
83 if(m_stream && m_ownership)
170 return m_stream->fail();
181 m_stream->write(buffer, length);
190 m_stream->write(buffer.data(), length);
198 char buff[1] = {value};
199 m_stream->write(buff, 1);
207 char buff[1] = {*
reinterpret_cast<char *
>(&value)};
208 m_stream->write(buff, 1);
216 char buff[
sizeof(
int16)];
217 getBytes(value, buff, 0, m_byteOrder);
218 m_stream->write(buff,
sizeof(
int16));
226 char buff[
sizeof(
uint16)];
227 getBytes(value, buff, 0, m_byteOrder);
228 m_stream->write(buff,
sizeof(
uint16));
239 getBytes(value, buff, 0, m_byteOrder);
240 switch(m_byteOrder) {
241 case ByteOrder::BigEndian:
242 m_stream->write(buff + 1, 3);
244 case ByteOrder::LittleEndian:
245 m_stream->write(buff, 3);
258 getBytes(value, buff, 0, m_byteOrder);
259 switch(m_byteOrder) {
260 case ByteOrder::BigEndian:
261 m_stream->write(buff + 1, 3);
263 case ByteOrder::LittleEndian:
264 m_stream->write(buff, 3);
274 char buff[
sizeof(
int32)];
275 getBytes(value, buff, 0, m_byteOrder);
276 m_stream->write(buff,
sizeof(
int32));
284 char buff[
sizeof(
uint32)];
285 getBytes(value, buff, 0, m_byteOrder);
286 m_stream->write(buff,
sizeof(
uint32));
294 char buff[
sizeof(
int64)];
295 getBytes(value, buff, 0, m_byteOrder);
296 m_stream->write(buff,
sizeof(
int64));
304 char buff[
sizeof(
uint64)];
305 getBytes(value, buff, 0, m_byteOrder);
306 m_stream->write(buff,
sizeof(
uint64));
314 char buff[
sizeof(float32)];
315 getBytes(value, buff, 0, m_byteOrder);
316 m_stream->write(buff,
sizeof(float32));
324 char buff[
sizeof(float64)];
325 getBytes(value, buff, 0, m_byteOrder);
326 m_stream->write(buff,
sizeof(float64));
334 m_stream->write(value.c_str(), value.length());
344 size_t length = value.length();
347 buff[0] = 0x80 | length;
348 m_stream->write(buff, 1);
349 }
else if(length < 0x4000) {
350 getBytes(static_cast<uint16>(0x4000 | length), buff, 0, ByteOrder::BigEndian);
351 m_stream->write(buff, 2);
352 }
else if(length < 0x200000) {
353 getBytes(static_cast<uint32>(0x200000 | length), buff, 0, ByteOrder::BigEndian);
354 m_stream->write(buff + 1, 3);
355 }
else if(length < 0x10000000) {
356 getBytes(static_cast<uint32>(0x10000000 | length), buff, 0, ByteOrder::BigEndian);
357 m_stream->write(buff, 4);
361 m_stream->write(value.c_str(), length);
void getBytes(int16 value, char *outputbuffer, int startIndex, ByteOrder byteOrder)
Stores the specified 16-bit signed integer value at a specified position in a char array...
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
ByteOrder
Specifies the byte order/endianness.
std::int64_t int64
signed 64-bit integer
Writes primitive data types to a std::ostream using a specified ConversionUtilities::ByteOrder.
The exception that is thrown when an conversion error occurs.
void writeUInt24(uint32 value)
Writes a 24-bit unsigned integer to the current stream and advances the current position of the strea...
void writeString(const std::string &value)
Writes a string to the current stream and advances the current position of the stream by the length o...
void flush()
Calls the flush() method of the assigned stream.
void writeUInt32(uint32 value)
Writes a 32-bit unsigned integer to the current stream and advances the current position of the strea...
void writeInt24(int32 value)
Writes a 24-bit signed integer to the current stream and advances the current position of the stream ...
std::uint64_t uint64
unsigned 64-bit integer
void writeInt32(int32 value)
Writes a 32-bit signed integer to the current stream and advances the current position of the stream ...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
void giveOwnership()
The writer will take ownership over the assigned stream.
void writeFloat32(float32 value)
Writes a 32-bit floating point value to the current stream and advances the current position of the s...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
void writeFloat64(float64 value)
Writes a 64-bit floating point value to the current stream and advances the current position of the s...
Contains utility classes helping to read and write streams.
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
void writeInt16(int16 value)
Writes a 16-bit signed integer to the current stream and advances the current position of the stream ...
void writeSynchsafeUInt32(uint32 valueToConvertAndWrite)
Writes 32-bit synchsafe integer to the current stream and advances the current position of the stream...
void write(const char *buffer, std::streamsize length)
Contains several functions providing conversions between different data types.
std::uint32_t uint32
unsigned 32-bit integer
BinaryWriter(std::ostream *stream, ConversionUtilities::ByteOrder byteOrder=ConversionUtilities::ByteOrder::LittleEndian)
Constructs a new BinaryWriter.
uint32 toSynchsafeInt(uint32 normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
void writeUInt64(uint64 value)
Writes a 64-bit unsigned integer to the current stream and advances the current position of the strea...
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
~BinaryWriter()
Destroys the BinaryWriter.
std::int32_t int32
signed 32-bit integer
void setByteOrder(ConversionUtilities::ByteOrder value)
Sets the byte order used when converting the provided values to the raw bytes written to the stream...
void writeInt64(int64 value)
Writes a 64-bit signed integer to the current stream and advances the current position of the stream ...
void detatchOwnership()
The writer will not take ownership over the assigned stream.
std::uint8_t byte
unsigned byte
void writeByte(byte value)
Writes a single byte to the current stream and advances the current position of the stream by one byt...
std::int16_t int16
signed 16-bit integer
void writeChar(char value)
Writes a single character to the current stream and advances the current position of the stream by on...
ConversionUtilities::ByteOrder byteOrder() const
Returns the byte order used when converting the provided values to the raw bytes written to the strea...
void writeUInt16(uint16 value)
Writes a 16-bit unsigned integer to the current stream and advances the current position of the strea...
std::uint16_t uint16
unsigned 16-bit integer