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.

201 lines
6.3KB

  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. namespace
  24. {
  25. int calcBufferStreamBufferSize (int requestedSize, InputStream* const source) noexcept
  26. {
  27. // You need to supply a real stream when creating a BufferedInputStream
  28. jassert (source != nullptr);
  29. requestedSize = jmax (256, requestedSize);
  30. const int64 sourceSize = source->getTotalLength();
  31. if (sourceSize >= 0 && sourceSize < requestedSize)
  32. requestedSize = jmax (32, (int) sourceSize);
  33. return requestedSize;
  34. }
  35. }
  36. //==============================================================================
  37. BufferedInputStream::BufferedInputStream (InputStream* const sourceStream, const int bufferSize_,
  38. const bool deleteSourceWhenDestroyed)
  39. : source (sourceStream, deleteSourceWhenDestroyed),
  40. bufferSize (calcBufferStreamBufferSize (bufferSize_, sourceStream)),
  41. position (sourceStream->getPosition()),
  42. lastReadPos (0),
  43. bufferStart (position),
  44. bufferOverlap (128)
  45. {
  46. buffer.malloc ((size_t) bufferSize);
  47. }
  48. BufferedInputStream::BufferedInputStream (InputStream& sourceStream, const int bufferSize_)
  49. : source (&sourceStream, false),
  50. bufferSize (calcBufferStreamBufferSize (bufferSize_, &sourceStream)),
  51. position (sourceStream.getPosition()),
  52. lastReadPos (0),
  53. bufferStart (position),
  54. bufferOverlap (128)
  55. {
  56. buffer.malloc ((size_t) bufferSize);
  57. }
  58. BufferedInputStream::~BufferedInputStream()
  59. {
  60. }
  61. //==============================================================================
  62. int64 BufferedInputStream::getTotalLength()
  63. {
  64. return source->getTotalLength();
  65. }
  66. int64 BufferedInputStream::getPosition()
  67. {
  68. return position;
  69. }
  70. bool BufferedInputStream::setPosition (int64 newPosition)
  71. {
  72. position = jmax ((int64) 0, newPosition);
  73. return true;
  74. }
  75. bool BufferedInputStream::isExhausted()
  76. {
  77. return position >= lastReadPos && source->isExhausted();
  78. }
  79. void BufferedInputStream::ensureBuffered()
  80. {
  81. const int64 bufferEndOverlap = lastReadPos - bufferOverlap;
  82. if (position < bufferStart || position >= bufferEndOverlap)
  83. {
  84. int bytesRead;
  85. if (position < lastReadPos
  86. && position >= bufferEndOverlap
  87. && position >= bufferStart)
  88. {
  89. const int bytesToKeep = (int) (lastReadPos - position);
  90. memmove (buffer, buffer + (int) (position - bufferStart), (size_t) bytesToKeep);
  91. bufferStart = position;
  92. bytesRead = source->read (buffer + bytesToKeep,
  93. (int) (bufferSize - bytesToKeep));
  94. lastReadPos += bytesRead;
  95. bytesRead += bytesToKeep;
  96. }
  97. else
  98. {
  99. bufferStart = position;
  100. source->setPosition (bufferStart);
  101. bytesRead = source->read (buffer, bufferSize);
  102. lastReadPos = bufferStart + bytesRead;
  103. }
  104. while (bytesRead < bufferSize)
  105. buffer [bytesRead++] = 0;
  106. }
  107. }
  108. int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
  109. {
  110. jassert (destBuffer != nullptr && maxBytesToRead >= 0);
  111. if (position >= bufferStart
  112. && position + maxBytesToRead <= lastReadPos)
  113. {
  114. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) maxBytesToRead);
  115. position += maxBytesToRead;
  116. return maxBytesToRead;
  117. }
  118. else
  119. {
  120. if (position < bufferStart || position >= lastReadPos)
  121. ensureBuffered();
  122. int bytesRead = 0;
  123. while (maxBytesToRead > 0)
  124. {
  125. const int bytesAvailable = jmin (maxBytesToRead, (int) (lastReadPos - position));
  126. if (bytesAvailable > 0)
  127. {
  128. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) bytesAvailable);
  129. maxBytesToRead -= bytesAvailable;
  130. bytesRead += bytesAvailable;
  131. position += bytesAvailable;
  132. destBuffer = static_cast<char*> (destBuffer) + bytesAvailable;
  133. }
  134. const int64 oldLastReadPos = lastReadPos;
  135. ensureBuffered();
  136. if (oldLastReadPos == lastReadPos)
  137. break; // if ensureBuffered() failed to read any more data, bail out
  138. if (isExhausted())
  139. break;
  140. }
  141. return bytesRead;
  142. }
  143. }
  144. String BufferedInputStream::readString()
  145. {
  146. if (position >= bufferStart
  147. && position < lastReadPos)
  148. {
  149. const int maxChars = (int) (lastReadPos - position);
  150. const char* const src = buffer + (int) (position - bufferStart);
  151. for (int i = 0; i < maxChars; ++i)
  152. {
  153. if (src[i] == 0)
  154. {
  155. position += i + 1;
  156. return String::fromUTF8 (src, i);
  157. }
  158. }
  159. }
  160. return InputStream::readString();
  161. }