Browse Source

Minor addition to MemoryOutputStream.

tags/2021-05-28
jules 12 years ago
parent
commit
84724e8947
2 changed files with 18 additions and 17 deletions
  1. +13
    -13
      modules/juce_core/streams/juce_MemoryOutputStream.cpp
  2. +5
    -4
      modules/juce_core/streams/juce_MemoryOutputStream.h

+ 13
- 13
modules/juce_core/streams/juce_MemoryOutputStream.cpp View File

@@ -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 <char*> (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<char*> (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<char*> (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


+ 5
- 4
modules/juce_core/streams/juce_MemoryOutputStream.h View File

@@ -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)
};


Loading…
Cancel
Save