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.

262 lines
10KB

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