Audio plugin host https://kx.studio/carla
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.

MemoryOutputStream.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_MEMORYOUTPUTSTREAM_H_INCLUDED
  21. #define WATER_MEMORYOUTPUTSTREAM_H_INCLUDED
  22. #include "OutputStream.h"
  23. #include "../memory/MemoryBlock.h"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Writes data to an internal memory buffer, which grows as required.
  28. The data that was written into the stream can then be accessed later as
  29. a contiguous block of memory.
  30. */
  31. class MemoryOutputStream : public OutputStream
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty memory stream, ready to be written into.
  36. @param initialSize the intial amount of capacity to allocate for writing into
  37. */
  38. MemoryOutputStream (size_t initialSize = 256);
  39. /** Creates a memory stream for writing into into a pre-existing MemoryBlock object.
  40. Note that the destination block will always be larger than the amount of data
  41. that has been written to the stream, because the MemoryOutputStream keeps some
  42. spare capactity at its end. To trim the block's size down to fit the actual
  43. data, call flush(), or delete the MemoryOutputStream.
  44. @param memoryBlockToWriteTo the block into which new data will be written.
  45. @param appendToExistingBlockContent if this is true, the contents of the block will be
  46. kept, and new data will be appended to it. If false,
  47. the block will be cleared before use
  48. */
  49. MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
  50. bool appendToExistingBlockContent);
  51. /** Destructor.
  52. This will free any data that was written to it.
  53. */
  54. ~MemoryOutputStream();
  55. //==============================================================================
  56. /** Returns a pointer to the data that has been written to the stream.
  57. @see getDataSize
  58. */
  59. const void* getData() const noexcept;
  60. /** Returns a pointer to the data that has been written to the stream and releases the buffer pointer.
  61. @see getDataSize
  62. */
  63. void* getDataAndRelease() noexcept;
  64. /** Returns the number of bytes of data that have been written to the stream.
  65. @see getData
  66. */
  67. size_t getDataSize() const noexcept { return size; }
  68. /** Resets the stream, clearing any data that has been written to it so far. */
  69. void reset() noexcept;
  70. /** Increases the internal storage capacity to be able to contain at least the specified
  71. amount of data without needing to be resized.
  72. */
  73. void preallocate (size_t bytesToPreallocate);
  74. /** Appends the utf-8 bytes for a unicode character */
  75. bool appendUTF8Char (water_uchar character);
  76. /** Returns a String created from the (UTF8) data that has been written to the stream. */
  77. String toUTF8() const;
  78. /** Attempts to detect the encoding of the data and convert it to a string.
  79. @see String::createStringFromData
  80. */
  81. String toString() const;
  82. /** Returns a copy of the stream's data as a memory block. */
  83. MemoryBlock getMemoryBlock() const;
  84. //==============================================================================
  85. /** If the stream is writing to a user-supplied MemoryBlock, this will trim any excess
  86. capacity off the block, so that its length matches the amount of actual data that
  87. has been written so far.
  88. */
  89. void flush() override;
  90. bool write (const void*, size_t) override;
  91. int64 getPosition() override { return (int64) position; }
  92. bool setPosition (int64) override;
  93. int64 writeFromInputStream (InputStream&, int64 maxNumBytesToWrite) override;
  94. bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
  95. private:
  96. //==============================================================================
  97. MemoryBlock internalBlock;
  98. MemoryBlock& blockToUse;
  99. size_t position, size;
  100. bool usingInternalBlock;
  101. void trimExternalBlockSize();
  102. char* prepareToWrite (size_t);
  103. CARLA_DECLARE_NON_COPYABLE (MemoryOutputStream)
  104. };
  105. /** Copies all the data that has been written to a MemoryOutputStream into another stream. */
  106. OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead);
  107. }
  108. #endif // WATER_MEMORYOUTPUTSTREAM_H_INCLUDED