Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutputStream.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_OUTPUTSTREAM_H_INCLUDED
  21. #define WATER_OUTPUTSTREAM_H_INCLUDED
  22. #include "../water.h"
  23. #include "../text/String.h"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. The base class for streams that write data to some kind of destination.
  28. Input and output streams are used throughout the library - subclasses can override
  29. some or all of the virtual functions to implement their behaviour.
  30. @see InputStream, MemoryOutputStream, FileOutputStream
  31. */
  32. class OutputStream
  33. {
  34. protected:
  35. //==============================================================================
  36. OutputStream();
  37. public:
  38. /** Destructor.
  39. Some subclasses might want to do things like call flush() during their
  40. destructors.
  41. */
  42. virtual ~OutputStream();
  43. //==============================================================================
  44. /** If the stream is using a buffer, this will ensure it gets written
  45. out to the destination. */
  46. virtual void flush() = 0;
  47. /** Tries to move the stream's output position.
  48. Not all streams will be able to seek to a new position - this will return
  49. false if it fails to work.
  50. @see getPosition
  51. */
  52. virtual bool setPosition (int64 newPosition) = 0;
  53. /** Returns the stream's current position.
  54. @see setPosition
  55. */
  56. virtual int64 getPosition() = 0;
  57. //==============================================================================
  58. /** Writes a block of data to the stream.
  59. When creating a subclass of OutputStream, this is the only write method
  60. that needs to be overloaded - the base class has methods for writing other
  61. types of data which use this to do the work.
  62. @param dataToWrite the target buffer to receive the data. This must not be null.
  63. @param numberOfBytes the number of bytes to write.
  64. @returns false if the write operation fails for some reason
  65. */
  66. virtual bool write (const void* dataToWrite,
  67. size_t numberOfBytes) = 0;
  68. //==============================================================================
  69. /** Writes a single byte to the stream.
  70. @returns false if the write operation fails for some reason
  71. @see InputStream::readByte
  72. */
  73. virtual bool writeByte (char byte);
  74. /** Writes a boolean to the stream as a single byte.
  75. This is encoded as a binary byte (not as text) with a value of 1 or 0.
  76. @returns false if the write operation fails for some reason
  77. @see InputStream::readBool
  78. */
  79. virtual bool writeBool (bool boolValue);
  80. /** Writes a 16-bit integer to the stream in a little-endian byte order.
  81. This will write two bytes to the stream: (value & 0xff), then (value >> 8).
  82. @returns false if the write operation fails for some reason
  83. @see InputStream::readShort
  84. */
  85. virtual bool writeShort (short value);
  86. /** Writes a 16-bit integer to the stream in a big-endian byte order.
  87. This will write two bytes to the stream: (value >> 8), then (value & 0xff).
  88. @returns false if the write operation fails for some reason
  89. @see InputStream::readShortBigEndian
  90. */
  91. virtual bool writeShortBigEndian (short value);
  92. /** Writes a 32-bit integer to the stream in a little-endian byte order.
  93. @returns false if the write operation fails for some reason
  94. @see InputStream::readInt
  95. */
  96. virtual bool writeInt (int value);
  97. /** Writes a 32-bit integer to the stream in a big-endian byte order.
  98. @returns false if the write operation fails for some reason
  99. @see InputStream::readIntBigEndian
  100. */
  101. virtual bool writeIntBigEndian (int value);
  102. /** Writes a 64-bit integer to the stream in a little-endian byte order.
  103. @returns false if the write operation fails for some reason
  104. @see InputStream::readInt64
  105. */
  106. virtual bool writeInt64 (int64 value);
  107. /** Writes a 64-bit integer to the stream in a big-endian byte order.
  108. @returns false if the write operation fails for some reason
  109. @see InputStream::readInt64BigEndian
  110. */
  111. virtual bool writeInt64BigEndian (int64 value);
  112. /** Writes a 32-bit floating point value to the stream in a binary format.
  113. The binary 32-bit encoding of the float is written as a little-endian int.
  114. @returns false if the write operation fails for some reason
  115. @see InputStream::readFloat
  116. */
  117. virtual bool writeFloat (float value);
  118. /** Writes a 32-bit floating point value to the stream in a binary format.
  119. The binary 32-bit encoding of the float is written as a big-endian int.
  120. @returns false if the write operation fails for some reason
  121. @see InputStream::readFloatBigEndian
  122. */
  123. virtual bool writeFloatBigEndian (float value);
  124. /** Writes a 64-bit floating point value to the stream in a binary format.
  125. The eight raw bytes of the double value are written out as a little-endian 64-bit int.
  126. @returns false if the write operation fails for some reason
  127. @see InputStream::readDouble
  128. */
  129. virtual bool writeDouble (double value);
  130. /** Writes a 64-bit floating point value to the stream in a binary format.
  131. The eight raw bytes of the double value are written out as a big-endian 64-bit int.
  132. @see InputStream::readDoubleBigEndian
  133. @returns false if the write operation fails for some reason
  134. */
  135. virtual bool writeDoubleBigEndian (double value);
  136. /** Writes a byte to the output stream a given number of times.
  137. @returns false if the write operation fails for some reason
  138. */
  139. virtual bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat);
  140. /** Writes a condensed binary encoding of a 32-bit integer.
  141. If you're storing a lot of integers which are unlikely to have very large values,
  142. this can save a lot of space, because values under 0xff will only take up 2 bytes,
  143. under 0xffff only 3 bytes, etc.
  144. The format used is: number of significant bytes + up to 4 bytes in little-endian order.
  145. @returns false if the write operation fails for some reason
  146. @see InputStream::readCompressedInt
  147. */
  148. virtual bool writeCompressedInt (int value);
  149. /** Stores a string in the stream in a binary format.
  150. This isn't the method to use if you're trying to append text to the end of a
  151. text-file! It's intended for storing a string so that it can be retrieved later
  152. by InputStream::readString().
  153. It writes the string to the stream as UTF8, including the null termination character.
  154. For appending text to a file, instead use writeText, or operator<<
  155. @returns false if the write operation fails for some reason
  156. @see InputStream::readString, writeText, operator<<
  157. */
  158. virtual bool writeString (const String& text);
  159. /** Writes a string of text to the stream.
  160. It can either write the text as UTF-8 or UTF-16, and can also add the UTF-16 byte-order-mark
  161. bytes (0xff, 0xfe) to indicate the endianness (these should only be used at the start
  162. of a file).
  163. The method also replaces '\\n' characters in the text with '\\r\\n'.
  164. @returns false if the write operation fails for some reason
  165. */
  166. virtual bool writeText (const String& text,
  167. bool asUTF16,
  168. bool writeUTF16ByteOrderMark);
  169. /** Reads data from an input stream and writes it to this stream.
  170. @param source the stream to read from
  171. @param maxNumBytesToWrite the number of bytes to read from the stream (if this is
  172. less than zero, it will keep reading until the input
  173. is exhausted)
  174. @returns the number of bytes written
  175. */
  176. virtual int64 writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite);
  177. //==============================================================================
  178. /** Sets the string to write to the stream when a new line is written.
  179. By default this will be set the value of NewLine::getDefault().
  180. */
  181. void setNewLineString (const String& newLineString);
  182. /** Returns the current new-line string that was set by setNewLineString(). */
  183. const String& getNewLineString() const noexcept { return newLineString; }
  184. private:
  185. //==============================================================================
  186. String newLineString;
  187. CARLA_DECLARE_NON_COPYABLE (OutputStream)
  188. };
  189. //==============================================================================
  190. /** Writes a number to a stream as 8-bit characters in the default system encoding. */
  191. OutputStream& operator<< (OutputStream& stream, int number);
  192. /** Writes a number to a stream as 8-bit characters in the default system encoding. */
  193. OutputStream& operator<< (OutputStream& stream, int64 number);
  194. /** Writes a number to a stream as 8-bit characters in the default system encoding. */
  195. OutputStream& operator<< (OutputStream& stream, double number);
  196. /** Writes a character to a stream. */
  197. OutputStream& operator<< (OutputStream& stream, char character);
  198. /** Writes a null-terminated text string to a stream. */
  199. OutputStream& operator<< (OutputStream& stream, const char* text);
  200. /** Writes a block of data from a MemoryBlock to a stream. */
  201. OutputStream& operator<< (OutputStream& stream, const MemoryBlock& data);
  202. /** Writes the contents of a file to a stream. */
  203. OutputStream& operator<< (OutputStream& stream, const File& fileToRead);
  204. /** Writes the complete contents of an input stream to an output stream. */
  205. OutputStream& operator<< (OutputStream& stream, InputStream& streamToRead);
  206. /** Writes a new-line to a stream.
  207. You can use the predefined symbol 'newLine' to invoke this, e.g.
  208. @code
  209. myOutputStream << "Hello World" << newLine << newLine;
  210. @endcode
  211. @see OutputStream::setNewLineString
  212. */
  213. OutputStream& operator<< (OutputStream& stream, const NewLine&);
  214. }
  215. #endif // WATER_OUTPUTSTREAM_H_INCLUDED