diff --git a/modules/juce_core/streams/juce_MemoryOutputStream.cpp b/modules/juce_core/streams/juce_MemoryOutputStream.cpp index 79d51d41fb..a6e98ce5c4 100644 --- a/modules/juce_core/streams/juce_MemoryOutputStream.cpp +++ b/modules/juce_core/streams/juce_MemoryOutputStream.cpp @@ -68,13 +68,18 @@ void MemoryOutputStream::reset() noexcept size = 0; } -void MemoryOutputStream::prepareToWrite (size_t numBytes) +char* MemoryOutputStream::prepareToWrite (size_t numBytes) { jassert ((ssize_t) numBytes >= 0); size_t storageNeeded = position + numBytes; if (storageNeeded >= data.getSize()) data.ensureSize ((storageNeeded + jmin (storageNeeded / 2, (size_t) (1024 * 1024)) + 32) & ~31u); + + char* const writePointer = static_cast (data.getData()) + position; + position += numBytes; + size = jmax (size, position); + return writePointer; } bool MemoryOutputStream::write (const void* const buffer, size_t howMany) @@ -82,12 +87,7 @@ bool MemoryOutputStream::write (const void* const buffer, size_t howMany) jassert (buffer != nullptr && ((ssize_t) howMany) >= 0); if (howMany > 0) - { - prepareToWrite (howMany); - memcpy (static_cast (data.getData()) + position, buffer, howMany); - position += howMany; - size = jmax (size, position); - } + memcpy (prepareToWrite (howMany), buffer, howMany); return true; } @@ -95,12 +95,12 @@ bool MemoryOutputStream::write (const void* const buffer, size_t howMany) void MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany) { if (howMany > 0) - { - prepareToWrite (howMany); - memset (static_cast (data.getData()) + position, byte, howMany); - position += howMany; - size = jmax (size, position); - } + memset (prepareToWrite (howMany), byte, howMany); +} + +void MemoryOutputStream::appendUTF8Char (juce_wchar c) +{ + CharPointer_UTF8 (prepareToWrite (CharPointer_UTF8::getBytesRequiredFor (c))).write (c); } MemoryBlock MemoryOutputStream::getMemoryBlock() const diff --git a/modules/juce_core/streams/juce_MemoryOutputStream.h b/modules/juce_core/streams/juce_MemoryOutputStream.h index da8422a7f2..cb969db556 100644 --- a/modules/juce_core/streams/juce_MemoryOutputStream.h +++ b/modules/juce_core/streams/juce_MemoryOutputStream.h @@ -42,7 +42,7 @@ class JUCE_API MemoryOutputStream : public OutputStream { public: //============================================================================== - /** Creates an empty memory stream ready for writing into. + /** Creates an empty memory stream, ready to be written into. @param initialSize the intial amount of capacity to allocate for writing into */ @@ -70,13 +70,11 @@ public: //============================================================================== /** Returns a pointer to the data that has been written to the stream. - @see getDataSize */ const void* getData() const noexcept; /** Returns the number of bytes of data that have been written to the stream. - @see getData */ size_t getDataSize() const noexcept { return size; } @@ -89,6 +87,9 @@ public: */ void preallocate (size_t bytesToPreallocate); + /** Appends the utf-8 bytes for a unicode character */ + void appendUTF8Char (juce_wchar character); + /** Returns a String created from the (UTF8) data that has been written to the stream. */ String toUTF8() const; @@ -120,7 +121,7 @@ private: size_t position, size; void trimExternalBlockSize(); - void prepareToWrite (size_t); + char* prepareToWrite (size_t); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryOutputStream) };