[DUG] Potential bug in XE2
Jolyon Smith
jsmith at deltics.co.nz
Mon Jul 2 10:04:17 NZST 2012
imho ReadBuffer/WriteBuffer are over-engineered. They appear to be
implemented to code around potential bugs (or perhaps an idiosyncrasy) in a
stream implementation.
If I ask a stream to read/write N bytes then that is what the stream should
do. If it needs to break the request into smaller "chunks" to satisfy the
request then that should be transparent to the consumer code.
I would call this "chunking" rather than "buffered". Buffered read/write
I/IO typically works on blocks LARGER than the individual requests, not
smaller. i.e. you ask to read 4 bytes but a larger buffer of (e.g.) 1KB is
read so that future requests can be satisfied from the buffer, and asking
to write 4 bytes just stuffs a buffer which is only *actually* written when
then buffer is full, both designed to minimise I/O.
"Chunking" does the opposite.
On 30 June 2012 21:54, Todd Martin <todd.martin.nz at gmail.com> wrote:
> TStrings.SaveToStream calls Stream.WriteBuffer()
> whereas
> TStrings.LoadFromStream calls Stream.Read()
>
> No buffering and no error message, if the buffer is not read in completely!
>
> procedure TStrings.SaveToStream(Stream: TStream; Encoding: TEncoding);
> var
> Buffer, Preamble: TBytes;
> begin
> if Encoding = nil then
> Encoding := FDefaultEncoding;
> Buffer := Encoding.GetBytes(GetTextStr);
> if FWriteBOM then
> begin
> Preamble := Encoding.GetPreamble;
> if Length(Preamble) > 0 then
> Stream.WriteBuffer(Preamble[0], Length(Preamble));
> end;
> Stream.WriteBuffer(Buffer[0], Length(Buffer));
> end;
>
>
> procedure TStrings.LoadFromStream(Stream: TStream; Encoding: TEncoding);
> var
> Size, PreambleSize: Integer;
> Buffer: TBytes;
> begin
> BeginUpdate;
> try
> Size := Stream.Size - Stream.Position;
> SetLength(Buffer, Size);
> Stream.Read(Buffer[0], Size);
> PreambleSize:= TEncoding.GetBufferEncoding(Buffer, Encoding,
> FDefaultEncoding);
> SetEncoding(Encoding); // Keep Encoding in case the stream is saved
> SetTextStr(Encoding.GetString(Buffer, PreambleSize, Length(Buffer) -
> PreambleSize));
> finally
> EndUpdate;
> end;
> end;
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with
> Subject: unsubscribe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20120702/06cf1a06/attachment.html
More information about the Delphi
mailing list