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.

257 lines
10.0KB

  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. @returns false if the write operation fails for some reason
  63. */
  64. virtual bool write (const void* dataToWrite,
  65. int howManyBytes) = 0;
  66. //==============================================================================
  67. /** Writes a single byte to the stream.
  68. @see InputStream::readByte
  69. */
  70. virtual void writeByte (char byte);
  71. /** Writes a boolean to the stream as a single byte.
  72. This is encoded as a binary byte (not as text) with a value of 1 or 0.
  73. @see InputStream::readBool
  74. */
  75. virtual void writeBool (bool boolValue);
  76. /** Writes a 16-bit integer to the stream in a little-endian byte order.
  77. This will write two bytes to the stream: (value & 0xff), then (value >> 8).
  78. @see InputStream::readShort
  79. */
  80. virtual void 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. @see InputStream::readShortBigEndian
  84. */
  85. virtual void writeShortBigEndian (short value);
  86. /** Writes a 32-bit integer to the stream in a little-endian byte order.
  87. @see InputStream::readInt
  88. */
  89. virtual void writeInt (int value);
  90. /** Writes a 32-bit integer to the stream in a big-endian byte order.
  91. @see InputStream::readIntBigEndian
  92. */
  93. virtual void writeIntBigEndian (int value);
  94. /** Writes a 64-bit integer to the stream in a little-endian byte order.
  95. @see InputStream::readInt64
  96. */
  97. virtual void writeInt64 (int64 value);
  98. /** Writes a 64-bit integer to the stream in a big-endian byte order.
  99. @see InputStream::readInt64BigEndian
  100. */
  101. virtual void writeInt64BigEndian (int64 value);
  102. /** Writes a 32-bit floating point value to the stream in a binary format.
  103. The binary 32-bit encoding of the float is written as a little-endian int.
  104. @see InputStream::readFloat
  105. */
  106. virtual void writeFloat (float 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 big-endian int.
  109. @see InputStream::readFloatBigEndian
  110. */
  111. virtual void writeFloatBigEndian (float value);
  112. /** Writes a 64-bit floating point value to the stream in a binary format.
  113. The eight raw bytes of the double value are written out as a little-endian 64-bit int.
  114. @see InputStream::readDouble
  115. */
  116. virtual void writeDouble (double 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 big-endian 64-bit int.
  119. @see InputStream::readDoubleBigEndian
  120. */
  121. virtual void writeDoubleBigEndian (double value);
  122. /** Writes a byte to the output stream a given number of times. */
  123. virtual void writeRepeatedByte (uint8 byte, int numTimesToRepeat);
  124. /** Writes a condensed binary encoding of a 32-bit integer.
  125. If you're storing a lot of integers which are unlikely to have very large values,
  126. this can save a lot of space, because values under 0xff will only take up 2 bytes,
  127. under 0xffff only 3 bytes, etc.
  128. The format used is: number of significant bytes + up to 4 bytes in little-endian order.
  129. @see InputStream::readCompressedInt
  130. */
  131. virtual void writeCompressedInt (int value);
  132. /** Stores a string in the stream in a binary format.
  133. This isn't the method to use if you're trying to append text to the end of a
  134. text-file! It's intended for storing a string so that it can be retrieved later
  135. by InputStream::readString().
  136. It writes the string to the stream as UTF8, including the null termination character.
  137. For appending text to a file, instead use writeText, or operator<<
  138. @see InputStream::readString, writeText, operator<<
  139. */
  140. virtual void writeString (const String& text);
  141. /** Writes a string of text to the stream.
  142. It can either write the text as UTF-8 or UTF-16, and can also add the UTF-16 byte-order-mark
  143. bytes (0xff, 0xfe) to indicate the endianness (these should only be used at the start
  144. of a file).
  145. The method also replaces '\\n' characters in the text with '\\r\\n'.
  146. */
  147. virtual void writeText (const String& text,
  148. bool asUTF16,
  149. bool writeUTF16ByteOrderMark);
  150. /** Reads data from an input stream and writes it to this stream.
  151. @param source the stream to read from
  152. @param maxNumBytesToWrite the number of bytes to read from the stream (if this is
  153. less than zero, it will keep reading until the input
  154. is exhausted)
  155. */
  156. virtual int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite);
  157. //==============================================================================
  158. /** Sets the string that will be written to the stream when the writeNewLine()
  159. method is called.
  160. By default this will be set the the value of NewLine::getDefault().
  161. */
  162. void setNewLineString (const String& newLineString);
  163. /** Returns the current new-line string that was set by setNewLineString(). */
  164. const String& getNewLineString() const noexcept { return newLineString; }
  165. private:
  166. //==============================================================================
  167. String newLineString;
  168. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OutputStream);
  169. };
  170. //==============================================================================
  171. /** Writes a number to a stream as 8-bit characters in the default system encoding. */
  172. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, int number);
  173. /** Writes a number to a stream as 8-bit characters in the default system encoding. */
  174. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, double number);
  175. /** Writes a character to a stream. */
  176. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, char character);
  177. /** Writes a null-terminated text string to a stream. */
  178. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const char* text);
  179. /** Writes a block of data from a MemoryBlock to a stream. */
  180. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const MemoryBlock& data);
  181. /** Writes the contents of a file to a stream. */
  182. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const File& fileToRead);
  183. /** Writes a new-line to a stream.
  184. You can use the predefined symbol 'newLine' to invoke this, e.g.
  185. @code
  186. myOutputStream << "Hello World" << newLine << newLine;
  187. @endcode
  188. @see OutputStream::setNewLineString
  189. */
  190. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const NewLine&);
  191. #endif // __JUCE_OUTPUTSTREAM_JUCEHEADER__