Solved

How to write a buffer to a TStream ?

Posted on 2003-11-01
2
341 Views
Last Modified: 2010-05-18
Hi!

How can I copy a buffer to a TStream ?

I have the following code and it doesn't work:

var
  Stream : TStream;
begin
  Stream.Size := MaxBufferSize;
  Stream.Position := 0;
  Stream.Write(Buffer, Buffer_Length);
end

where Buffer is a Pchar containing the data and Buffer_Length is the length of Buffer.  MaxBufferSize is the max size of Buffer.

On the other hand,  If I do the following it works fine:
var
  Stream : TStream;
  List : TStringList;
begin
  Stream.Size := 0;
  Stream.Position := 0;
  List := TStringList.Create;
  List.Text := Buffer;
  List.SaveToStream(Stream);
  List.Free;
end;

Can somebody please tell me how exactly should I write the 'Buffer' to the stream ? ... The problem with using a TStringList is that the buffer might contain binary data instead of text.

BTW, the above code is just an example, ... that's the reason why I omited the creation of the stream, etc ..
Thanks a lot
0
Comment
Question by:delphi112497
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 9664598

If buffer is a pchar, then it should be

Stream.Write(Buffer^, Buffer_Length);

Regards,
Russell

0
 
LVL 2

Expert Comment

by:Robn
ID: 9671423
writing the buffer using Delphi Strings

procedure WriteBuffer(S: TStream; Text: String);
var
  Size: Integer;
begin
  Size := Length(Text);
  if Size > 0 then begin
    S.Write(PChar(Text)^,Size);
  end;
end;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question