BinaryWriter.h 7.0 KB

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