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.

97 lines
4.1KB

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