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.

84 lines
3.3KB

  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. #pragma once
  18. //==============================================================================
  19. /** Wraps another input stream, and reads from it using an intermediate buffer
  20. If you're using an input stream such as a file input stream, and making lots of
  21. small read accesses to it, it's probably sensible to wrap it in one of these,
  22. so that the source stream gets accessed in larger chunk sizes, meaning less
  23. work for the underlying stream.
  24. */
  25. class JUCE_API BufferedInputStream : public InputStream
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a BufferedInputStream from an input source.
  30. @param sourceStream the source stream to read from
  31. @param bufferSize the size of reservoir to use to buffer the source
  32. @param deleteSourceWhenDestroyed whether the sourceStream that is passed in should be
  33. deleted by this object when it is itself deleted.
  34. */
  35. BufferedInputStream (InputStream* sourceStream,
  36. int bufferSize,
  37. bool deleteSourceWhenDestroyed);
  38. /** Creates a BufferedInputStream from an input source.
  39. @param sourceStream the source stream to read from - the source stream must not
  40. be deleted until this object has been destroyed.
  41. @param bufferSize the size of reservoir to use to buffer the source
  42. */
  43. BufferedInputStream (InputStream& sourceStream, int bufferSize);
  44. /** Destructor.
  45. This may also delete the source stream, if that option was chosen when the
  46. buffered stream was created.
  47. */
  48. ~BufferedInputStream();
  49. //==============================================================================
  50. int64 getTotalLength() override;
  51. int64 getPosition() override;
  52. bool setPosition (int64 newPosition) override;
  53. int read (void* destBuffer, int maxBytesToRead) override;
  54. String readString() override;
  55. bool isExhausted() override;
  56. private:
  57. //==============================================================================
  58. OptionalScopedPointer<InputStream> source;
  59. int bufferSize;
  60. int64 position, lastReadPos, bufferStart, bufferOverlap;
  61. HeapBlock<char> buffer;
  62. void ensureBuffered();
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferedInputStream)
  64. };