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.

193 lines
5.8KB

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