?
Solved

How to write a buffer to a TStream ?

Posted on 2003-11-01
2
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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