Solved

How to write a buffer to a TStream ?

Posted on 2003-11-01
2
340 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 165
FMX StringGrid1->Canvas->FillRect Problem 3 173
Delphi 2 69
Delphi android app hide keyboard 3 16
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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