Audio plugin host https://kx.studio/carla
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.

269 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_INPUTSTREAM_H_INCLUDED
  24. #define JUCE_INPUTSTREAM_H_INCLUDED
  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 - non-zero 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 (byte1 | (byte2 << 8)).
  77. If the stream is exhausted partway through reading the bytes, this will return zero.
  78. @see OutputStream::writeShort, readShortBigEndian
  79. */
  80. virtual short readShort();
  81. /** Reads two bytes from the stream as a little-endian 16-bit value.
  82. If the next two bytes read are byte1 and byte2, this returns (byte2 | (byte1 << 8)).
  83. If the stream is exhausted partway through reading the bytes, this will return zero.
  84. @see OutputStream::writeShortBigEndian, readShort
  85. */
  86. virtual short readShortBigEndian();
  87. /** Reads four bytes from the stream as a little-endian 32-bit value.
  88. If the next four bytes are byte1 to byte4, this returns
  89. (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24)).
  90. If the stream is exhausted partway through reading the bytes, this will return zero.
  91. @see OutputStream::writeInt, readIntBigEndian
  92. */
  93. virtual int readInt();
  94. /** Reads four bytes from the stream as a big-endian 32-bit value.
  95. If the next four bytes are byte1 to byte4, this returns
  96. (byte4 | (byte3 << 8) | (byte2 << 16) | (byte1 << 24)).
  97. If the stream is exhausted partway through reading the bytes, this will return zero.
  98. @see OutputStream::writeIntBigEndian, readInt
  99. */
  100. virtual int readIntBigEndian();
  101. /** Reads eight bytes from the stream as a little-endian 64-bit value.
  102. If the next eight bytes are byte1 to byte8, this returns
  103. (byte1 | (byte2 << 8) | (byte3 << 16) | (byte4 << 24) | (byte5 << 32) | (byte6 << 40) | (byte7 << 48) | (byte8 << 56)).
  104. If the stream is exhausted partway through reading the bytes, this will return zero.
  105. @see OutputStream::writeInt64, readInt64BigEndian
  106. */
  107. virtual int64 readInt64();
  108. /** Reads eight bytes from the stream as a big-endian 64-bit value.
  109. If the next eight bytes are byte1 to byte8, this returns
  110. (byte8 | (byte7 << 8) | (byte6 << 16) | (byte5 << 24) | (byte4 << 32) | (byte3 << 40) | (byte2 << 48) | (byte1 << 56)).
  111. If the stream is exhausted partway through reading the bytes, this will return zero.
  112. @see OutputStream::writeInt64BigEndian, readInt64
  113. */
  114. virtual int64 readInt64BigEndian();
  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 little-endian int.
  117. If the stream is exhausted partway through reading the bytes, this will return zero.
  118. @see OutputStream::writeFloat, readDouble
  119. */
  120. virtual float readFloat();
  121. /** Reads four bytes as a 32-bit floating point value.
  122. The raw 32-bit encoding of the float is read from the stream as a big-endian int.
  123. If the stream is exhausted partway through reading the bytes, this will return zero.
  124. @see OutputStream::writeFloatBigEndian, readDoubleBigEndian
  125. */
  126. virtual float readFloatBigEndian();
  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 little-endian int64.
  129. If the stream is exhausted partway through reading the bytes, this will return zero.
  130. @see OutputStream::writeDouble, readFloat
  131. */
  132. virtual double readDouble();
  133. /** Reads eight bytes as a 64-bit floating point value.
  134. The raw 64-bit encoding of the double is read from the stream as a big-endian int64.
  135. If the stream is exhausted partway through reading the bytes, this will return zero.
  136. @see OutputStream::writeDoubleBigEndian, readFloatBigEndian
  137. */
  138. virtual double readDoubleBigEndian();
  139. /** Reads an encoded 32-bit number from the stream using a space-saving compressed format.
  140. For small values, this is more space-efficient than using readInt() and OutputStream::writeInt()
  141. The format used is: number of significant bytes + up to 4 bytes in little-endian order.
  142. @see OutputStream::writeCompressedInt()
  143. */
  144. virtual int readCompressedInt();
  145. //==============================================================================
  146. /** Reads a UTF-8 string from the stream, up to the next linefeed or carriage return.
  147. This will read up to the next "\n" or "\r\n" or end-of-stream.
  148. After this call, the stream's position will be left pointing to the next character
  149. following the line-feed, but the linefeeds aren't included in the string that
  150. is returned.
  151. */
  152. virtual String readNextLine();
  153. /** Reads a zero-terminated UTF-8 string from the stream.
  154. This will read characters from the stream until it hits a null character
  155. or end-of-stream.
  156. @see OutputStream::writeString, readEntireStreamAsString
  157. */
  158. virtual String readString();
  159. /** Tries to read the whole stream and turn it into a string.
  160. This will read from the stream's current position until the end-of-stream.
  161. It can read from UTF-8 data, or UTF-16 if it detects suitable header-bytes.
  162. */
  163. virtual String readEntireStreamAsString();
  164. /** Reads from the stream and appends the data to a MemoryBlock.
  165. @param destBlock the block to append the data onto
  166. @param maxNumBytesToRead if this is a positive value, it sets a limit to the number
  167. of bytes that will be read - if it's negative, data
  168. will be read until the stream is exhausted.
  169. @returns the number of bytes that were added to the memory block
  170. */
  171. virtual size_t readIntoMemoryBlock (MemoryBlock& destBlock,
  172. ssize_t maxNumBytesToRead = -1);
  173. //==============================================================================
  174. /** Returns the offset of the next byte that will be read from the stream.
  175. @see setPosition
  176. */
  177. virtual int64 getPosition() = 0;
  178. /** Tries to move the current read position of the stream.
  179. The position is an absolute number of bytes from the stream's start.
  180. Some streams might not be able to do this, in which case they should do
  181. nothing and return false. Others might be able to manage it by resetting
  182. themselves and skipping to the correct position, although this is
  183. obviously a bit slow.
  184. @returns true if the stream manages to reposition itself correctly
  185. @see getPosition
  186. */
  187. virtual bool setPosition (int64 newPosition) = 0;
  188. /** Reads and discards a number of bytes from the stream.
  189. Some input streams might implement this efficiently, but the base
  190. class will just keep reading data until the requisite number of bytes
  191. have been done.
  192. */
  193. virtual void skipNextBytes (int64 numBytesToSkip);
  194. protected:
  195. //==============================================================================
  196. InputStream() noexcept {}
  197. private:
  198. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InputStream)
  199. };
  200. #endif // JUCE_INPUTSTREAM_H_INCLUDED