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.

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