- m_stream->m_lasterror = wxStream_NOERROR;
- if (!m_buffer_size && m_fixed)
- return (m_stream->m_lastcount = m_stream->OnSysWrite(buffer, size));
-
- // ------------------
- // Buffering enabled
- // ------------------
-
- size_t buf_left, orig_size = size;
-
- while (size > 0) {
- buf_left = m_buffer_end - m_buffer_pos;
-
- // First case: the buffer to write is larger than the stream buffer,
- // we split it
- // NB: If stream buffer isn't fixed (as for wxMemoryOutputStream),
- // we always go to the second case.
- if (size > buf_left && m_fixed) {
- PutToBuffer(buffer, buf_left);
- size -= buf_left;
- buffer = (char *)buffer + buf_left; // ANSI C++ violation.
-
- if (!FlushBuffer()) {
- CHECK_ERROR(wxStream_WRITE_ERR);
- return (m_stream->m_lastcount = orig_size-size);
- }
-
- 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;