11 Vote

Delphi/Lazarus: Write Stream to Stream

Question by Guest | Last update on 2024-01-23 | Created on 2014-12-15

I would like to write the content of a stream into another stream.

For example, the content of some TFileStream into an arbitrary TMemoryStream.

How can I achieve this?

ReplyPositiveNegative
2Best Answer2 Votes

Assuming having two streams, StreamA and StreamB, you can copy the content from StreamB into StreamA using the following code:

StreamB.Position := 0;
StreamA.CopyFrom(StreamB, StreamB.Size);

It is important to set the position to 0 first. Otherwise (if the current stream position is not 0) it will not be copied from the beginning of the stream (except, of course, you only want to copy a part of the stream beginning somewhere in the middle).

The second parameter of CopyFrom is the length that should be copied. With passing "StreamB.Size", the full length of the stream is copied, with values smaller than the size of the stream, it is possible to copy only a part of the stream.
Last update on 2024-01-23 | Created on 2014-12-17

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.