juce_OutputStream.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #if JUCE_DEBUG
  22. struct DanglingStreamChecker
  23. {
  24. DanglingStreamChecker() {}
  25. ~DanglingStreamChecker()
  26. {
  27. /*
  28. It's always a bad idea to leak any object, but if you're leaking output
  29. streams, then there's a good chance that you're failing to flush a file
  30. to disk properly, which could result in corrupted data and other similar
  31. nastiness..
  32. */
  33. jassert (activeStreams.size() == 0);
  34. }
  35. Array<void*, CriticalSection> activeStreams;
  36. };
  37. static DanglingStreamChecker danglingStreamChecker;
  38. #endif
  39. //==============================================================================
  40. OutputStream::OutputStream()
  41. : newLineString (NewLine::getDefault())
  42. {
  43. #if JUCE_DEBUG
  44. danglingStreamChecker.activeStreams.add (this);
  45. #endif
  46. }
  47. OutputStream::~OutputStream()
  48. {
  49. #if JUCE_DEBUG
  50. danglingStreamChecker.activeStreams.removeFirstMatchingValue (this);
  51. #endif
  52. }
  53. //==============================================================================
  54. bool OutputStream::writeBool (const bool b)
  55. {
  56. return writeByte (b ? (char) 1
  57. : (char) 0);
  58. }
  59. bool OutputStream::writeByte (char byte)
  60. {
  61. return write (&byte, 1);
  62. }
  63. bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat)
  64. {
  65. for (size_t i = 0; i < numTimesToRepeat; ++i)
  66. if (! writeByte ((char) byte))
  67. return false;
  68. return true;
  69. }
  70. bool OutputStream::writeShort (short value)
  71. {
  72. const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value);
  73. return write (&v, 2);
  74. }
  75. bool OutputStream::writeShortBigEndian (short value)
  76. {
  77. const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value);
  78. return write (&v, 2);
  79. }
  80. bool OutputStream::writeInt (int value)
  81. {
  82. const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value);
  83. return write (&v, 4);
  84. }
  85. bool OutputStream::writeIntBigEndian (int value)
  86. {
  87. const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value);
  88. return write (&v, 4);
  89. }
  90. bool OutputStream::writeCompressedInt (int value)
  91. {
  92. unsigned int un = (value < 0) ? (unsigned int) -value
  93. : (unsigned int) value;
  94. uint8 data[5];
  95. int num = 0;
  96. while (un > 0)
  97. {
  98. data[++num] = (uint8) un;
  99. un >>= 8;
  100. }
  101. data[0] = (uint8) num;
  102. if (value < 0)
  103. data[0] |= 0x80;
  104. return write (data, (size_t) num + 1);
  105. }
  106. bool OutputStream::writeInt64 (int64 value)
  107. {
  108. const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value);
  109. return write (&v, 8);
  110. }
  111. bool OutputStream::writeInt64BigEndian (int64 value)
  112. {
  113. const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value);
  114. return write (&v, 8);
  115. }
  116. bool OutputStream::writeFloat (float value)
  117. {
  118. union { int asInt; float asFloat; } n;
  119. n.asFloat = value;
  120. return writeInt (n.asInt);
  121. }
  122. bool OutputStream::writeFloatBigEndian (float value)
  123. {
  124. union { int asInt; float asFloat; } n;
  125. n.asFloat = value;
  126. return writeIntBigEndian (n.asInt);
  127. }
  128. bool OutputStream::writeDouble (double value)
  129. {
  130. union { int64 asInt; double asDouble; } n;
  131. n.asDouble = value;
  132. return writeInt64 (n.asInt);
  133. }
  134. bool OutputStream::writeDoubleBigEndian (double value)
  135. {
  136. union { int64 asInt; double asDouble; } n;
  137. n.asDouble = value;
  138. return writeInt64BigEndian (n.asInt);
  139. }
  140. bool OutputStream::writeString (const String& text)
  141. {
  142. #if (JUCE_STRING_UTF_TYPE == 8)
  143. return write (text.toRawUTF8(), text.getNumBytesAsUTF8() + 1);
  144. #else
  145. // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind
  146. // if lots of large, persistent strings were to be written to streams).
  147. const size_t numBytes = text.getNumBytesAsUTF8() + 1;
  148. HeapBlock<char> temp (numBytes);
  149. text.copyToUTF8 (temp, numBytes);
  150. return write (temp, numBytes);
  151. #endif
  152. }
  153. bool OutputStream::writeText (const String& text, const bool asUTF16,
  154. const bool writeUTF16ByteOrderMark)
  155. {
  156. if (asUTF16)
  157. {
  158. if (writeUTF16ByteOrderMark)
  159. write ("\x0ff\x0fe", 2);
  160. String::CharPointerType src (text.getCharPointer());
  161. bool lastCharWasReturn = false;
  162. for (;;)
  163. {
  164. const juce_wchar c = src.getAndAdvance();
  165. if (c == 0)
  166. break;
  167. if (c == '\n' && ! lastCharWasReturn)
  168. writeShort ((short) '\r');
  169. lastCharWasReturn = (c == L'\r');
  170. if (! writeShort ((short) c))
  171. return false;
  172. }
  173. }
  174. else
  175. {
  176. const char* src = text.toUTF8();
  177. const char* t = src;
  178. for (;;)
  179. {
  180. if (*t == '\n')
  181. {
  182. if (t > src)
  183. if (! write (src, (size_t) (t - src)))
  184. return false;
  185. if (! write ("\r\n", 2))
  186. return false;
  187. src = t + 1;
  188. }
  189. else if (*t == '\r')
  190. {
  191. if (t[1] == '\n')
  192. ++t;
  193. }
  194. else if (*t == 0)
  195. {
  196. if (t > src)
  197. if (! write (src, (size_t) (t - src)))
  198. return false;
  199. break;
  200. }
  201. ++t;
  202. }
  203. }
  204. return true;
  205. }
  206. int64 OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)
  207. {
  208. if (numBytesToWrite < 0)
  209. numBytesToWrite = std::numeric_limits<int64>::max();
  210. int64 numWritten = 0;
  211. while (numBytesToWrite > 0)
  212. {
  213. char buffer [8192];
  214. const int num = source.read (buffer, (int) jmin (numBytesToWrite, (int64) sizeof (buffer)));
  215. if (num <= 0)
  216. break;
  217. write (buffer, (size_t) num);
  218. numBytesToWrite -= num;
  219. numWritten += num;
  220. }
  221. return numWritten;
  222. }
  223. //==============================================================================
  224. void OutputStream::setNewLineString (const String& newLineString_)
  225. {
  226. newLineString = newLineString_;
  227. }
  228. //==============================================================================
  229. template <typename IntegerType>
  230. static void writeIntToStream (OutputStream& stream, IntegerType number)
  231. {
  232. char buffer [NumberToStringConverters::charsNeededForInt];
  233. char* end = buffer + numElementsInArray (buffer);
  234. const char* start = NumberToStringConverters::numberToString (end, number);
  235. stream.write (start, (size_t) (end - start - 1));
  236. }
  237. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int number)
  238. {
  239. writeIntToStream (stream, number);
  240. return stream;
  241. }
  242. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const int64 number)
  243. {
  244. writeIntToStream (stream, number);
  245. return stream;
  246. }
  247. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const double number)
  248. {
  249. return stream << String (number);
  250. }
  251. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char character)
  252. {
  253. stream.writeByte (character);
  254. return stream;
  255. }
  256. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* const text)
  257. {
  258. stream.write (text, strlen (text));
  259. return stream;
  260. }
  261. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryBlock& data)
  262. {
  263. if (data.getSize() > 0)
  264. stream.write (data.getData(), data.getSize());
  265. return stream;
  266. }
  267. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const File& fileToRead)
  268. {
  269. FileInputStream in (fileToRead);
  270. if (in.openedOk())
  271. return stream << in;
  272. return stream;
  273. }
  274. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
  275. {
  276. stream.writeFromInputStream (streamToRead, -1);
  277. return stream;
  278. }
  279. JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const NewLine&)
  280. {
  281. return stream << stream.getNewLineString();
  282. }