How can i convert Tbytes
type to a Binary file
using MemoryStream
?
4 Answers
Or directly with a TFileStream
to cut down on the number of intermediate objects created:
procedure SaveBytesToFile(const Data: TBytes; const FileName: string);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
if Data <> nil then
Stream.WriteBuffer(Data[0], Length(Data));
finally
Stream.Free;
end;
end;
I don't believe using TMemoryStream
is helpful here since it just involves an extra unnecessary heap allocation/deallocation.
-
Thank you David :) . Which one is faster? using
TFileStream
orTMemoryStream.SaveToFile
?– KermiaJun 22, 2011 at 19:14 -
7
TMemoryStream.SaveToFile
does its job via aTFileStream
. Best to cut out the middle man and go straight toTFileStream
. Jun 22, 2011 at 19:16 -
You may consider moving the if-clause outside of the try finally, so the stream will not be created at all if nothing is to write. Jun 22, 2011 at 19:42
-
@uwe you'd still need to make an empty file and since this code does that perfectly well I'd be loath to add another test for what is likely a rare corner case.add another test for what is likely a rare corner case. Jun 22, 2011 at 19:45
Uwe's answer will work if you have TBytesStream available. If not:
procedure SaveBytesToFile(const Data: TBytes; const FileName: string);
var
stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
try
if length(data) > 0 then
stream.WriteBuffer(data[0], length(data));
stream.SaveToFile(FileName);
finally
stream.Free;
end;
end;
-
@Kermia You do realise you have accepted the answer with the worst performance characteristics? @Mason Please don't take offence! Jun 22, 2011 at 19:17
-
2Title says :
using MemoryStream
and I think I should select the related answer . isn't it?– KermiaJun 22, 2011 at 19:22 -
@David: For this particular case, yes. But if there's a lot of individual writes instead of just one, especially if they're small ones, you can save a huge amount of time by writing everything to a memory stream first and then blockwriting that to a file all at once. Jun 22, 2011 at 19:23
-
1@Kermia I have no problem with which answer you accept. I just want to be sure that you are clear that using
TMemoryStream
is a solution that is slower than necessary. This seemed to be relevant to you when you asked in a comment, "which is faster?" Jun 22, 2011 at 19:31 -
3@Mason An aside. You have used the wrong write method. You should use
WriteBuffer
instead since it's the one with error checking. I'm sure you know this and this was an oversight. Jun 22, 2011 at 19:35
Well, if answers mention Delphi XE and other streams than TMemoryStream
, then i suggest one more method.
procedure SaveBytesToFile(const Data: TBytes; const FileName: string);
begin
TFile.WriteAllBytes( FileName, Data );
end;
F.I. in Delphi XE:
procedure SaveBytesToFile(const Data: TBytes; const FileName: string);
var
stream: TBytesStream;
begin
stream := TBytesStream.Create(Data);
try
stream.SaveToFile(FileName);
finally
stream.Free;
end;
end;
-
-
-
4A TBytesStream or TMemoryStream creates a TFileStream behind the scenes when you use SaveToFile. It's probably better to just write directly to a TFileStream like David Heffernan shows.– PetriWJun 22, 2011 at 19:16
-
The whole purpose of my answer was to show that the answer actually lies at your feet when you happen to know the RTL. I didn't care about speed or memory considerations as that was not part of the question. Normally I start optimizing for speed and space when it is necessary. Jun 22, 2011 at 19:48
-
@Uwe I don't know
TBytesStream
. Does is copy the data or does it take a reference to it? Jun 22, 2011 at 19:54