@@ -148,14 +148,104 @@ bool MCMemoryInputStreamCreate(const void *p_block, size_t p_size, MCStreamRef&
148148
149149// //////////////////////////////////////////////////////////////////////////////
150150
151+ struct __MCMemoryOutputStream
152+ {
153+ void *buffer;
154+ size_t length;
155+ size_t capacity;
156+ };
157+
158+ static void __MCMemoryOutputStreamDestroy (MCStreamRef p_stream)
159+ {
160+ __MCMemoryOutputStream *self;
161+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (p_stream);
162+
163+ free (self -> buffer);
164+ }
165+
166+ static bool __MCMemoryOutputStreamIsFinished (MCStreamRef p_stream, bool & r_finished)
167+ {
168+ __MCMemoryOutputStream *self;
169+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (p_stream);
170+ r_finished = false ;
171+ return true ;
172+ }
173+
174+ static bool __MCMemoryOutputStreamGetAvailableForWrite (MCStreamRef p_stream, size_t & r_amount)
175+ {
176+ __MCMemoryOutputStream *self;
177+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (p_stream);
178+ r_amount = SIZE_MAX;
179+ return true ;
180+ }
181+
182+ static bool __MCMemoryOutputStreamWrite (MCStreamRef p_stream, const void *p_buffer, size_t p_amount)
183+ {
184+ __MCMemoryOutputStream *self;
185+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (p_stream);
186+ if (p_amount > self -> capacity - self -> length)
187+ {
188+ size_t t_new_capacity;
189+ t_new_capacity = (self -> length + p_amount + 65536 ) & ~65535 ;
190+
191+ void *t_new_buffer;
192+ t_new_buffer = realloc (self -> buffer, t_new_capacity);
193+ if (t_new_buffer == nil)
194+ return false ;
195+
196+ self -> buffer = t_new_buffer;
197+ self -> capacity = t_new_capacity;
198+ }
199+ MCMemoryCopy ((byte_t *)self -> buffer + self -> length, p_buffer, p_amount);
200+ self -> length += p_amount;
201+ return true ;
202+ }
203+
204+ static MCStreamCallbacks kMCMemoryOutputStreamCallbacks =
205+ {
206+ __MCMemoryOutputStreamDestroy,
207+ __MCMemoryOutputStreamIsFinished,
208+ nil,
209+ nil,
210+ __MCMemoryOutputStreamGetAvailableForWrite,
211+ __MCMemoryOutputStreamWrite,
212+ nil,
213+ nil,
214+ nil,
215+ nil,
216+ nil,
217+ };
218+
151219bool MCMemoryOutputStreamCreate (MCStreamRef& r_stream)
152220{
153- return false ;
221+ MCStreamRef t_stream;
222+ if (!MCStreamCreate (&kMCMemoryOutputStreamCallbacks , sizeof (__MCMemoryOutputStream), t_stream))
223+ return false ;
224+
225+ __MCMemoryOutputStream *self;
226+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (t_stream);
227+ self -> buffer = nil;
228+ self -> length = 0 ;
229+ self -> capacity = 0 ;
230+
231+ r_stream = t_stream;
232+
233+ return true ;
154234}
155235
156- bool MCMemoryOutputStreamFinish (MCStreamRef stream , void *& r_buffer, size_t & r_size)
236+ bool MCMemoryOutputStreamFinish (MCStreamRef p_stream , void *& r_buffer, size_t & r_size)
157237{
158- return false ;
238+ __MCMemoryOutputStream *self;
239+ self = (__MCMemoryOutputStream *)MCStreamGetExtraBytesPtr (p_stream);
240+
241+ r_buffer = realloc (self -> buffer, self -> length);
242+ r_size = self -> length;
243+
244+ self -> buffer = nil;
245+ self -> length = 0 ;
246+ self -> capacity = 0 ;
247+
248+ return true ;
159249}
160250
161251// //////////////////////////////////////////////////////////////////////////////
@@ -427,7 +517,7 @@ bool MCStreamWriteUInt32(MCStreamRef self, uint32_t p_value)
427517 return MCStreamWrite (self, &t_swapped_value, sizeof (uint32_t ));
428518}
429519
430- bool MCStreamReadUInt64 (MCStreamRef self, uint64_t p_value)
520+ bool MCStreamWriteUInt64 (MCStreamRef self, uint64_t p_value)
431521{
432522 uint64_t t_swapped_value;
433523 t_swapped_value = MCSwapInt64NetworkToHost (p_value);
0 commit comments