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.

289 lines
11KB

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