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 <[email protected]> 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: [email protected]
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to [email protected] with
> Subject: unsubscribe
>
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: [email protected]
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to [email protected] with 
Subject: unsubscribe

Reply via email to