From 84d4c8794af52b897a25379679745c7531def2f5 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 8 Dec 2022 18:58:43 +0000 Subject: [PATCH] MemoryOutputStream: Avoid allocating unnecessarily large buffers Previously, we would allocate storage large enough to fit the entire contents of the input stream, even if this was lower than the maxNumBytesToWrite. --- modules/juce_core/streams/juce_MemoryOutputStream.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/juce_core/streams/juce_MemoryOutputStream.cpp b/modules/juce_core/streams/juce_MemoryOutputStream.cpp index 2ca69dc62c..d3be4a8bbd 100644 --- a/modules/juce_core/streams/juce_MemoryOutputStream.cpp +++ b/modules/juce_core/streams/juce_MemoryOutputStream.cpp @@ -176,11 +176,11 @@ int64 MemoryOutputStream::writeFromInputStream (InputStream& source, int64 maxNu if (availableData > 0) { - if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0) + if (maxNumBytesToWrite < 0 || availableData < maxNumBytesToWrite) maxNumBytesToWrite = availableData; if (blockToUse != nullptr) - preallocate (position + (size_t) availableData); + preallocate (position + (size_t) maxNumBytesToWrite); } return OutputStream::writeFromInputStream (source, maxNumBytesToWrite);