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.8KB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 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. /** Creates a MemoryOutputStream that will write into a user-supplied, fixed-size
  52. block of memory.
  53. When using this mode, the stream will write directly into this memory area until
  54. it's full, at which point write operations will fail.
  55. */
  56. MemoryOutputStream (void* destBuffer, size_t destBufferSize);
  57. /** Destructor.
  58. This will free any data that was written to it.
  59. */
  60. ~MemoryOutputStream();
  61. //==============================================================================
  62. /** Returns a pointer to the data that has been written to the stream.
  63. @see getDataSize
  64. */
  65. const void* getData() const noexcept;
  66. /** Returns the number of bytes of data that have been written to the stream.
  67. @see getData
  68. */
  69. size_t getDataSize() const noexcept { return size; }
  70. /** Resets the stream, clearing any data that has been written to it so far. */
  71. void reset() noexcept;
  72. /** Increases the internal storage capacity to be able to contain at least the specified
  73. amount of data without needing to be resized.
  74. */
  75. void preallocate (size_t bytesToPreallocate);
  76. /** Appends the utf-8 bytes for a unicode character */
  77. bool appendUTF8Char (water_uchar character);
  78. /** Returns a String created from the (UTF8) data that has been written to the stream. */
  79. String toUTF8() const;
  80. /** Attempts to detect the encoding of the data and convert it to a string.
  81. @see String::createStringFromData
  82. */
  83. String toString() const;
  84. /** Returns a copy of the stream's data as a memory block. */
  85. MemoryBlock getMemoryBlock() const;
  86. //==============================================================================
  87. /** If the stream is writing to a user-supplied MemoryBlock, this will trim any excess
  88. capacity off the block, so that its length matches the amount of actual data that
  89. has been written so far.
  90. */
  91. void flush() override;
  92. bool write (const void*, size_t) override;
  93. int64 getPosition() override { return (int64) position; }
  94. bool setPosition (int64) override;
  95. int64 writeFromInputStream (InputStream&, int64 maxNumBytesToWrite) override;
  96. bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
  97. private:
  98. //==============================================================================
  99. MemoryBlock* const blockToUse;
  100. MemoryBlock internalBlock;
  101. void* externalData;
  102. size_t position, size, availableSize;
  103. void trimExternalBlockSize();
  104. char* prepareToWrite (size_t);
  105. CARLA_DECLARE_NON_COPY_CLASS (MemoryOutputStream)
  106. };
  107. /** Copies all the data that has been written to a MemoryOutputStream into another stream. */
  108. OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead);
  109. }
  110. #endif // WATER_MEMORYOUTPUTSTREAM_H_INCLUDED