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.

183 lines
5.3KB

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