juce_BufferedInputStream.h 4.0 KB

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