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.

91 lines
3.5KB

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