Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Broken TBytes (https://www.delphipraxis.net/187095-broken-tbytes.html)

wil32 27. Okt 2015 19:42

Delphi-Version: XE8

Broken TBytes
 
Delphi-Quellcode:
procedure WhyIsItNotWorking;
var
Stream : TBytesStream;
b : TBytes ;
begin
Stream := TBytesStream.Create;
Stream.LoadFromFile( AttachedPDF );

b := Stream.Bytes ;

Stream := TBytesStream.Create( b );
Stream.SaveToFile( AttachedPDF + '2' );

FreeAndNil(Stream);
end;
2nd file is corrupted. there is a bunch of wrong bytes at the end of the file. if you notepad the 2nd pdf and remove said bytes. everything is normal

http://rghost.net/private/67PGPC8QF/...ccaf86fec0bb41

this is a form/fillable pdf

above code works with 'regular' pdf

Dejan Vu 28. Okt 2015 07:19

AW: Broken TBytes
 
Please format your code using the Delphi tags.

Edit your post, select your Code and press that little helmet button.
This will surround your Code with [ CODE="Delphi"] [ /CODE] Tags.

Then save.

Klaus01 28. Okt 2015 07:45

AW: Broken TBytes
 
Hi,
your TBytes Array doesn't have a defined size.

May this will help:
Delphi-Quellcode:
setlength(b,stream.size);
stream.ReadBuffer(b,stream.size);
Cheers Klaus

Der schöne Günther 28. Okt 2015 08:06

AW: Broken TBytes
 
Just use the debugger to inspect your
Delphi-Quellcode:
b: TBytes
after you've done your
Delphi-Quellcode:
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
Delphi-Quellcode:
LoadFromFile
.

The root cause is the protected method
Delphi-Quellcode:
Realloc
of
Delphi-Quellcode:
TMemoryStream
, the parent of your
Delphi-Quellcode:
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.

Sir Rufo 28. Okt 2015 09:01

AW: Broken TBytes
 
It is documented and works as designed:
Zitat:

The Bytes property returns the buffer in which the data is stored. Use the Size property to find the actual amount of data in the buffer.
(taken from http://docwiki.embarcadero.com/Libra...esStream.Bytes)

wil32 28. Okt 2015 12:20

AW: Broken TBytes
 
Thanks everyone. Please note that the 'issue' didn't happen on android

Sir Rufo 28. Okt 2015 13:07

AW: Broken TBytes
 
Zitat:

Zitat von wil32 (Beitrag 1319969)
Thanks everyone. Please note that the 'issue' didn't happen on android

You should note, that there is no issue at all ;)


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:15 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz