Just use the debugger to inspect your
b: TBytes
after you've done your
b := Stream.Bytes;
.
It already contains more bytes than your original file. Since the Bytes property just reads out an ordinary field, something must have gone wrong in
LoadFromFile
.
The root cause is the protected method
Realloc
of
TMemoryStream
, the parent of your
TBytesStream
. It makes sure the internal byte array is always aligned to a multiple of 8KB.
This way, the Byte property is garbage, you will have to truncate it afterwards, just as Klaus already said:
Delphi-Quellcode:
procedure WhyIsItNotWorking();
const
path = 'x:\cake.pdf';
var
Stream : TBytesStream;
b : TBytes ;
begin
Stream := TBytesStream.Create();
Stream.LoadFromFile(path);
b := Stream.Bytes;
SetLength(b, Stream.Size); // This is new
Stream := TBytesStream.Create(b);
Stream.SaveToFile( path.Replace('cake', 'cake2') );
FreeAndNil(Stream); // You know that you did not destroy the first TStream instance?
end;
I would have made the same mistake. I don't think this was to be expected.