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.

95 lines
4.0KB

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