Coverage Report

Created: 2026-10-28 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/include/Poco/BinaryWriter.h
Line
Count
Source
1
//
2
// BinaryWriter.h
3
//
4
// Library: Foundation
5
// Package: Streams
6
// Module:  BinaryReaderWriter
7
//
8
// Definition of the BinaryWriter class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_BinaryWriter_INCLUDED
18
#define Foundation_BinaryWriter_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
#include "Poco/Buffer.h"
23
#include "Poco/MemoryStream.h"
24
#include <vector>
25
#include <ostream>
26
#include <memory>
27
28
29
namespace Poco {
30
31
32
class TextEncoding;
33
class TextConverter;
34
35
36
class Foundation_API BinaryWriter
37
  /// This class writes basic types (and std::vectors of these)
38
  /// in binary form into an output stream.
39
  /// It provides an inserter-based interface similar to ostream.
40
  /// The writer also supports automatic conversion from big-endian
41
  /// (network byte order) to little-endian and vice-versa.
42
  /// Use a BinaryReader to read from a stream created by a BinaryWriter.
43
  /// Be careful when exchanging data between systems with different
44
  /// data type sizes (e.g., 32-bit and 64-bit architectures), as the sizes
45
  /// of some of the basic types may be different. For example, writing a
46
  /// long integer on a 64-bit system and reading it on a 32-bit system
47
  /// may yield an incorrent result. Use fixed-size types (Int32, Int64, etc.)
48
  /// in such a case.
49
{
50
public:
51
  enum StreamByteOrder
52
  {
53
    NATIVE_BYTE_ORDER        = 1, /// the host's native byte-order
54
    BIG_ENDIAN_BYTE_ORDER    = 2, /// big-endian (network) byte-order
55
    NETWORK_BYTE_ORDER       = 2, /// big-endian (network) byte-order
56
    LITTLE_ENDIAN_BYTE_ORDER = 3  /// little-endian byte-order
57
  };
58
59
  static constexpr std::streamsize  DEFAULT_MAX_CSTR_LENGTH { 1024 };
60
61
  BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
62
    /// Creates the BinaryWriter.
63
64
  BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
65
    /// Creates the BinaryWriter using the given TextEncoding.
66
    ///
67
    /// Strings will be converted from the currently set global encoding
68
    /// (see Poco::TextEncoding::global()) to the specified encoding.
69
    /// A copy of the BinaryWriter writes to the same stream and
70
    /// shares the converter.
71
72
  ~BinaryWriter();
73
    /// Destroys the BinaryWriter.
74
75
  BinaryWriter& operator << (bool value);
76
  BinaryWriter& operator << (char value);
77
  BinaryWriter& operator << (unsigned char value);
78
  BinaryWriter& operator << (signed char value);
79
  BinaryWriter& operator << (short value);
80
  BinaryWriter& operator << (unsigned short value);
81
  BinaryWriter& operator << (int value);
82
  BinaryWriter& operator << (unsigned int value);
83
  BinaryWriter& operator << (long value);
84
  BinaryWriter& operator << (unsigned long value);
85
  BinaryWriter& operator << (float value);
86
  BinaryWriter& operator << (double value);
87
88
#if defined(POCO_HAVE_INT64)
89
  BinaryWriter& operator << (long long value);
90
  BinaryWriter& operator << (unsigned long long value);
91
#endif
92
93
  BinaryWriter& operator << (const std::string& value);
94
  BinaryWriter& operator << (const char* value);
95
96
  template <typename T>
97
  BinaryWriter& operator << (const std::vector<T>& value)
98
  {
99
    Poco::UInt32 size(static_cast<Poco::UInt32>(value.size()));
100
101
    *this << size;
102
    for (const auto& v: value)
103
    {
104
      *this << v;
105
    }
106
107
    return *this;
108
  }
109
110
  void write7BitEncoded(UInt32 value);
111
    /// Writes a 32-bit unsigned integer in a compressed format.
112
    /// The value is written out seven bits at a time, starting
113
    /// with the seven least-significant bits.
114
    /// The high bit of a byte indicates whether there are more bytes to be
115
    /// written after this one.
116
    /// If value will fit in seven bits, it takes only one byte of space.
117
    /// If value will not fit in seven bits, the high bit is set on the first byte and
118
    /// written out. value is then shifted by seven bits and the next byte is written.
119
    /// This process is repeated until the entire integer has been written.
120
121
#if defined(POCO_HAVE_INT64)
122
  void write7BitEncoded(UInt64 value);
123
    /// Writes a 64-bit unsigned integer in a compressed format.
124
    /// The value written out seven bits at a time, starting
125
    /// with the seven least-significant bits.
126
    /// The high bit of a byte indicates whether there are more bytes to be
127
    /// written after this one.
128
    /// If value will fit in seven bits, it takes only one byte of space.
129
    /// If value will not fit in seven bits, the high bit is set on the first byte and
130
    /// written out. value is then shifted by seven bits and the next byte is written.
131
    /// This process is repeated until the entire integer has been written.
132
#endif
133
134
  void writeRaw(const std::string& rawData);
135
    /// Writes the string as-is to the stream.
136
137
  void writeRaw(const char* buffer, std::streamsize length);
138
    /// Writes length raw bytes from the given buffer to the stream.
139
140
  void writeCString(const char* cString, std::streamsize maxLength = DEFAULT_MAX_CSTR_LENGTH);
141
    /// Writes zero-terminated C-string.
142
143
  void writeBOM();
144
    /// Writes a byte-order mark to the stream. A byte order mark is
145
    /// a 16-bit integer with a value of 0xFEFF, written in host byte-order.
146
    /// A BinaryReader uses the byte-order mark to determine the byte-order
147
    /// of the stream.
148
149
  void flush();
150
    /// Flushes the underlying stream.
151
152
  [[nodiscard]] bool good();
153
    /// Returns _ostr.good();
154
155
  [[nodiscard]] bool fail();
156
    /// Returns _ostr.fail();
157
158
  [[nodiscard]] bool bad();
159
    /// Returns _ostr.bad();
160
161
  [[nodiscard]] std::ostream& stream() const;
162
    /// Returns the underlying stream.
163
164
  [[nodiscard]] StreamByteOrder byteOrder() const;
165
    /// Returns the byte ordering used by the writer, which is
166
    /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER.
167
168
private:
169
  std::ostream&  _ostr;
170
  bool           _flipBytes;
171
  std::shared_ptr<TextConverter> _pTextConverter;
172
    /// Shared, so that a copy of the writer does not delete it a second time.
173
};
174
175
176
template <typename T>
177
class BasicMemoryBinaryWriter: public BinaryWriter
178
  /// A convenient wrapper for using Buffer and MemoryStream with BinarWriter.
179
{
180
public:
181
  BasicMemoryBinaryWriter(Buffer<T>& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
182
    BinaryWriter(_ostr, byteOrder),
183
    _data(data),
184
    _ostr(data.begin(), data.capacity())
185
  {
186
  }
187
188
  BasicMemoryBinaryWriter(Buffer<T>& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
189
    BinaryWriter(_ostr, encoding, byteOrder),
190
    _data(data),
191
    _ostr(data.begin(), data.capacity())
192
  {
193
  }
194
195
  ~BasicMemoryBinaryWriter()
196
  {
197
    try
198
    {
199
      flush();
200
    }
201
    catch (...)
202
    {
203
      poco_unexpected();
204
    }
205
  }
206
207
  [[nodiscard]] Buffer<T>& data()
208
  {
209
    return _data;
210
  }
211
212
  [[nodiscard]] const Buffer<T>& data() const
213
  {
214
    return _data;
215
  }
216
217
  [[nodiscard]] const MemoryOutputStream& stream() const
218
  {
219
    return _ostr;
220
  }
221
222
  [[nodiscard]] MemoryOutputStream& stream()
223
  {
224
    return _ostr;
225
  }
226
227
private:
228
  Buffer<T>& _data;
229
  MemoryOutputStream _ostr;
230
};
231
232
using MemoryBinaryWriter = BasicMemoryBinaryWriter<char>;
233
234
//
235
// inlines
236
//
237
238
239
inline std::ostream& BinaryWriter::stream() const
240
0
{
241
0
  return _ostr;
242
0
}
243
244
245
inline bool BinaryWriter::good()
246
0
{
247
0
  return _ostr.good();
248
0
}
249
250
251
inline bool BinaryWriter::fail()
252
0
{
253
0
  return _ostr.fail();
254
0
}
255
256
257
inline bool BinaryWriter::bad()
258
0
{
259
0
  return _ostr.bad();
260
0
}
261
262
263
inline BinaryWriter::StreamByteOrder BinaryWriter::byteOrder() const
264
0
{
265
0
#if defined(POCO_ARCH_BIG_ENDIAN)
266
0
  return _flipBytes ? LITTLE_ENDIAN_BYTE_ORDER : BIG_ENDIAN_BYTE_ORDER;
267
0
#else
268
0
  return _flipBytes ? BIG_ENDIAN_BYTE_ORDER : LITTLE_ENDIAN_BYTE_ORDER;
269
0
#endif
270
0
}
271
272
273
} // namespace Poco
274
275
276
#endif // Foundation_BinaryWriter_INCLUDED