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.

188 lines
5.4KB

  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 juce
  18. {
  19. static inline 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 ((size_t) 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. ensureBuffered();
  49. return position < lastReadPos ? *(buffer + (int) (position - bufferStart)) : 0;
  50. }
  51. int64 BufferedInputStream::getTotalLength()
  52. {
  53. return source->getTotalLength();
  54. }
  55. int64 BufferedInputStream::getPosition()
  56. {
  57. return position;
  58. }
  59. bool BufferedInputStream::setPosition (int64 newPosition)
  60. {
  61. position = jmax ((int64) 0, newPosition);
  62. return true;
  63. }
  64. bool BufferedInputStream::isExhausted()
  65. {
  66. return position >= lastReadPos && source->isExhausted();
  67. }
  68. void BufferedInputStream::ensureBuffered()
  69. {
  70. auto bufferEndOverlap = lastReadPos - bufferOverlap;
  71. if (position < bufferStart || position >= bufferEndOverlap)
  72. {
  73. int bytesRead;
  74. if (position < lastReadPos
  75. && position >= bufferEndOverlap
  76. && position >= bufferStart)
  77. {
  78. auto bytesToKeep = (int) (lastReadPos - position);
  79. memmove (buffer, buffer + (int) (position - bufferStart), (size_t) bytesToKeep);
  80. bufferStart = position;
  81. bytesRead = source->read (buffer + bytesToKeep,
  82. (int) (bufferSize - bytesToKeep));
  83. lastReadPos += bytesRead;
  84. bytesRead += bytesToKeep;
  85. }
  86. else
  87. {
  88. bufferStart = position;
  89. source->setPosition (bufferStart);
  90. bytesRead = source->read (buffer, bufferSize);
  91. lastReadPos = bufferStart + bytesRead;
  92. }
  93. while (bytesRead < bufferSize)
  94. buffer [bytesRead++] = 0;
  95. }
  96. }
  97. int BufferedInputStream::read (void* destBuffer, int maxBytesToRead)
  98. {
  99. jassert (destBuffer != nullptr && maxBytesToRead >= 0);
  100. if (position >= bufferStart
  101. && position + maxBytesToRead <= lastReadPos)
  102. {
  103. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) maxBytesToRead);
  104. position += maxBytesToRead;
  105. return maxBytesToRead;
  106. }
  107. if (position < bufferStart || position >= lastReadPos)
  108. ensureBuffered();
  109. int bytesRead = 0;
  110. while (maxBytesToRead > 0)
  111. {
  112. auto numToRead = jmin (maxBytesToRead, (int) (lastReadPos - position));
  113. if (numToRead > 0)
  114. {
  115. memcpy (destBuffer, buffer + (int) (position - bufferStart), (size_t) numToRead);
  116. maxBytesToRead -= numToRead;
  117. bytesRead += numToRead;
  118. position += numToRead;
  119. destBuffer = static_cast<char*> (destBuffer) + numToRead;
  120. }
  121. auto oldLastReadPos = lastReadPos;
  122. ensureBuffered();
  123. if (oldLastReadPos == lastReadPos)
  124. break; // if ensureBuffered() failed to read any more data, bail out
  125. if (isExhausted())
  126. break;
  127. }
  128. return bytesRead;
  129. }
  130. String BufferedInputStream::readString()
  131. {
  132. if (position >= bufferStart
  133. && position < lastReadPos)
  134. {
  135. auto maxChars = (int) (lastReadPos - position);
  136. auto* src = buffer + (int) (position - bufferStart);
  137. for (int i = 0; i < maxChars; ++i)
  138. {
  139. if (src[i] == 0)
  140. {
  141. position += i + 1;
  142. return String::fromUTF8 (src, i);
  143. }
  144. }
  145. }
  146. return InputStream::readString();
  147. }
  148. } // namespace juce