juce_InputStream.h 11 KB

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