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.

205 lines
5.7KB

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