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

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