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.

82 lines
2.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. } // namespace juce