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.

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