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.

239 lines
6.0KB

  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. int64 InputStream::getNumBytesRemaining()
  24. {
  25. int64 len = getTotalLength();
  26. if (len >= 0)
  27. len -= getPosition();
  28. return len;
  29. }
  30. char InputStream::readByte()
  31. {
  32. char temp = 0;
  33. read (&temp, 1);
  34. return temp;
  35. }
  36. bool InputStream::readBool()
  37. {
  38. return readByte() != 0;
  39. }
  40. short InputStream::readShort()
  41. {
  42. char temp[2];
  43. if (read (temp, 2) == 2)
  44. return (short) ByteOrder::littleEndianShort (temp);
  45. return 0;
  46. }
  47. short InputStream::readShortBigEndian()
  48. {
  49. char temp[2];
  50. if (read (temp, 2) == 2)
  51. return (short) ByteOrder::bigEndianShort (temp);
  52. return 0;
  53. }
  54. int InputStream::readInt()
  55. {
  56. char temp[4];
  57. if (read (temp, 4) == 4)
  58. return (int) ByteOrder::littleEndianInt (temp);
  59. return 0;
  60. }
  61. int InputStream::readIntBigEndian()
  62. {
  63. char temp[4];
  64. if (read (temp, 4) == 4)
  65. return (int) ByteOrder::bigEndianInt (temp);
  66. return 0;
  67. }
  68. int InputStream::readCompressedInt()
  69. {
  70. const uint8 sizeByte = (uint8) readByte();
  71. if (sizeByte == 0)
  72. return 0;
  73. const int numBytes = (sizeByte & 0x7f);
  74. if (numBytes > 4)
  75. {
  76. jassertfalse; // trying to read corrupt data - this method must only be used
  77. // to read data that was written by OutputStream::writeCompressedInt()
  78. return 0;
  79. }
  80. char bytes[4] = { 0, 0, 0, 0 };
  81. if (read (bytes, numBytes) != numBytes)
  82. return 0;
  83. const int num = (int) ByteOrder::littleEndianInt (bytes);
  84. return (sizeByte >> 7) ? -num : num;
  85. }
  86. int64 InputStream::readInt64()
  87. {
  88. union { uint8 asBytes[8]; uint64 asInt64; } n;
  89. if (read (n.asBytes, 8) == 8)
  90. return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
  91. return 0;
  92. }
  93. int64 InputStream::readInt64BigEndian()
  94. {
  95. union { uint8 asBytes[8]; uint64 asInt64; } n;
  96. if (read (n.asBytes, 8) == 8)
  97. return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
  98. return 0;
  99. }
  100. float InputStream::readFloat()
  101. {
  102. // the union below relies on these types being the same size...
  103. static_jassert (sizeof (int32) == sizeof (float));
  104. union { int32 asInt; float asFloat; } n;
  105. n.asInt = (int32) readInt();
  106. return n.asFloat;
  107. }
  108. float InputStream::readFloatBigEndian()
  109. {
  110. union { int32 asInt; float asFloat; } n;
  111. n.asInt = (int32) readIntBigEndian();
  112. return n.asFloat;
  113. }
  114. double InputStream::readDouble()
  115. {
  116. union { int64 asInt; double asDouble; } n;
  117. n.asInt = readInt64();
  118. return n.asDouble;
  119. }
  120. double InputStream::readDoubleBigEndian()
  121. {
  122. union { int64 asInt; double asDouble; } n;
  123. n.asInt = readInt64BigEndian();
  124. return n.asDouble;
  125. }
  126. String InputStream::readString()
  127. {
  128. MemoryBlock buffer (256);
  129. char* data = static_cast<char*> (buffer.getData());
  130. size_t i = 0;
  131. while ((data[i] = readByte()) != 0)
  132. {
  133. if (++i >= buffer.getSize())
  134. {
  135. buffer.setSize (buffer.getSize() + 512);
  136. data = static_cast<char*> (buffer.getData());
  137. }
  138. }
  139. return String::fromUTF8 (data, (int) i);
  140. }
  141. String InputStream::readNextLine()
  142. {
  143. MemoryBlock buffer (256);
  144. char* data = static_cast<char*> (buffer.getData());
  145. size_t i = 0;
  146. while ((data[i] = readByte()) != 0)
  147. {
  148. if (data[i] == '\n')
  149. break;
  150. if (data[i] == '\r')
  151. {
  152. const int64 lastPos = getPosition();
  153. if (readByte() != '\n')
  154. setPosition (lastPos);
  155. break;
  156. }
  157. if (++i >= buffer.getSize())
  158. {
  159. buffer.setSize (buffer.getSize() + 512);
  160. data = static_cast<char*> (buffer.getData());
  161. }
  162. }
  163. return String::fromUTF8 (data, (int) i);
  164. }
  165. size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
  166. {
  167. MemoryOutputStream mo (block, true);
  168. return (size_t) mo.writeFromInputStream (*this, numBytes);
  169. }
  170. String InputStream::readEntireStreamAsString()
  171. {
  172. MemoryOutputStream mo;
  173. mo << *this;
  174. return mo.toString();
  175. }
  176. //==============================================================================
  177. void InputStream::skipNextBytes (int64 numBytesToSkip)
  178. {
  179. if (numBytesToSkip > 0)
  180. {
  181. const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
  182. HeapBlock<char> temp ((size_t) skipBufferSize);
  183. while (numBytesToSkip > 0 && ! isExhausted())
  184. numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
  185. }
  186. }