3 #include "../conversion/conversionexception.h" 23 BinaryWriter::BinaryWriter(ostream *stream)
34 : m_stream(other.m_stream)
80 byte prefixLength = 1;
81 for (; boundCheck != 0x8000000000000000; boundCheck <<= 7, ++prefixLength) {
82 if (value < boundCheck) {
83 getBytes(value | boundCheck, m_buffer);
87 if (prefixLength == 9) {
88 throw ConversionException(
"The variable-length integer to be written exceeds the maximum.");
90 m_stream->write(m_buffer + 8 - prefixLength, prefixLength);
104 m_stream->write(value.data(), static_cast<streamsize>(value.size()));
118 m_stream->write(value, static_cast<streamsize>(size));
BinaryWriter(std::ostream *stream)
Constructs a new BinaryWriter.
Writes primitive data types to a std::ostream.
The ConversionException class is thrown by the various conversion functions of this library when a co...
std::uint64_t uint64
unsigned 64-bit integer
void giveOwnership()
The writer will take ownership over the assigned stream.
void writeVariableLengthUIntBE(uint64 value)
Writes an up to 8 byte long big endian unsigned integer to the current stream and advances the curren...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
Contains utility classes helping to read and write streams.
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
Contains several functions providing conversions between different data types.
~BinaryWriter()
Destroys the BinaryWriter.
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
void writeLengthPrefixedCString(const char *value, std::size_t size)
Writes the length of a string and the string itself to the current stream.
CPP_UTILITIES_EXPORT void getBytes(int16 value, char *outputbuffer)
Stores the specified 16-bit signed integer value at a specified position in a char array.