Discussion:
using TStream.CopyFrom in Same Stream
(too old to reply)
a***@aol.com
2012-08-12 18:41:13 UTC
Permalink
I want to delete a section in the middle of a MemoryStream, can I use
CopyFrom for this purpose as :

// Delete 218 bytes from MemoryStream

MemoryStream.Seek(MemDeleteStart + 218, soFromBeginning);
CopyStream := TMemoryStream.Create;
MemStrmSize := MemoryStream.Size;
BytesToCopy := MemoryStream.Size - (MemDeleteStart + 218)
CopyStream.CopyFrom(MemoryStream, BytesToCopy);
MemoryStream.Seek(MemDeleteStart, soFromBeginning);
MemoryStream.CopyFrom(CopyStream, 0);
MemoryStream.SetSize(MemStrmSize - 218);
CopyStream.Free;

. . . or is there a better way to do this ?

Alan Lloyd
a***@aol.com
2012-08-12 21:12:10 UTC
Permalink
Post by a***@aol.com
I want to delete a section in the middle of a MemoryStream, can I use
// Delete 218 bytes from MemoryStream
MemoryStream.Seek(MemDeleteStart + 218, soFromBeginning);
CopyStream := TMemoryStream.Create;
MemStrmSize := MemoryStream.Size;
BytesToCopy := MemoryStream.Size - (MemDeleteStart + 218)
CopyStream.CopyFrom(MemoryStream, BytesToCopy);
MemoryStream.Seek(MemDeleteStart, soFromBeginning);
MemoryStream.CopyFrom(CopyStream, 0);
MemoryStream.SetSize(MemStrmSize - 218);
CopyStream.Free;
. . . or is there a better way to do this ?
Alan Lloyd
Silly me ! I put down what I would have to do with _two_ streams.

When I thought about it I realised that I couldn't do it on the one
MemoryStream anyway, because I would need two pointers on the one
memory stream, & we only have one Read/Write pointer.

Alan Lloyd
Maarten Wiltink
2012-08-13 22:18:11 UTC
Permalink
Post by a***@aol.com
I want to delete a section in the middle of a MemoryStream, can I use
<snip code>
Post by a***@aol.com
. . . or is there a better way to do this ?
Anything expressed in terms of streams should work with TStreams,
without depending on TMemoryStream or whatever descendant. If you
are going to assume a memory buffer, don't wrap it in a stream.
That reduces the problem to a single Move for the content and a Dec
for the length.

Groetjes,
Maarten Wiltink

Loading...