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