The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

279 lines
12KB

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