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.

203 lines
5.6KB

  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. : internalBlock(), blockToUse (internalBlock),
  25. position (0), size (0)
  26. {
  27. internalBlock.setSize (initialSize, false);
  28. }
  29. MemoryOutputStream::MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
  30. const bool appendToExistingBlockContent)
  31. : internalBlock(), blockToUse (memoryBlockToWriteTo),
  32. position (0), size (0)
  33. {
  34. if (appendToExistingBlockContent)
  35. position = size = memoryBlockToWriteTo.getSize();
  36. }
  37. MemoryOutputStream::~MemoryOutputStream()
  38. {
  39. trimExternalBlockSize();
  40. }
  41. void MemoryOutputStream::flush()
  42. {
  43. trimExternalBlockSize();
  44. }
  45. void MemoryOutputStream::trimExternalBlockSize()
  46. {
  47. if (blockToUse != internalBlock)
  48. blockToUse.setSize (size, false);
  49. }
  50. void MemoryOutputStream::preallocate (const size_t bytesToPreallocate)
  51. {
  52. blockToUse.ensureSize (bytesToPreallocate + 1);
  53. }
  54. void MemoryOutputStream::reset() noexcept
  55. {
  56. position = 0;
  57. size = 0;
  58. }
  59. char* MemoryOutputStream::prepareToWrite (size_t numBytes)
  60. {
  61. CARLA_SAFE_ASSERT_RETURN ((ssize_t) numBytes >= 0, nullptr);
  62. const size_t storageNeeded = position + numBytes;
  63. if (storageNeeded >= blockToUse.getSize())
  64. blockToUse.ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31u);
  65. char* const data = static_cast<char*> (blockToUse.getData());
  66. char* const writePointer = data + position;
  67. position += numBytes;
  68. size = jmax (size, position);
  69. return writePointer;
  70. }
  71. bool MemoryOutputStream::write (const void* const buffer, size_t howMany)
  72. {
  73. CARLA_SAFE_ASSERT_RETURN (buffer != nullptr, false);
  74. if (howMany == 0)
  75. return true;
  76. if (char* const dest = prepareToWrite (howMany))
  77. {
  78. std::memcpy (dest, buffer, howMany);
  79. return true;
  80. }
  81. return false;
  82. }
  83. bool MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany)
  84. {
  85. if (howMany == 0)
  86. return true;
  87. if (char* dest = prepareToWrite (howMany))
  88. {
  89. memset (dest, byte, howMany);
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool MemoryOutputStream::appendUTF8Char (water_uchar c)
  95. {
  96. if (char* dest = prepareToWrite (CharPointer_UTF8::getBytesRequiredFor (c)))
  97. {
  98. CharPointer_UTF8 (dest).write (c);
  99. return true;
  100. }
  101. return false;
  102. }
  103. MemoryBlock MemoryOutputStream::getMemoryBlock() const
  104. {
  105. return MemoryBlock (getData(), getDataSize());
  106. }
  107. const void* MemoryOutputStream::getData() const noexcept
  108. {
  109. if (blockToUse.getSize() > size)
  110. static_cast<char*> (blockToUse.getData()) [size] = 0;
  111. return blockToUse.getData();
  112. }
  113. void* MemoryOutputStream::getDataAndRelease() noexcept
  114. {
  115. if (blockToUse.getSize() > size)
  116. static_cast<char*> (blockToUse.getData()) [size] = 0;
  117. return blockToUse.release();
  118. }
  119. bool MemoryOutputStream::setPosition (int64 newPosition)
  120. {
  121. if (newPosition <= (int64) size)
  122. {
  123. // ok to seek backwards
  124. position = jlimit ((size_t) 0, size, (size_t) newPosition);
  125. return true;
  126. }
  127. // can't move beyond the end of the stream..
  128. return false;
  129. }
  130. int64 MemoryOutputStream::writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite)
  131. {
  132. // before writing from an input, see if we can preallocate to make it more efficient..
  133. int64 availableData = source.getTotalLength() - source.getPosition();
  134. if (availableData > 0)
  135. {
  136. if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0)
  137. maxNumBytesToWrite = availableData;
  138. preallocate (blockToUse.getSize() + (size_t) maxNumBytesToWrite);
  139. }
  140. return OutputStream::writeFromInputStream (source, maxNumBytesToWrite);
  141. }
  142. String MemoryOutputStream::toUTF8() const
  143. {
  144. const char* const d = static_cast<const char*> (getData());
  145. return String (CharPointer_UTF8 (d), CharPointer_UTF8 (d + getDataSize()));
  146. }
  147. String MemoryOutputStream::toString() const
  148. {
  149. return String::createStringFromData (getData(), (int) getDataSize());
  150. }
  151. OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead)
  152. {
  153. const size_t dataSize = streamToRead.getDataSize();
  154. if (dataSize > 0)
  155. stream.write (streamToRead.getData(), dataSize);
  156. return stream;
  157. }
  158. }