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.

265 lines
11KB

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