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.

270 lines
11KB

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