- m_buffer_pos = m_buffer_start;
-
- } else {
-
- // Second case: just copy it in the stream buffer.
- PutToBuffer(buffer, size);
- break;
+ if ( !HasBuffer() && m_fixed )
+ {
+ // no buffer, just forward the call to the stream
+ m_stream->m_lastcount = outStream->OnSysWrite(buffer, size);
+ }
+ else // we [may] have a buffer, use it
+ {
+ size_t orig_size = size;
+
+ while ( size > 0 )
+ {
+ size_t left = GetBytesLeft();
+
+ // if the buffer is too large to fit in the stream buffer, split
+ // it in smaller parts
+ //
+ // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
+ // we always go to the second case.
+ //
+ // FIXME: fine, but if it fails we should (re)try writing it by
+ // chunks as this will (hopefully) always work (VZ)
+ if ( size > left && m_fixed )
+ {
+ PutToBuffer(buffer, left);
+ size -= left;
+ buffer = (char *)buffer + left;
+
+ if ( !FlushBuffer() )
+ {
+ SetError(wxStream_WRITE_ERR);
+
+ break;
+ }
+
+ m_buffer_pos = m_buffer_start;
+ }
+ else // we can do it in one gulp
+ {
+ PutToBuffer(buffer, size);
+ size = 0;
+ }
+ }
+
+ m_stream->m_lastcount = orig_size - size;