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.

279 lines
8.4KB

  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. static int calcBufferStreamBufferSize (int requestedSize, InputStream* source) noexcept
  20. {
  21. // You need to supply a real stream when creating a BufferedInputStream
  22. jassert (source != nullptr);
  23. requestedSize = jmax (256, requestedSize);
  24. auto sourceSize = source->getTotalLength();
  25. if (sourceSize >= 0 && sourceSize < requestedSize)
  26. return jmax (32, (int) sourceSize);
  27. return requestedSize;
  28. }
  29. //==============================================================================
  30. BufferedInputStream::BufferedInputStream (InputStream* sourceStream, int size, bool takeOwnership)
  31. : source (sourceStream, takeOwnership),
  32. bufferSize (calcBufferStreamBufferSize (size, sourceStream)),
  33. position (sourceStream->getPosition()),
  34. bufferStart (position)
  35. {
  36. buffer.malloc (bufferSize);
  37. }
  38. BufferedInputStream::BufferedInputStream (InputStream& sourceStream, int size)
  39. : BufferedInputStream (&sourceStream, size, false)
  40. {
  41. }
  42. BufferedInputStream::~BufferedInputStream()
  43. {
  44. }
  45. //==============================================================================
  46. char BufferedInputStream::peekByte()
  47. {
  48. if (! ensureBuffered())
  49. return 0;
  50. return position < lastReadPos ? buffer[(int) (position - bufferStart)] : 0;
  51. }
  52. int64 BufferedInputStream::getTotalLength()
  53. {
  54. return source->getTotalLength();
  55. }
  56. int64 BufferedInputStream::getPosition()
  57. {
  58. return position;
  59. }
  60. bool BufferedInputStream::setPosition (int64 newPosition)
  61. {
  62. position = jmax ((int64) 0, newPosition);
  63. return true;
  64. }
  65. bool BufferedInputStream::isExhausted()
  66. {
  67. return position >= lastReadPos && source->isExhausted();
  68. }
  69. bool BufferedInputStream::ensureBuffered()
  70. {
  71. auto bufferEndOverlap = lastReadPos - bufferOverlap;
  72. if (position < bufferStart || position >= bufferEndOverlap)
  73. {
  74. int bytesRead;
  75. if (position < lastReadPos
  76. && position >= bufferEndOverlap
  77. && position >= bufferStart)
  78. {
  79. auto bytesToKeep = (int) (lastReadPos - position);
  80. memmove (buffer, buffer + (int) (position - bufferStart), (size_t) bytesToKeep);
  81. bufferStart = position;
  82. bytesRead = source->read (buffer + bytesToKeep,
  83. (int) (bufferSize - bytesToKeep));
  84. if (bytesRead < 0)
  85. return false;
  86. lastReadPos += bytesRead;
  87. bytesRead += bytesToKeep;
  88. }
  89. else
  90. {
  91. bufferStart = position;
  92. if (! source->setPosition (bufferStart))
  93. return false;
  94. bytesRead = source->read (buffer, bufferSize);
  95. if (bytesRead < 0)
  96. return false;
  97. lastReadPos = bufferStart + bytesRead;
  98. }
  99. while (bytesRead < bufferSize)
  100. buffer[bytesRead++] = 0;
  101. }
  102. return true;
  103. }
  104. int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
  105. {
  106. jassert (destBuffer != nullptr && maxBytesToRead >= 0);
  107. if (position >= bufferStart
  108. && position + maxBytesToRead <= lastReadPos)
  109. {
  110. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) maxBytesToRead);
  111. position += maxBytesToRead;
  112. return maxBytesToRead;
  113. }
  114. if (position < bufferStart || position >= lastReadPos)
  115. if (! ensureBuffered())
  116. return 0;
  117. int bytesRead = 0;
  118. while (maxBytesToRead > 0)
  119. {
  120. auto numToRead = jmin (maxBytesToRead, (int) (lastReadPos - position));
  121. if (numToRead > 0)
  122. {
  123. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) numToRead);
  124. maxBytesToRead -= numToRead;
  125. bytesRead += numToRead;
  126. position += numToRead;
  127. destBuffer = static_cast<char*> (destBuffer) + numToRead;
  128. }
  129. auto oldLastReadPos = lastReadPos;
  130. if (! ensureBuffered()
  131. || oldLastReadPos == lastReadPos
  132. || isExhausted())
  133. break;
  134. }
  135. return bytesRead;
  136. }
  137. String BufferedInputStream::readString()
  138. {
  139. if (position >= bufferStart
  140. && position < lastReadPos)
  141. {
  142. auto maxChars = (int) (lastReadPos - position);
  143. auto* src = buffer + (int) (position - bufferStart);
  144. for (int i = 0; i < maxChars; ++i)
  145. {
  146. if (src[i] == 0)
  147. {
  148. position += i + 1;
  149. return String::fromUTF8 (src, i);
  150. }
  151. }
  152. }
  153. return InputStream::readString();
  154. }
  155. //==============================================================================
  156. //==============================================================================
  157. #if JUCE_UNIT_TESTS
  158. struct BufferedInputStreamTests : public UnitTest
  159. {
  160. BufferedInputStreamTests()
  161. : UnitTest ("BufferedInputStream", UnitTestCategories::streams)
  162. {}
  163. void runTest() override
  164. {
  165. const MemoryBlock data ("abcdefghijklmnopqrstuvwxyz", 26);
  166. MemoryInputStream mi (data, true);
  167. BufferedInputStream stream (mi, (int) data.getSize());
  168. beginTest ("Read");
  169. expectEquals (stream.getPosition(), (int64) 0);
  170. expectEquals (stream.getTotalLength(), (int64) data.getSize());
  171. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  172. expect (! stream.isExhausted());
  173. size_t numBytesRead = 0;
  174. MemoryBlock readBuffer (data.getSize());
  175. while (numBytesRead < data.getSize())
  176. {
  177. expectEquals (stream.peekByte(), *(char*) (data.begin() + numBytesRead));
  178. numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
  179. expectEquals (stream.getPosition(), (int64) numBytesRead);
  180. expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
  181. expect (stream.isExhausted() == (numBytesRead == data.getSize()));
  182. }
  183. expectEquals (stream.getPosition(), (int64) data.getSize());
  184. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  185. expect (stream.isExhausted());
  186. expect (readBuffer == data);
  187. beginTest ("Skip");
  188. stream.setPosition (0);
  189. expectEquals (stream.getPosition(), (int64) 0);
  190. expectEquals (stream.getTotalLength(), (int64) data.getSize());
  191. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  192. expect (! stream.isExhausted());
  193. numBytesRead = 0;
  194. const int numBytesToSkip = 5;
  195. while (numBytesRead < data.getSize())
  196. {
  197. expectEquals (stream.peekByte(), *(char*) (data.begin() + numBytesRead));
  198. stream.skipNextBytes (numBytesToSkip);
  199. numBytesRead += numBytesToSkip;
  200. numBytesRead = std::min (numBytesRead, data.getSize());
  201. expectEquals (stream.getPosition(), (int64) numBytesRead);
  202. expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
  203. expect (stream.isExhausted() == (numBytesRead == data.getSize()));
  204. }
  205. expectEquals (stream.getPosition(), (int64) data.getSize());
  206. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  207. expect (stream.isExhausted());
  208. }
  209. };
  210. static BufferedInputStreamTests bufferedInputStreamTests;
  211. #endif
  212. } // namespace juce