AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Broken TBytes

Ein Thema von wil32 · begonnen am 27. Okt 2015 · letzter Beitrag vom 28. Okt 2015
Antwort Antwort
wil32

Registriert seit: 27. Okt 2015
3 Beiträge
 
#1

Broken TBytes

  Alt 27. Okt 2015, 19:42
Delphi-Version: XE8
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

Geändert von MrSpock (28. Okt 2015 um 07:23 Uhr) Grund: Delphi Tags eingefügt.
  Mit Zitat antworten Zitat
Dejan Vu
(Gast)

n/a Beiträge
 
#2

AW: Broken TBytes

  Alt 28. Okt 2015, 07:19
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.
  Mit Zitat antworten Zitat
Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.754 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Broken TBytes

  Alt 28. Okt 2015, 07:45
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
Klaus
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: Broken TBytes

  Alt 28. Okt 2015, 08:06
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.
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#5

AW: Broken TBytes

  Alt 28. Okt 2015, 09:01
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)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
wil32

Registriert seit: 27. Okt 2015
3 Beiträge
 
#6

AW: Broken TBytes

  Alt 28. Okt 2015, 12:20
Thanks everyone. Please note that the 'issue' didn't happen on android
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#7

AW: Broken TBytes

  Alt 28. Okt 2015, 13:07
Thanks everyone. Please note that the 'issue' didn't happen on android
You should note, that there is no issue at all
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:12 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