Audio plugin host https://kx.studio/carla
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.

219 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "MemoryOutputStream.h"
  21. #include "InputStream.h"
  22. namespace water {
  23. MemoryOutputStream::MemoryOutputStream (const size_t initialSize)
  24. : blockToUse (&internalBlock), externalData (nullptr),
  25. position (0), size (0), availableSize (0)
  26. {
  27. internalBlock.setSize (initialSize, false);
  28. }
  29. MemoryOutputStream::MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
  30. const bool appendToExistingBlockContent)
  31. : blockToUse (&memoryBlockToWriteTo), externalData (nullptr),
  32. position (0), size (0), availableSize (0)
  33. {
  34. if (appendToExistingBlockContent)
  35. position = size = memoryBlockToWriteTo.getSize();
  36. }
  37. MemoryOutputStream::MemoryOutputStream (void* destBuffer, size_t destBufferSize)
  38. : blockToUse (nullptr), externalData (destBuffer),
  39. position (0), size (0), availableSize (destBufferSize)
  40. {
  41. jassert (externalData != nullptr); // This must be a valid pointer.
  42. }
  43. MemoryOutputStream::~MemoryOutputStream()
  44. {
  45. trimExternalBlockSize();
  46. }
  47. void MemoryOutputStream::flush()
  48. {
  49. trimExternalBlockSize();
  50. }
  51. void MemoryOutputStream::trimExternalBlockSize()
  52. {
  53. if (blockToUse != &internalBlock && blockToUse != nullptr)
  54. blockToUse->setSize (size, false);
  55. }
  56. void MemoryOutputStream::preallocate (const size_t bytesToPreallocate)
  57. {
  58. if (blockToUse != nullptr)
  59. blockToUse->ensureSize (bytesToPreallocate + 1);
  60. }
  61. void MemoryOutputStream::reset() noexcept
  62. {
  63. position = 0;
  64. size = 0;
  65. }
  66. char* MemoryOutputStream::prepareToWrite (size_t numBytes)
  67. {
  68. jassert ((ssize_t) numBytes >= 0);
  69. size_t storageNeeded = position + numBytes;
  70. char* data;
  71. if (blockToUse != nullptr)
  72. {
  73. if (storageNeeded >= blockToUse->getSize())
  74. blockToUse->ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31u);
  75. data = static_cast<char*> (blockToUse->getData());
  76. }
  77. else
  78. {
  79. if (storageNeeded > availableSize)
  80. return nullptr;
  81. data = static_cast<char*> (externalData);
  82. }
  83. char* const writePointer = data + position;
  84. position += numBytes;
  85. size = jmax (size, position);
  86. return writePointer;
  87. }
  88. bool MemoryOutputStream::write (const void* const buffer, size_t howMany)
  89. {
  90. jassert (buffer != nullptr);
  91. if (howMany == 0)
  92. return true;
  93. if (char* dest = prepareToWrite (howMany))
  94. {
  95. memcpy (dest, buffer, howMany);
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany)
  101. {
  102. if (howMany == 0)
  103. return true;
  104. if (char* dest = prepareToWrite (howMany))
  105. {
  106. memset (dest, byte, howMany);
  107. return true;
  108. }
  109. return false;
  110. }
  111. bool MemoryOutputStream::appendUTF8Char (water_uchar c)
  112. {
  113. if (char* dest = prepareToWrite (CharPointer_UTF8::getBytesRequiredFor (c)))
  114. {
  115. CharPointer_UTF8 (dest).write (c);
  116. return true;
  117. }
  118. return false;
  119. }
  120. MemoryBlock MemoryOutputStream::getMemoryBlock() const
  121. {
  122. return MemoryBlock (getData(), getDataSize());
  123. }
  124. const void* MemoryOutputStream::getData() const noexcept
  125. {
  126. if (blockToUse == nullptr)
  127. return externalData;
  128. if (blockToUse->getSize() > size)
  129. static_cast<char*> (blockToUse->getData()) [size] = 0;
  130. return blockToUse->getData();
  131. }
  132. bool MemoryOutputStream::setPosition (int64 newPosition)
  133. {
  134. if (newPosition <= (int64) size)
  135. {
  136. // ok to seek backwards
  137. position = jlimit ((size_t) 0, size, (size_t) newPosition);
  138. return true;
  139. }
  140. // can't move beyond the end of the stream..
  141. return false;
  142. }
  143. int64 MemoryOutputStream::writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite)
  144. {
  145. // before writing from an input, see if we can preallocate to make it more efficient..
  146. int64 availableData = source.getTotalLength() - source.getPosition();
  147. if (availableData > 0)
  148. {
  149. if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0)
  150. maxNumBytesToWrite = availableData;
  151. if (blockToUse != nullptr)
  152. preallocate (blockToUse->getSize() + (size_t) maxNumBytesToWrite);
  153. }
  154. return OutputStream::writeFromInputStream (source, maxNumBytesToWrite);
  155. }
  156. String MemoryOutputStream::toUTF8() const
  157. {
  158. const char* const d = static_cast<const char*> (getData());
  159. return String (CharPointer_UTF8 (d), CharPointer_UTF8 (d + getDataSize()));
  160. }
  161. String MemoryOutputStream::toString() const
  162. {
  163. return String::createStringFromData (getData(), (int) getDataSize());
  164. }
  165. OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead)
  166. {
  167. const size_t dataSize = streamToRead.getDataSize();
  168. if (dataSize > 0)
  169. stream.write (streamToRead.getData(), dataSize);
  170. return stream;
  171. }
  172. }