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.

260 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_INPUTSTREAM_H_INCLUDED
  21. #define WATER_INPUTSTREAM_H_INCLUDED
  22. #include "../water.h"
  23. namespace water {
  24. //==============================================================================
  25. /** The base class for streams that read data.
  26. Input and output streams are used throughout the library - subclasses can override
  27. some or all of the virtual functions to implement their behaviour.
  28. @see OutputStream, MemoryInputStream, BufferedInputStream, FileInputStream
  29. */
  30. class InputStream
  31. {
  32. public:
  33. /** Destructor. */
  34. virtual ~InputStream() {}
  35. //==============================================================================
  36. /** Returns the total number of bytes available for reading in this stream.
  37. Note that this is the number of bytes available from the start of the
  38. stream, not from the current position.
  39. If the size of the stream isn't actually known, this will return -1.
  40. @see getNumBytesRemaining
  41. */
  42. virtual int64 getTotalLength() = 0;
  43. /** Returns the number of bytes available for reading, or a negative value if
  44. the remaining length is not known.
  45. @see getTotalLength
  46. */
  47. int64 getNumBytesRemaining();
  48. /** Returns true if the stream has no more data to read. */
  49. virtual bool isExhausted() = 0;
  50. //==============================================================================
  51. /** Reads some data from the stream into a memory buffer.
  52. This is the only read method that subclasses actually need to implement, as the
  53. InputStream base class implements the other read methods in terms of this one (although
  54. it's often more efficient for subclasses to implement them directly).
  55. @param destBuffer the destination buffer for the data. This must not be null.
  56. @param maxBytesToRead the maximum number of bytes to read - make sure the
  57. memory block passed in is big enough to contain this
  58. many bytes. This value must not be negative.
  59. @returns the actual number of bytes that were read, which may be less than
  60. maxBytesToRead if the stream is exhausted before it gets that far
  61. */
  62. virtual int read (void* destBuffer, int maxBytesToRead) = 0;
  63. /** Reads a byte from the stream.
  64. If the stream is exhausted, this will return zero.
  65. @see OutputStream::writeByte
  66. */
  67. virtual char readByte();
  68. /** Reads a boolean from the stream.
  69. The bool is encoded as a single byte - non-zero for true, 0 for false.
  70. If the stream is exhausted, this will return false.
  71. @see OutputStream::writeBool
  72. */
  73. virtual bool readBool();
  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 (byte1 | (byte2 << 8)).
  76. If the stream is exhausted partway through reading the bytes, this will return zero.
  77. @see OutputStream::writeShort, readShortBigEndian
  78. */
  79. virtual short readShort();
  80. /** Reads two bytes from the stream as a little-endian 16-bit value.
  81. If the next two bytes read are byte1 and byte2, this returns (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 UTF-8 data, or UTF-16 if it detects suitable header-bytes.
  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 size_t 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. protected:
  188. //==============================================================================
  189. InputStream() noexcept {}
  190. private:
  191. CARLA_DECLARE_NON_COPY_CLASS (InputStream)
  192. };
  193. }
  194. #endif // WATER_INPUTSTREAM_H_INCLUDED