Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent, binary data in a custom component

I am making a custom component to hold binary data in a program. What data type can hold and automatically stream binary data to a .DFM file?

I have tried TBytes, but it does not stream. String and AnsiString stream, but they are not good for binary data. I am trying to avoid using the manual TReader/TWriter approach.

Below is a little code piece that describes the problem. The BinProp (TBytes) is not streamed into the Blob.bin file, even though the BinProp property is published:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  tBlob = class (TComponent)
  private
    fBinProp : TBytes;
    fString : String;
    procedure SetBinProp(const Value: TBytes);
    procedure SetStringProp(const Value: String);
  public
  published
    property BinProp : TBytes read fBinProp write SetBinProp;
    property StringProp : String read fString write SetStringProp;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure tBlob.SetBinProp(const Value: TBytes);
begin
  fBinProp := Value;
end;

procedure tBlob.SetStringProp(const Value: String);
begin
  fString := Value;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Blob : tBlob;
  i : integer;
  Fs : TFileStream;
begin
  Blob := tBlob.Create(Self);
  SetLength(Blob.fBinProp, 10);
  for I := 0 to 9 do
    Blob.BinProp[I] := i;
  Blob.StringProp := '1234567890';
  Fs := TFileStream.Create('Blob.bin', fmCreate);
  Fs.WriteComponent(Blob);
  Fs.Free;
  Blob.Free;
  Fs := TFileStream.Create('Blob.bin', fmOpenRead);
  Blob := Fs.ReadComponent(nil) as TBlob;
  Caption :=
    'Length BinProp: '+IntToStr(Length(Blob.BinProp))+    //0
    ' - Length StringProp: '+IntToStr(Length(Blob.StringProp))+ // 10
    ' - Value StringProp: '+Blob.StringProp;   // 1234567890
  Fs.free;
end;

initialization
  RegisterClass(tBlob);
end.
like image 683
Thomas Riedel Avatar asked Sep 19 '25 20:09

Thomas Riedel


1 Answers

There is no data type that automatically streams binary data to/from a DFM. You must stream the data manually. Have your component override the virtual DefineProperties() method, then it can call the TFiler.DefineBinaryProperty() method to provide custom reader/writer methods that stream the binary data as needed.

See Overriding the DefineProperties Method in Embarcadero's DocWiki.

For example:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  tBlob = class (TComponent)
  private
    fBinProp : TBytes;
    fString : String;
    procedure ReadBinProp(Stream: TStream);
    procedure WriteBinProp(Stream: TStream);
  protected
    procedure DefineProperties(Filer: TFiler); override;
  public
  published
    property BinProp : TBytes read fBinProp write fBinProp;
    property StringProp : String read fString write fString;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure tBlob.DefineProperties(Filer: TFiler);
begin
  inherited DefineProperties(Filer);
  Filer.DefineBinaryProperty('BinPropBytes', ReadBinProp, WriteBinProp, Length(fBinProp) > 0);
end;

procedure tBlob.ReadBinProp(Stream: TStream);
var
  BinSize: Integer;
begin
  BinSize := Stream.Size;
  SetLength(fBinProp, BinSize);
  if BinSize > 0 then
    Stream.ReadBuffer(PByte(fBinProp)^, BinSize);
    // or, in XE3+:
    // Stream.ReadBuffer(fBinProp, BinSize);
end;

procedure tBlob.WriteBinProp(Stream: TStream);
begin
  Stream.WriteBuffer(PByte(fBinProp)^, Length(fBinProp));
  // or, in XE3+:
  // Stream.WriteBuffer(fBinProp, Length(fBinProp));
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Blob : tBlob;
  I : Integer;
  Fs : TFileStream;
begin
  Blob := tBlob.Create(nil);
  try
    SetLength(Blob.fBinProp, 10);
    for I := 0 to 9 do
      Blob.BinProp[I] := Byte(i);
    Blob.StringProp := '1234567890';
    Fs := TFileStream.Create('Blob.bin', fmCreate);
    try
      Fs.WriteComponent(Blob);
    finally
      Fs.Free;
    end;
    FreeAndNil(Blob);
    Fs := TFileStream.Create('Blob.bin', fmOpenRead or fmShareDenyWrite);
    try
      Blob := Fs.ReadComponent(nil) as TBlob;
    finally
      Fs.Free;
    end;
    Caption :=
      'Length BinProp: '+IntToStr(Length(Blob.BinProp))+    //0
      ' - Length StringProp: '+IntToStr(Length(Blob.StringProp))+ // 10
      ' - Value StringProp: '+Blob.StringProp;   // 1234567890
  finally
    Blob.Free;
  end;
end;

initialization
  RegisterClass(tBlob);

end.
like image 150
Remy Lebeau Avatar answered Sep 22 '25 05:09

Remy Lebeau