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.

299 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_INPUTSTREAM_JUCEHEADER__
  22. #define __JUCE_INPUTSTREAM_JUCEHEADER__
  23. #include "../text/juce_String.h"
  24. class MemoryBlock;
  25. //==============================================================================
  26. /** The base class for streams that read data.
  27. Input and output streams are used throughout the library - subclasses can override
  28. some or all of the virtual functions to implement their behaviour.
  29. @see OutputStream, MemoryInputStream, BufferedInputStream, FileInputStream
  30. */
  31. class JUCE_API InputStream
  32. {
  33. public:
  34. /** Destructor. */
  35. virtual ~InputStream() {}
  36. //==============================================================================
  37. /** Returns the total number of bytes available for reading in this stream.
  38. Note that this is the number of bytes available from the start of the
  39. stream, not from the current position.
  40. If the size of the stream isn't actually known, this will return -1.
  41. @see getNumBytesRemaining
  42. */
  43. virtual int64 getTotalLength() = 0;
  44. /** Returns the number of bytes available for reading, or a negative value if
  45. the remaining length is not known.
  46. @see getTotalLength
  47. */
  48. int64 getNumBytesRemaining();
  49. /** Returns true if the stream has no more data to read. */
  50. virtual bool isExhausted() = 0;
  51. //==============================================================================
  52. /** Reads some data from the stream into a memory buffer.
  53. This is the only read method that subclasses actually need to implement, as the
  54. InputStream base class implements the other read methods in terms of this one (although
  55. it's often more efficient for subclasses to implement them directly).
  56. @param destBuffer the destination buffer for the data. This must not be null.
  57. @param maxBytesToRead the maximum number of bytes to read - make sure the
  58. memory block passed in is big enough to contain this
  59. many bytes. This value must not be negative.
  60. @returns the actual number of bytes that were read, which may be less than
  61. maxBytesToRead if the stream is exhausted before it gets that far
  62. */
  63. virtual int read (void* destBuffer, int maxBytesToRead) = 0;
  64. /** Reads a byte from the stream.
  65. If the stream is exhausted, this will return zero.
  66. @see OutputStream::writeByte
  67. */
  68. virtual char readByte();
  69. /** Reads a boolean from the stream.
  70. The bool is encoded as a single byte - 1 for true, 0 for false.
  71. If the stream is exhausted, this will return false.
  72. @see OutputStream::writeBool
  73. */
  74. virtual bool readBool();
  75. /** Reads two bytes from the stream as a little-endian 16-bit value.
  76. If the next two bytes read are byte1 and byte2, this returns
  77. (byte1 | (byte2 << 8)).
  78. If the stream is exhausted partway through reading the bytes, this will return zero.
  79. @see OutputStream::writeShort, readShortBigEndian
  80. */
  81. virtual short readShort();
  82. /** Reads two bytes from the stream as a little-endian 16-bit value.
  83. If the next two bytes read are byte1 and byte2, this returns
  84. (byte2 | (byte1 << 8)).
  85. If the stream is exhausted partway through reading the bytes, this will return zero.
  86. @see OutputStream::writeShortBigEndian, readShort
  87. */
  88. virtual short readShortBigEndian();
  89. /** Reads four bytes from the stream as a little-endian 32-bit value.
  90. If the next four bytes are byte1 to byte4, this returns
  91. (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24)).
  92. If the stream is exhausted partway through reading the bytes, this will return zero.
  93. @see OutputStream::writeInt, readIntBigEndian
  94. */
  95. virtual int readInt();
  96. /** Reads four bytes from the stream as a big-endian 32-bit value.
  97. If the next four bytes are byte1 to byte4, this returns
  98. (byte4 | (byte3 << 8) | (byte2 << 16) | (byte1 << 24)).
  99. If the stream is exhausted partway through reading the bytes, this will return zero.
  100. @see OutputStream::writeIntBigEndian, readInt
  101. */
  102. virtual int readIntBigEndian();
  103. /** Reads eight bytes from the stream as a little-endian 64-bit value.
  104. If the next eight bytes are byte1 to byte8, this returns
  105. (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24) | (byte5 << 32) | (byte6 << 40) | (byte7 << 48) | (byte8 << 56)).
  106. If the stream is exhausted partway through reading the bytes, this will return zero.
  107. @see OutputStream::writeInt64, readInt64BigEndian
  108. */
  109. virtual int64 readInt64();
  110. /** Reads eight bytes from the stream as a big-endian 64-bit value.
  111. If the next eight bytes are byte1 to byte8, this returns
  112. (byte8 | (byte7 << 8) | (byte6 << 16) | (byte5 << 24) | (byte4 << 32) | (byte3 << 40) | (byte2 << 48) | (byte1 << 56)).
  113. If the stream is exhausted partway through reading the bytes, this will return zero.
  114. @see OutputStream::writeInt64BigEndian, readInt64
  115. */
  116. virtual int64 readInt64BigEndian();
  117. /** Reads four bytes as a 32-bit floating point value.
  118. The raw 32-bit encoding of the float is read from the stream as a little-endian int.
  119. If the stream is exhausted partway through reading the bytes, this will return zero.
  120. @see OutputStream::writeFloat, readDouble
  121. */
  122. virtual float readFloat();
  123. /** Reads four bytes as a 32-bit floating point value.
  124. The raw 32-bit encoding of the float is read from the stream as a big-endian int.
  125. If the stream is exhausted partway through reading the bytes, this will return zero.
  126. @see OutputStream::writeFloatBigEndian, readDoubleBigEndian
  127. */
  128. virtual float readFloatBigEndian();
  129. /** Reads eight bytes as a 64-bit floating point value.
  130. The raw 64-bit encoding of the double is read from the stream as a little-endian int64.
  131. If the stream is exhausted partway through reading the bytes, this will return zero.
  132. @see OutputStream::writeDouble, readFloat
  133. */
  134. virtual double readDouble();
  135. /** Reads eight bytes as a 64-bit floating point value.
  136. The raw 64-bit encoding of the double is read from the stream as a big-endian int64.
  137. If the stream is exhausted partway through reading the bytes, this will return zero.
  138. @see OutputStream::writeDoubleBigEndian, readFloatBigEndian
  139. */
  140. virtual double readDoubleBigEndian();
  141. /** Reads an encoded 32-bit number from the stream using a space-saving compressed format.
  142. For small values, this is more space-efficient than using readInt() and OutputStream::writeInt()
  143. The format used is: number of significant bytes + up to 4 bytes in little-endian order.
  144. @see OutputStream::writeCompressedInt()
  145. */
  146. virtual int readCompressedInt();
  147. //==============================================================================
  148. /** Reads a UTF-8 string from the stream, up to the next linefeed or carriage return.
  149. This will read up to the next "\n" or "\r\n" or end-of-stream.
  150. After this call, the stream's position will be left pointing to the next character
  151. following the line-feed, but the linefeeds aren't included in the string that
  152. is returned.
  153. */
  154. virtual String readNextLine();
  155. /** Reads a zero-terminated UTF-8 string from the stream.
  156. This will read characters from the stream until it hits a null character
  157. or end-of-stream.
  158. @see OutputStream::writeString, readEntireStreamAsString
  159. */
  160. virtual String readString();
  161. /** Tries to read the whole stream and turn it into a string.
  162. This will read from the stream's current position until the end-of-stream.
  163. It can read from either UTF-16 or UTF-8 formats.
  164. */
  165. virtual String readEntireStreamAsString();
  166. /** Reads from the stream and appends the data to a MemoryBlock.
  167. @param destBlock the block to append the data onto
  168. @param maxNumBytesToRead if this is a positive value, it sets a limit to the number
  169. of bytes that will be read - if it's negative, data
  170. will be read until the stream is exhausted.
  171. @returns the number of bytes that were added to the memory block
  172. */
  173. virtual int readIntoMemoryBlock (MemoryBlock& destBlock,
  174. ssize_t maxNumBytesToRead = -1);
  175. //==============================================================================
  176. /** Returns the offset of the next byte that will be read from the stream.
  177. @see setPosition
  178. */
  179. virtual int64 getPosition() = 0;
  180. /** Tries to move the current read position of the stream.
  181. The position is an absolute number of bytes from the stream's start.
  182. Some streams might not be able to do this, in which case they should do
  183. nothing and return false. Others might be able to manage it by resetting
  184. themselves and skipping to the correct position, although this is
  185. obviously a bit slow.
  186. @returns true if the stream manages to reposition itself correctly
  187. @see getPosition
  188. */
  189. virtual bool setPosition (int64 newPosition) = 0;
  190. /** Reads and discards a number of bytes from the stream.
  191. Some input streams might implement this efficiently, but the base
  192. class will just keep reading data until the requisite number of bytes
  193. have been done.
  194. */
  195. virtual void skipNextBytes (int64 numBytesToSkip);
  196. protected:
  197. //==============================================================================
  198. InputStream() noexcept {}
  199. private:
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InputStream)
  201. };
  202. #endif // __JUCE_INPUTSTREAM_JUCEHEADER__