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.

168 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Copyright (C) 2021 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 "MemoryInputStream.h"
  21. namespace water {
  22. MemoryInputStream::MemoryInputStream (const void* const sourceData,
  23. const size_t sourceDataSize,
  24. const bool keepInternalCopy)
  25. : data (sourceData),
  26. dataSize (sourceDataSize),
  27. position (0)
  28. {
  29. if (keepInternalCopy)
  30. createInternalCopy();
  31. }
  32. MemoryInputStream::MemoryInputStream (const MemoryBlock& sourceData,
  33. const bool keepInternalCopy)
  34. : data (sourceData.getData()),
  35. dataSize (sourceData.getSize()),
  36. position (0)
  37. {
  38. if (keepInternalCopy)
  39. createInternalCopy();
  40. }
  41. void MemoryInputStream::createInternalCopy()
  42. {
  43. internalCopy.malloc (dataSize);
  44. memcpy (internalCopy, data, dataSize);
  45. data = internalCopy;
  46. }
  47. MemoryInputStream::~MemoryInputStream()
  48. {
  49. }
  50. int64 MemoryInputStream::getTotalLength()
  51. {
  52. return (int64) dataSize;
  53. }
  54. int MemoryInputStream::read (void* const buffer, const int howMany)
  55. {
  56. wassert (buffer != nullptr && howMany >= 0);
  57. if (howMany <= 0 || position >= dataSize)
  58. return 0;
  59. const size_t num = jmin ((size_t) howMany, dataSize - position);
  60. if (num > 0)
  61. {
  62. memcpy (buffer, addBytesToPointer (data, position), num);
  63. position += num;
  64. }
  65. return (int) num;
  66. }
  67. bool MemoryInputStream::isExhausted()
  68. {
  69. return position >= dataSize;
  70. }
  71. bool MemoryInputStream::setPosition (const int64 pos)
  72. {
  73. position = (size_t) jlimit ((int64) 0, (int64) dataSize, pos);
  74. return true;
  75. }
  76. int64 MemoryInputStream::getPosition()
  77. {
  78. return (int64) position;
  79. }
  80. //==============================================================================
  81. #if WATER_UNIT_TESTS
  82. class MemoryStreamTests : public UnitTest
  83. {
  84. public:
  85. MemoryStreamTests() : UnitTest ("MemoryInputStream & MemoryOutputStream") {}
  86. void runTest() override
  87. {
  88. beginTest ("Basics");
  89. Random r = getRandom();
  90. int randomInt = r.nextInt();
  91. int64 randomInt64 = r.nextInt64();
  92. double randomDouble = r.nextDouble();
  93. String randomString (createRandomWideCharString (r));
  94. MemoryOutputStream mo;
  95. mo.writeInt (randomInt);
  96. mo.writeIntBigEndian (randomInt);
  97. mo.writeCompressedInt (randomInt);
  98. mo.writeString (randomString);
  99. mo.writeInt64 (randomInt64);
  100. mo.writeInt64BigEndian (randomInt64);
  101. mo.writeDouble (randomDouble);
  102. mo.writeDoubleBigEndian (randomDouble);
  103. MemoryInputStream mi (mo.getData(), mo.getDataSize(), false);
  104. expect (mi.readInt() == randomInt);
  105. expect (mi.readIntBigEndian() == randomInt);
  106. expect (mi.readCompressedInt() == randomInt);
  107. expectEquals (mi.readString(), randomString);
  108. expect (mi.readInt64() == randomInt64);
  109. expect (mi.readInt64BigEndian() == randomInt64);
  110. expect (mi.readDouble() == randomDouble);
  111. expect (mi.readDoubleBigEndian() == randomDouble);
  112. }
  113. static String createRandomWideCharString (Random& r)
  114. {
  115. water_wchar buffer [50] = { 0 };
  116. for (int i = 0; i < numElementsInArray (buffer) - 1; ++i)
  117. {
  118. if (r.nextBool())
  119. {
  120. do
  121. {
  122. buffer[i] = (water_wchar) (1 + r.nextInt (0x10ffff - 1));
  123. }
  124. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  125. }
  126. else
  127. buffer[i] = (water_wchar) (1 + r.nextInt (0xff));
  128. }
  129. return CharPointer_UTF32 (buffer);
  130. }
  131. };
  132. static MemoryStreamTests memoryInputStreamUnitTests;
  133. #endif
  134. }