Small improvement for Tcompressionstream
Original Reporter info from Mantis: Marcin Wiazowski
-
Reporter name:
Original Reporter info from Mantis: Marcin Wiazowski
- Reporter name:
Description:
When writing compressed data to a stream, the Tcompressionstream implementation uses either stream's Write or WriteBuffer method.
Using the WriteBuffer method is a good way of writing data - in case of writing error, an exception is raised.
On the contrary - in case of writing error - the Write method just returns smaller number of written bytes as a result. The problem is that silent data loss will occur in such case:
Function TCompressionstream.ClearOutBuffer : Integer;
begin
{ Flush the buffer to the stream and update progress }
Result:=source.write(Fbuffer^,bufsize); <====== returned Result may be less than bufsize - and no exception is raised
inc(compressed_written,Result);
{ reset output buffer } <====== data buffer is cleared, although not fully written to the output stream
Fstream.next_out:=Fbuffer;
Fstream.avail_out:=bufsize;
end;
To avoid silent data loss, the attached patch converts Write call to a WriteBuffer call.
In addition, due to requesting "bufsize-Fstream.avail_out" bytes to be written, instead of just "bufsize", the modified ClearOutBuffer implementation may be also used to replace a bunch of code in a Tcompressionstream.flush implementation.
Since both these changes affect the same code, both of them are contained in the attached patch.
Mantis conversion info:
- Mantis ID: 36758
- Build: 44263
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 44282 (#ce3a9ee7)
- Target version: 4.0.0