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.

161 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. SubregionStream::SubregionStream (InputStream* sourceStream,
  20. int64 start, int64 length,
  21. bool deleteSourceWhenDestroyed)
  22. : source (sourceStream, deleteSourceWhenDestroyed),
  23. startPositionInSourceStream (start),
  24. lengthOfSourceStream (length)
  25. {
  26. SubregionStream::setPosition (0);
  27. }
  28. SubregionStream::~SubregionStream()
  29. {
  30. }
  31. int64 SubregionStream::getTotalLength()
  32. {
  33. auto srcLen = source->getTotalLength() - startPositionInSourceStream;
  34. return lengthOfSourceStream >= 0 ? jmin (lengthOfSourceStream, srcLen)
  35. : srcLen;
  36. }
  37. int64 SubregionStream::getPosition()
  38. {
  39. return source->getPosition() - startPositionInSourceStream;
  40. }
  41. bool SubregionStream::setPosition (int64 newPosition)
  42. {
  43. return source->setPosition (jmax ((int64) 0, newPosition + startPositionInSourceStream));
  44. }
  45. int SubregionStream::read (void* destBuffer, int maxBytesToRead)
  46. {
  47. jassert (destBuffer != nullptr && maxBytesToRead >= 0);
  48. if (lengthOfSourceStream < 0)
  49. return source->read (destBuffer, maxBytesToRead);
  50. maxBytesToRead = (int) jmin ((int64) maxBytesToRead, lengthOfSourceStream - getPosition());
  51. if (maxBytesToRead <= 0)
  52. return 0;
  53. return source->read (destBuffer, maxBytesToRead);
  54. }
  55. bool SubregionStream::isExhausted()
  56. {
  57. if (lengthOfSourceStream >= 0 && getPosition() >= lengthOfSourceStream)
  58. return true;
  59. return source->isExhausted();
  60. }
  61. //==============================================================================
  62. //==============================================================================
  63. #if JUCE_UNIT_TESTS
  64. struct SubregionInputStreamTests : public UnitTest
  65. {
  66. SubregionInputStreamTests()
  67. : UnitTest ("SubregionInputStream", UnitTestCategories::streams)
  68. {}
  69. void runTest() override
  70. {
  71. const MemoryBlock data ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
  72. MemoryInputStream mi (data, true);
  73. const int offset = getRandom().nextInt ((int) data.getSize());
  74. const size_t subregionSize = data.getSize() - (size_t) offset;
  75. SubregionStream stream (&mi, offset, (int) subregionSize, false);
  76. beginTest ("Read");
  77. expectEquals (stream.getPosition(), (int64) 0);
  78. expectEquals (stream.getTotalLength(), (int64) subregionSize);
  79. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  80. expect (! stream.isExhausted());
  81. size_t numBytesRead = 0;
  82. MemoryBlock readBuffer (subregionSize);
  83. while (numBytesRead < subregionSize)
  84. {
  85. numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
  86. expectEquals (stream.getPosition(), (int64) numBytesRead);
  87. expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
  88. expect (stream.isExhausted() == (numBytesRead == subregionSize));
  89. }
  90. expectEquals (stream.getPosition(), (int64) subregionSize);
  91. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  92. expect (stream.isExhausted());
  93. const MemoryBlock memoryBlockToCheck (data.begin() + (size_t) offset, data.getSize() - (size_t) offset);
  94. expect (readBuffer == memoryBlockToCheck);
  95. beginTest ("Skip");
  96. stream.setPosition (0);
  97. expectEquals (stream.getPosition(), (int64) 0);
  98. expectEquals (stream.getTotalLength(), (int64) subregionSize);
  99. expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
  100. expect (! stream.isExhausted());
  101. numBytesRead = 0;
  102. const int64 numBytesToSkip = 5;
  103. while (numBytesRead < subregionSize)
  104. {
  105. stream.skipNextBytes (numBytesToSkip);
  106. numBytesRead += numBytesToSkip;
  107. numBytesRead = std::min (numBytesRead, subregionSize);
  108. expectEquals (stream.getPosition(), (int64) numBytesRead);
  109. expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
  110. expect (stream.isExhausted() == (numBytesRead == subregionSize));
  111. }
  112. expectEquals (stream.getPosition(), (int64) subregionSize);
  113. expectEquals (stream.getNumBytesRemaining(), (int64) 0);
  114. expect (stream.isExhausted());
  115. }
  116. };
  117. static SubregionInputStreamTests subregionInputStreamTests;
  118. #endif
  119. } // namespace juce