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.

275 lines
11KB

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