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.

92 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /** Wraps another input stream, and reads from it using an intermediate buffer
  26. If you're using an input stream such as a file input stream, and making lots of
  27. small read accesses to it, it's probably sensible to wrap it in one of these,
  28. so that the source stream gets accessed in larger chunk sizes, meaning less
  29. work for the underlying stream.
  30. */
  31. class JUCE_API BufferedInputStream : public InputStream
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a BufferedInputStream from an input source.
  36. @param sourceStream the source stream to read from
  37. @param bufferSize the size of reservoir to use to buffer the source
  38. @param deleteSourceWhenDestroyed whether the sourceStream that is passed in should be
  39. deleted by this object when it is itself deleted.
  40. */
  41. BufferedInputStream (InputStream* sourceStream,
  42. int bufferSize,
  43. bool deleteSourceWhenDestroyed);
  44. /** Creates a BufferedInputStream from an input source.
  45. @param sourceStream the source stream to read from - the source stream must not
  46. be deleted until this object has been destroyed.
  47. @param bufferSize the size of reservoir to use to buffer the source
  48. */
  49. BufferedInputStream (InputStream& sourceStream, int bufferSize);
  50. /** Destructor.
  51. This may also delete the source stream, if that option was chosen when the
  52. buffered stream was created.
  53. */
  54. ~BufferedInputStream();
  55. //==============================================================================
  56. int64 getTotalLength() override;
  57. int64 getPosition() override;
  58. bool setPosition (int64 newPosition) override;
  59. int read (void* destBuffer, int maxBytesToRead) override;
  60. String readString() override;
  61. bool isExhausted() override;
  62. private:
  63. //==============================================================================
  64. OptionalScopedPointer<InputStream> source;
  65. int bufferSize;
  66. int64 position, lastReadPos, bufferStart, bufferOverlap;
  67. HeapBlock<char> buffer;
  68. void ensureBuffered();
  69. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferedInputStream)
  70. };